summaryrefslogtreecommitdiff
path: root/src/network/core/packet.cpp
diff options
context:
space:
mode:
authorRubidium <rubidium@openttd.org>2021-04-18 10:49:12 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-04-24 20:42:01 +0200
commit6f161f655942f2ca0091a75cdab8e3260e31bb5f (patch)
tree6bdcff599e22887cde7f9badbb3c03d63dd2edcd /src/network/core/packet.cpp
parent38d15fc9b788e2c904705d2ba8de4d5f1ff7988d (diff)
downloadopenttd-6f161f655942f2ca0091a75cdab8e3260e31bb5f.tar.xz
Codechange: encapsulate the logic about how many bytes can be sent from a buffer in to a Packet
Diffstat (limited to 'src/network/core/packet.cpp')
-rw-r--r--src/network/core/packet.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp
index 6e6bb51c0..c033aec98 100644
--- a/src/network/core/packet.cpp
+++ b/src/network/core/packet.cpp
@@ -175,6 +175,21 @@ void Packet::Send_string(const char *data)
while ((this->buffer[this->size++] = *data++) != '\0') {}
}
+/**
+ * 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.
+ * @param begin The begin of the buffer to send.
+ * @param end The end of the buffer to send.
+ * @return The number of bytes that were added to this packet.
+ */
+size_t Packet::Send_bytes(const byte *begin, const byte *end)
+{
+ size_t amount = std::min<size_t>(end - begin, SEND_MTU - this->size);
+ memcpy(this->buffer + this->size, begin, amount);
+ this->size += static_cast<PacketSize>(amount);
+ return amount;
+}
/*
* Receiving commands