summaryrefslogtreecommitdiff
path: root/src/network/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/network/core')
-rw-r--r--src/network/core/packet.cpp28
-rw-r--r--src/network/core/packet.h2
2 files changed, 30 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,
diff --git a/src/network/core/packet.h b/src/network/core/packet.h
index 277ff8bba..04a232e1c 100644
--- a/src/network/core/packet.h
+++ b/src/network/core/packet.h
@@ -72,6 +72,7 @@ public:
void Send_uint32(uint32 data);
void Send_uint64(uint64 data);
void Send_string(const std::string_view data);
+ void Send_buffer(const std::vector<byte> &data);
size_t Send_bytes (const byte *begin, const byte *end);
/* Reading/receiving of packets */
@@ -87,6 +88,7 @@ public:
uint16 Recv_uint16();
uint32 Recv_uint32();
uint64 Recv_uint64();
+ std::vector<byte> Recv_buffer();
std::string Recv_string(size_t length, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
size_t RemainingBytesToTransfer() const;