summaryrefslogtreecommitdiff
path: root/src/network/core/packet.cpp
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2021-10-28 23:48:26 +0200
committerMichael Lutz <michi@icosahedron.de>2021-12-16 22:28:32 +0100
commita05fd7aa50ecbee425df2d6f9015ec3ea359232f (patch)
tree9b9208d39813670789635b638592a903a2aafaa4 /src/network/core/packet.cpp
parentb0990fcff7358e839468e5cf811ffddc8b9d73e2 (diff)
downloadopenttd-a05fd7aa50ecbee425df2d6f9015ec3ea359232f.tar.xz
Change: [Network] Transfer command data as serialized byte stream without fixed structure.
The data will be transmitted as the length followed by the serialized data. This allows the command data to be different for every command type in the future.
Diffstat (limited to 'src/network/core/packet.cpp')
-rw-r--r--src/network/core/packet.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp
index e106d5787..ec0919757 100644
--- a/src/network/core/packet.cpp
+++ b/src/network/core/packet.cpp
@@ -186,6 +186,17 @@ void Packet::Send_string(const std::string_view data)
}
/**
+ * Copy a sized byte buffer into the packet.
+ * @param data The data to send.
+ */
+void Packet::Send_buffer(const std::vector<byte> &data)
+{
+ assert(this->CanWriteToPacket(sizeof(uint16) + data.size()));
+ this->Send_uint16((uint16)data.size());
+ this->buffer.insert(this->buffer.end(), data.begin(), data.end());
+}
+
+/**
* Send as many of the bytes as possible in the packet. This can mean
* that it is possible that not all bytes are sent. To cope with this
* the function returns the amount of bytes that were actually sent.
@@ -367,6 +378,23 @@ uint64 Packet::Recv_uint64()
}
/**
+ * Extract a sized byte buffer from the packet.
+ * @return The extracted buffer.
+ */
+std::vector<byte> Packet::Recv_buffer()
+{
+ uint16 size = this->Recv_uint16();
+ if (size == 0 || !this->CanReadFromPacket(size, true)) return {};
+
+ std::vector<byte> data;
+ while (size-- > 0) {
+ data.push_back(this->buffer[this->pos++]);
+ }
+
+ return data;
+}
+
+/**
* Reads characters (bytes) from the packet until it finds a '\0', or reaches a
* maximum of \c length characters.
* When the '\0' has not been reached in the first \c length read characters,