Codechange: encapsulate reading the size of a Packet

This commit is contained in:
Rubidium
2021-04-18 12:29:34 +02:00
committed by rubidium42
parent 6f161f6559
commit f71fb0f54a
5 changed files with 17 additions and 4 deletions

View File

@@ -230,6 +230,18 @@ bool Packet::HasPacketSizeData() const
return this->pos >= sizeof(PacketSize);
}
/**
* Get the number of bytes in the packet.
* When sending a packet this is the size of the data up to that moment.
* When receiving a packet (before PrepareToRead) this is the allocated size for the data to be read.
* When reading a packet (after PrepareToRead) this is the full size of the packet.
* @return The packet's size.
*/
size_t Packet::Size() const
{
return this->size;
}
/**
* Reads the packet size from the raw packet and stores it in the packet->size
* @return True iff the packet size seems plausible.

View File

@@ -77,6 +77,7 @@ public:
/* Reading/receiving of packets */
bool HasPacketSizeData() const;
bool ParsePacketSize();
size_t Size() const;
void PrepareToRead();
bool CanReadFromPacket(size_t bytes_to_read, bool close_connection = false);

View File

@@ -137,7 +137,7 @@ void NetworkUDPSocketHandler::ReceivePackets()
/* If the size does not match the packet must be corrupted.
* Otherwise it will be marked as corrupted later on. */
if (!p.ParsePacketSize() || nbytes != p.size) {
if (!p.ParsePacketSize() || (size_t)nbytes != p.Size()) {
DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString().c_str());
continue;
}