| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/ENet.h" |
| 5 | |
| 6 | #include "Common/CommonTypes.h" |
| 7 | #include "Common/Logging/Log.h" |
| 8 | |
| 9 | namespace Common::ENet |
| 10 | { |
| 11 | void WakeupThread(ENetHost* host) |
| 12 | { |
| 13 | // Send ourselves a spurious message. This is hackier than it should be. |
| 14 | // comex reported this as https://github.com/lsalzman/enet/issues/23, so |
| 15 | // hopefully there will be a better way to do it in the future. |
| 16 | ENetAddress address; |
| 17 | if (host->address.port != 0) |
| 18 | address.port = host->address.port; |
| 19 | else |
| 20 | enet_socket_get_address(host->socket, &address); |
| 21 | address.host = 0x0100007f; // localhost |
| 22 | u8 byte = 0; |
| 23 | ENetBuffer buf; |
| 24 | buf.data = &byte; |
| 25 | buf.dataLength = 1; |
| 26 | enet_socket_send(host->socket, &address, &buf, 1); |
| 27 | } |
| 28 | |
| 29 | int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event) |
| 30 | { |
| 31 | // wakeup packet received |
| 32 | if (host->receivedDataLength == 1 && host->receivedData[0] == 0) |
| 33 | { |
| 34 | event->type = static_cast<ENetEventType>(SKIPPABLE_EVENT); |
| 35 | return 1; |
| 36 | } |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | bool SendPacket(ENetPeer* socket, const sf::Packet& packet, u8 channel_id) |
| 41 | { |
| 42 | if (!socket) |
| 43 | { |
| 44 | ERROR_LOG_FMT(NETPLAY, "Target socket is null."); |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | ENetPacket* epac = |
| 49 | enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE); |
| 50 | if (!epac) |
| 51 | { |
| 52 | ERROR_LOG_FMT(NETPLAY, "Failed to create ENetPacket ({} bytes).", packet.getDataSize()); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | const int result = enet_peer_send(socket, channel_id, epac); |
| 57 | if (result != 0) |
| 58 | { |
| 59 | ERROR_LOG_FMT(NETPLAY, "Failed to send ENetPacket (error code {}).", result); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | } // namespace Common::ENet |