summaryrefslogtreecommitdiff
path: root/src/network/core
diff options
context:
space:
mode:
authorRubidium <rubidium@openttd.org>2021-04-18 12:29:34 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-04-24 20:42:01 +0200
commitf71fb0f54af443cee37d3c41a0d4f39a24617741 (patch)
tree5b3f1aeb2f54f72fa29b6ad19f3507c27b6b901c /src/network/core
parent6f161f655942f2ca0091a75cdab8e3260e31bb5f (diff)
downloadopenttd-f71fb0f54af443cee37d3c41a0d4f39a24617741.tar.xz
Codechange: encapsulate reading the size of a Packet
Diffstat (limited to 'src/network/core')
-rw-r--r--src/network/core/packet.cpp12
-rw-r--r--src/network/core/packet.h1
-rw-r--r--src/network/core/udp.cpp2
3 files changed, 14 insertions, 1 deletions
diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp
index c033aec98..e32b7fad8 100644
--- a/src/network/core/packet.cpp
+++ b/src/network/core/packet.cpp
@@ -231,6 +231,18 @@ bool Packet::HasPacketSizeData() const
}
/**
+ * 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.
*/
diff --git a/src/network/core/packet.h b/src/network/core/packet.h
index 4eb4703c1..b6a7ff5d3 100644
--- a/src/network/core/packet.h
+++ b/src/network/core/packet.h
@@ -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);
diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp
index 8e476f4e2..3bd2151fe 100644
--- a/src/network/core/udp.cpp
+++ b/src/network/core/udp.cpp
@@ -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;
}