diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp index 626f978b42..b792aa96f9 100644 --- a/src/network/core/tcp.cpp +++ b/src/network/core/tcp.cpp @@ -41,7 +41,6 @@ void NetworkTCPSocketHandler::EmptyPacketQueue() while (this->packet_queue != nullptr) { delete Packet::PopFromQueue(&this->packet_queue); } - delete this->packet_recv; this->packet_recv = nullptr; } @@ -141,17 +140,17 @@ SendPacketsState NetworkTCPSocketHandler::SendPackets(bool closing_down) * Receives a packet for the given client * @return The received packet (or nullptr when it didn't receive one) */ -Packet *NetworkTCPSocketHandler::ReceivePacket() +std::unique_ptr NetworkTCPSocketHandler::ReceivePacket() { ssize_t res; if (!this->IsConnected()) return nullptr; if (this->packet_recv == nullptr) { - this->packet_recv = new Packet(this, TCP_MTU); + this->packet_recv = std::make_unique(this, TCP_MTU); } - Packet *p = this->packet_recv; + Packet *p = this->packet_recv.get(); /* Read packet size */ if (!p->HasPacketSizeData()) { @@ -203,11 +202,8 @@ Packet *NetworkTCPSocketHandler::ReceivePacket() } } - /* Prepare for receiving a new packet */ - this->packet_recv = nullptr; - p->PrepareToRead(); - return p; + return std::move(this->packet_recv); } /** diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h index caade125b0..e4b49b31cd 100644 --- a/src/network/core/tcp.h +++ b/src/network/core/tcp.h @@ -31,7 +31,7 @@ enum SendPacketsState { class NetworkTCPSocketHandler : public NetworkSocketHandler { private: Packet *packet_queue; ///< Packets that are awaiting delivery - Packet *packet_recv; ///< Partially received packet + std::unique_ptr packet_recv; ///< Partially received packet void EmptyPacketQueue(); public: @@ -50,7 +50,7 @@ public: virtual void SendPacket(Packet *packet); SendPacketsState SendPackets(bool closing_down = false); - virtual Packet *ReceivePacket(); + virtual std::unique_ptr ReceivePacket(); bool CanSendReceive(); diff --git a/src/network/core/tcp_admin.cpp b/src/network/core/tcp_admin.cpp index 895817cad3..fe1750482e 100644 --- a/src/network/core/tcp_admin.cpp +++ b/src/network/core/tcp_admin.cpp @@ -108,10 +108,9 @@ NetworkRecvStatus NetworkAdminSocketHandler::HandlePacket(Packet *p) */ NetworkRecvStatus NetworkAdminSocketHandler::ReceivePackets() { - Packet *p; + std::unique_ptr p; while ((p = this->ReceivePacket()) != nullptr) { - NetworkRecvStatus res = this->HandlePacket(p); - delete p; + NetworkRecvStatus res = this->HandlePacket(p.get()); if (res != NETWORK_RECV_STATUS_OKAY) return res; } diff --git a/src/network/core/tcp_content.cpp b/src/network/core/tcp_content.cpp index 11df443b86..199c70b11c 100644 --- a/src/network/core/tcp_content.cpp +++ b/src/network/core/tcp_content.cpp @@ -146,12 +146,11 @@ bool NetworkContentSocketHandler::ReceivePackets() * * What arbitrary number to choose is the ultimate question though. */ - Packet *p; + std::unique_ptr p; static const int MAX_PACKETS_TO_RECEIVE = 42; int i = MAX_PACKETS_TO_RECEIVE; while (--i != 0 && (p = this->ReceivePacket()) != nullptr) { - bool cont = this->HandlePacket(p); - delete p; + bool cont = this->HandlePacket(p.get()); if (!cont) return true; } diff --git a/src/network/core/tcp_coordinator.cpp b/src/network/core/tcp_coordinator.cpp index cffb36ab6d..70533657b8 100644 --- a/src/network/core/tcp_coordinator.cpp +++ b/src/network/core/tcp_coordinator.cpp @@ -64,12 +64,11 @@ bool NetworkCoordinatorSocketHandler::ReceivePackets() * * What arbitrary number to choose is the ultimate question though. */ - Packet *p; + std::unique_ptr p; static const int MAX_PACKETS_TO_RECEIVE = 42; int i = MAX_PACKETS_TO_RECEIVE; while (--i != 0 && (p = this->ReceivePacket()) != nullptr) { - bool cont = this->HandlePacket(p); - delete p; + bool cont = this->HandlePacket(p.get()); if (!cont) return true; } diff --git a/src/network/core/tcp_game.cpp b/src/network/core/tcp_game.cpp index 0f8051eef6..fc199c5d60 100644 --- a/src/network/core/tcp_game.cpp +++ b/src/network/core/tcp_game.cpp @@ -135,10 +135,9 @@ NetworkRecvStatus NetworkGameSocketHandler::HandlePacket(Packet *p) */ NetworkRecvStatus NetworkGameSocketHandler::ReceivePackets() { - Packet *p; + std::unique_ptr p; while ((p = this->ReceivePacket()) != nullptr) { - NetworkRecvStatus res = HandlePacket(p); - delete p; + NetworkRecvStatus res = HandlePacket(p.get()); if (res != NETWORK_RECV_STATUS_OKAY) return res; } diff --git a/src/network/core/tcp_turn.cpp b/src/network/core/tcp_turn.cpp index 76096aeb2b..0fc549aacd 100644 --- a/src/network/core/tcp_turn.cpp +++ b/src/network/core/tcp_turn.cpp @@ -43,12 +43,11 @@ bool NetworkTurnSocketHandler::HandlePacket(Packet *p) */ bool NetworkTurnSocketHandler::ReceivePackets() { - Packet *p; + std::unique_ptr p; static const int MAX_PACKETS_TO_RECEIVE = 4; int i = MAX_PACKETS_TO_RECEIVE; while (--i != 0 && (p = this->ReceivePacket()) != nullptr) { - bool cont = this->HandlePacket(p); - delete p; + bool cont = this->HandlePacket(p.get()); if (!cont) return true; } diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 1dda7dbcf4..7e8a11dffa 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -239,7 +239,7 @@ ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler() } } -Packet *ServerNetworkGameSocketHandler::ReceivePacket() +std::unique_ptr ServerNetworkGameSocketHandler::ReceivePacket() { /* Only allow receiving when we have some buffer free; this value * can go negative, but eventually it will become positive again. */ @@ -247,7 +247,7 @@ Packet *ServerNetworkGameSocketHandler::ReceivePacket() /* We can receive a packet, so try that and if needed account for * the amount of received data. */ - Packet *p = this->NetworkTCPSocketHandler::ReceivePacket(); + std::unique_ptr p = this->NetworkTCPSocketHandler::ReceivePacket(); if (p != nullptr) this->receive_limit -= p->Size(); return p; } diff --git a/src/network/network_server.h b/src/network/network_server.h index 2ff1c9798f..973b30157e 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -75,7 +75,7 @@ public: ServerNetworkGameSocketHandler(SOCKET s); ~ServerNetworkGameSocketHandler(); - Packet *ReceivePacket() override; + std::unique_ptr ReceivePacket() override; NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override; std::string GetClientName() const;