From 450178d780eb885717c53a2dad62587332efc0f4 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Wed, 21 Apr 2021 07:10:09 +0200 Subject: [PATCH] Codechange: add accessor for the packet type to Packet and make the internal state of Packet private --- src/network/core/packet.cpp | 10 ++++++++++ src/network/core/packet.h | 4 ++-- src/network/network_server.cpp | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp index 9e9ce69012..d534df4d0d 100644 --- a/src/network/core/packet.cpp +++ b/src/network/core/packet.cpp @@ -296,6 +296,16 @@ void Packet::PrepareToRead() this->pos = sizeof(PacketSize); } +/** + * Get the \c PacketType from this packet. + * @return The packet type. + */ +PacketType Packet::GetPacketType() const +{ + assert(this->Size() >= sizeof(PacketSize) + sizeof(PacketType)); + return static_cast(buffer[sizeof(PacketSize)]); +} + /** * Read a boolean from the packet. * @return The read data. diff --git a/src/network/core/packet.h b/src/network/core/packet.h index d7ab7fee63..1a9c9faea6 100644 --- a/src/network/core/packet.h +++ b/src/network/core/packet.h @@ -40,6 +40,7 @@ typedef uint8 PacketType; ///< Identifier for the packet * (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0)) */ struct Packet { +private: /** The next packet. Used for queueing packets before sending. */ Packet *next; /** @@ -52,8 +53,6 @@ struct Packet { PacketSize pos; /** The buffer of this packet, of basically variable length up to SEND_MTU. */ byte *buffer; - -private: /** Socket we're associated with. */ NetworkSocketHandler *cs; @@ -82,6 +81,7 @@ public: bool ParsePacketSize(); size_t Size() const; void PrepareToRead(); + PacketType GetPacketType() const; bool CanReadFromPacket(size_t bytes_to_read, bool close_connection = false); bool Recv_bool (); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 80a9c56a0c..a0d1a00666 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -626,7 +626,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap() for (uint i = 0; (has_packets = this->savegame->HasPackets()) && i < sent_packets; i++) { Packet *p = this->savegame->PopPacket(); - last_packet = p->buffer[2] == PACKET_SERVER_MAP_DONE; + last_packet = p->GetPacketType() == PACKET_SERVER_MAP_DONE; this->SendPacket(p);