ENet.cpp 1.7 KB

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