From 66684a392f15344845368e880cfbe421f6e9af38 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Sun, 9 Oct 2022 02:25:28 +0200 Subject: [PATCH 1/2] Deduplicate NetPlayServer::Send() and NetPlayClient::Send() into ENetUtil::SendPacket(). --- Source/Core/Common/ENetUtil.cpp | 7 +++++++ Source/Core/Common/ENetUtil.h | 5 +++++ Source/Core/Core/NetPlayClient.cpp | 4 +--- Source/Core/Core/NetPlayServer.cpp | 4 +--- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Source/Core/Common/ENetUtil.cpp b/Source/Core/Common/ENetUtil.cpp index 646e7f5439..e450ee65d1 100644 --- a/Source/Core/Common/ENetUtil.cpp +++ b/Source/Core/Common/ENetUtil.cpp @@ -35,4 +35,11 @@ int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event) } return 0; } + +void SendPacket(ENetPeer* socket, const sf::Packet& packet, u8 channel_id) +{ + ENetPacket* epac = + enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(socket, channel_id, epac); +} } // namespace ENetUtil diff --git a/Source/Core/Common/ENetUtil.h b/Source/Core/Common/ENetUtil.h index 62270ce533..11a45a4e05 100644 --- a/Source/Core/Common/ENetUtil.h +++ b/Source/Core/Common/ENetUtil.h @@ -5,8 +5,13 @@ #include +#include + +#include "Common/CommonTypes.h" + namespace ENetUtil { void WakeupThread(ENetHost* host); int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event); +void SendPacket(ENetPeer* socket, const sf::Packet& packet, u8 channel_id); } // namespace ENetUtil diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index 013d7e4fe5..72b403e4d0 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -1465,9 +1465,7 @@ void NetPlayClient::OnGameDigestAbort() void NetPlayClient::Send(const sf::Packet& packet, const u8 channel_id) { - ENetPacket* epac = - enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE); - enet_peer_send(m_server, channel_id, epac); + ENetUtil::SendPacket(m_server, packet, channel_id); } void NetPlayClient::DisplayPlayersPing() diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index 3d044e9121..1c4b46216f 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -2008,9 +2008,7 @@ void NetPlayServer::SendToClients(const sf::Packet& packet, const PlayerId skip_ void NetPlayServer::Send(ENetPeer* socket, const sf::Packet& packet, const u8 channel_id) { - ENetPacket* epac = - enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE); - enet_peer_send(socket, channel_id, epac); + ENetUtil::SendPacket(socket, packet, channel_id); } void NetPlayServer::KickPlayer(PlayerId player) From bfbc04ef5ec6bc51a931108456e174ec2f291155 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Sun, 9 Oct 2022 02:39:38 +0200 Subject: [PATCH 2/2] ENetUtil: Check return values of ENet functions in SendPacket(). --- Source/Core/Common/ENetUtil.cpp | 18 ++++++++++++++++-- Source/Core/Common/ENetUtil.h | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Source/Core/Common/ENetUtil.cpp b/Source/Core/Common/ENetUtil.cpp index e450ee65d1..d06de7c82a 100644 --- a/Source/Core/Common/ENetUtil.cpp +++ b/Source/Core/Common/ENetUtil.cpp @@ -4,6 +4,7 @@ #include "Common/ENetUtil.h" #include "Common/CommonTypes.h" +#include "Common/Logging/Log.h" namespace ENetUtil { @@ -36,10 +37,23 @@ int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event) return 0; } -void SendPacket(ENetPeer* socket, const sf::Packet& packet, u8 channel_id) +bool SendPacket(ENetPeer* socket, const sf::Packet& packet, u8 channel_id) { ENetPacket* epac = enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE); - enet_peer_send(socket, channel_id, epac); + 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; } } // namespace ENetUtil diff --git a/Source/Core/Common/ENetUtil.h b/Source/Core/Common/ENetUtil.h index 11a45a4e05..5b06e5a007 100644 --- a/Source/Core/Common/ENetUtil.h +++ b/Source/Core/Common/ENetUtil.h @@ -13,5 +13,5 @@ namespace ENetUtil { void WakeupThread(ENetHost* host); int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event); -void SendPacket(ENetPeer* socket, const sf::Packet& packet, u8 channel_id); +bool SendPacket(ENetPeer* socket, const sf::Packet& packet, u8 channel_id); } // namespace ENetUtil