Changeset - r28680:0915cea9bab0
[Not reviewed]
master
0 9 0
Rubidium - 10 months ago 2024-02-03 18:03:47
rubidium@openttd.org
Codechange: use std::unique_ptr for receiving network packets
9 files changed with 19 insertions and 28 deletions:
0 comments (0 inline, 0 general)
src/network/core/tcp.cpp
Show inline comments
 
@@ -41,7 +41,6 @@ void NetworkTCPSocketHandler::EmptyPacke
 
	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
 
 * 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::Receive
 
		}
 
	}
 

	
 
	/* Prepare for receiving a new packet */
 
	this->packet_recv = nullptr;
 

	
 
	p->PrepareToRead();
 
	return p;
 
	return std::move(this->packet_recv);
 
}
 

	
 
/**
src/network/core/tcp.h
Show inline comments
 
@@ -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();
 

	
src/network/core/tcp_admin.cpp
Show inline comments
 
@@ -108,10 +108,9 @@ NetworkRecvStatus NetworkAdminSocketHand
 
 */
 
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;
 
	}
 

	
src/network/core/tcp_content.cpp
Show inline comments
 
@@ -146,12 +146,11 @@ bool NetworkContentSocketHandler::Receiv
 
	 *
 
	 * 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;
 
	}
 

	
src/network/core/tcp_coordinator.cpp
Show inline comments
 
@@ -64,12 +64,11 @@ bool NetworkCoordinatorSocketHandler::Re
 
	 *
 
	 * 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;
 
	}
 

	
src/network/core/tcp_game.cpp
Show inline comments
 
@@ -135,10 +135,9 @@ NetworkRecvStatus NetworkGameSocketHandl
 
 */
 
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;
 
	}
 

	
src/network/core/tcp_turn.cpp
Show inline comments
 
@@ -43,12 +43,11 @@ bool NetworkTurnSocketHandler::HandlePac
 
 */
 
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;
 
	}
 

	
src/network/network_server.cpp
Show inline comments
 
@@ -239,7 +239,7 @@ ServerNetworkGameSocketHandler::~ServerN
 
	}
 
}
 

	
 
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::
 

	
 
	/* 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;
 
}
src/network/network_server.h
Show inline comments
 
@@ -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;
 

	
0 comments (0 inline, 0 general)