From 9249af970f91adf2d05d52076a54c2cf3ac6dacb Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Wed, 25 May 2022 12:40:13 +0200 Subject: Check return value for enet_peer_send If it fails, we will need to destroy the packet ourselves rather than relying on enet to eventually destroy it. --- src/net/connection.cpp | 18 +++++++++++++----- src/net/netcomputer.cpp | 6 +++++- 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/net/connection.cpp b/src/net/connection.cpp index c54c0dbb..b2bc7cef 100644 --- a/src/net/connection.cpp +++ b/src/net/connection.cpp @@ -31,8 +31,8 @@ #endif Connection::Connection(): - mRemote(0), - mLocal(0) + mRemote(nullptr), + mLocal(nullptr) { } @@ -86,8 +86,8 @@ void Connection::stop() if (mLocal) enet_host_destroy(mLocal); - mRemote = 0; - mLocal = 0; + mRemote = nullptr; + mLocal = nullptr; } bool Connection::isConnected() const @@ -110,9 +110,17 @@ void Connection::send(const MessageOut &msg, bool reliable, unsigned channel) reliable ? ENET_PACKET_FLAG_RELIABLE : 0); if (packet) - enet_peer_send(mRemote, channel, packet); + { + if (enet_peer_send(mRemote, channel, packet) < 0) + { + LOG_ERROR("Failure to send packet!"); + enet_packet_destroy(packet); + } + } else + { LOG_ERROR("Failure to create packet!"); + } } void Connection::process() diff --git a/src/net/netcomputer.cpp b/src/net/netcomputer.cpp index 5d86f13a..f2d9607b 100644 --- a/src/net/netcomputer.cpp +++ b/src/net/netcomputer.cpp @@ -70,7 +70,11 @@ void NetComputer::send(const MessageOut &msg, bool reliable, if (packet) { - enet_peer_send(mPeer, channel, packet); + if (enet_peer_send(mPeer, channel, packet) < 0) + { + LOG_ERROR("Failure to send packet!"); + enet_packet_destroy(packet); + } } else { -- cgit v1.2.3-70-g09d2