Codechange: use std::unique_ptr for receiving network packets

This commit is contained in:
Rubidium 2024-02-03 19:03:47 +01:00 committed by rubidium42
parent 4ba62594e5
commit c77a45ed86
9 changed files with 19 additions and 28 deletions

View File

@ -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<Packet> 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<Packet>(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);
}
/**

View File

@ -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> 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<Packet> ReceivePacket();
bool CanSendReceive();

View File

@ -108,10 +108,9 @@ NetworkRecvStatus NetworkAdminSocketHandler::HandlePacket(Packet *p)
*/
NetworkRecvStatus NetworkAdminSocketHandler::ReceivePackets()
{
Packet *p;
std::unique_ptr<Packet> 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;
}

View File

@ -146,12 +146,11 @@ bool NetworkContentSocketHandler::ReceivePackets()
*
* What arbitrary number to choose is the ultimate question though.
*/
Packet *p;
std::unique_ptr<Packet> 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;
}

View File

@ -64,12 +64,11 @@ bool NetworkCoordinatorSocketHandler::ReceivePackets()
*
* What arbitrary number to choose is the ultimate question though.
*/
Packet *p;
std::unique_ptr<Packet> 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;
}

View File

@ -135,10 +135,9 @@ NetworkRecvStatus NetworkGameSocketHandler::HandlePacket(Packet *p)
*/
NetworkRecvStatus NetworkGameSocketHandler::ReceivePackets()
{
Packet *p;
std::unique_ptr<Packet> 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;
}

View File

@ -43,12 +43,11 @@ bool NetworkTurnSocketHandler::HandlePacket(Packet *p)
*/
bool NetworkTurnSocketHandler::ReceivePackets()
{
Packet *p;
std::unique_ptr<Packet> 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;
}

View File

@ -239,7 +239,7 @@ ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler()
}
}
Packet *ServerNetworkGameSocketHandler::ReceivePacket()
std::unique_ptr<Packet> 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<Packet> p = this->NetworkTCPSocketHandler::ReceivePacket();
if (p != nullptr) this->receive_limit -= p->Size();
return p;
}

View File

@ -75,7 +75,7 @@ public:
ServerNetworkGameSocketHandler(SOCKET s);
~ServerNetworkGameSocketHandler();
Packet *ReceivePacket() override;
std::unique_ptr<Packet> ReceivePacket() override;
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override;
std::string GetClientName() const;