2015-03-09 11:37:02 -06:00
|
|
|
// Copyright 2015 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-03-09 11:37:02 -06:00
|
|
|
|
2023-04-11 09:48:46 -06:00
|
|
|
#include "Common/ENet.h"
|
2015-03-09 11:37:02 -06:00
|
|
|
|
2015-05-10 19:16:05 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2022-10-08 18:39:38 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2015-05-10 19:16:05 -06:00
|
|
|
|
2023-04-11 07:12:01 -06:00
|
|
|
namespace Common::ENet
|
2015-03-09 11:37:02 -06:00
|
|
|
{
|
|
|
|
void WakeupThread(ENetHost* host)
|
|
|
|
{
|
|
|
|
// Send ourselves a spurious message. This is hackier than it should be.
|
|
|
|
// comex reported this as https://github.com/lsalzman/enet/issues/23, so
|
|
|
|
// hopefully there will be a better way to do it in the future.
|
|
|
|
ENetAddress address;
|
|
|
|
if (host->address.port != 0)
|
|
|
|
address.port = host->address.port;
|
|
|
|
else
|
|
|
|
enet_socket_get_address(host->socket, &address);
|
|
|
|
address.host = 0x0100007f; // localhost
|
|
|
|
u8 byte = 0;
|
|
|
|
ENetBuffer buf;
|
|
|
|
buf.data = &byte;
|
|
|
|
buf.dataLength = 1;
|
|
|
|
enet_socket_send(host->socket, &address, &buf, 1);
|
|
|
|
}
|
|
|
|
|
2015-03-14 08:19:18 -06:00
|
|
|
int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event)
|
|
|
|
{
|
|
|
|
// wakeup packet received
|
|
|
|
if (host->receivedDataLength == 1 && host->receivedData[0] == 0)
|
|
|
|
{
|
2023-11-03 09:15:04 -06:00
|
|
|
event->type = static_cast<ENetEventType>(SKIPPABLE_EVENT);
|
2015-03-14 08:19:18 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2022-10-08 18:25:28 -06:00
|
|
|
|
2022-10-08 18:39:38 -06:00
|
|
|
bool SendPacket(ENetPeer* socket, const sf::Packet& packet, u8 channel_id)
|
2022-10-08 18:25:28 -06:00
|
|
|
{
|
2023-02-01 00:55:05 -07:00
|
|
|
if (!socket)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(NETPLAY, "Target socket is null.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-08 18:25:28 -06:00
|
|
|
ENetPacket* epac =
|
|
|
|
enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
2022-10-08 18:39:38 -06:00
|
|
|
if (!epac)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(NETPLAY, "Failed to create ENetPacket ({} bytes).", packet.getDataSize());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int result = enet_peer_send(socket, channel_id, epac);
|
|
|
|
if (result != 0)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(NETPLAY, "Failed to send ENetPacket (error code {}).", result);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-10-08 18:25:28 -06:00
|
|
|
}
|
2023-04-11 07:12:01 -06:00
|
|
|
} // namespace Common::ENet
|