diff --git a/src/core/serialisation.cpp b/src/core/serialisation.cpp index 3458dc73f5..94a0dea161 100644 --- a/src/core/serialisation.cpp +++ b/src/core/serialisation.cpp @@ -137,6 +137,22 @@ void BufferSend_binary(std::vector &buffer, size_t limit, const char *data buffer.insert(buffer.end(), data, data + size); } +/** + * Sends a binary buffer over the network. + * The data is length prefixed with a uint16. + * @param data The string to send + */ +void BufferSend_buffer(std::vector &buffer, size_t limit, const byte *data, const size_t size) +{ + assert(size <= UINT16_MAX); + assert(BufferCanWriteToPacket(buffer, limit, size + 2)); + buffer.insert(buffer.end(), { + (uint8)GB(size, 0, 8), + (uint8)GB(size, 8, 8), + }); + buffer.insert(buffer.end(), data, data + size); +} + void BufferRecvStringValidate(std::string &buffer, StringValidationSettings settings) { StrMakeValidInPlace(buffer, settings); diff --git a/src/core/serialisation.hpp b/src/core/serialisation.hpp index a3a10f01c9..33b962bd56 100644 --- a/src/core/serialisation.hpp +++ b/src/core/serialisation.hpp @@ -25,6 +25,7 @@ void BufferSend_uint64(std::vector &buffer, size_t limit, uint64 data); void BufferSend_string(std::vector &buffer, size_t limit, const std::string_view data); size_t BufferSend_bytes (std::vector &buffer, size_t limit, const byte *begin, const byte *end); void BufferSend_binary(std::vector &buffer, size_t limit, const char *data, const size_t size); +void BufferSend_buffer(std::vector &buffer, size_t limit, const byte *data, const size_t size); template struct BufferSerialisationHelper { @@ -75,6 +76,17 @@ struct BufferSerialisationHelper { T *self = static_cast(this); BufferSend_binary(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data, size); } + + void Send_buffer(const byte *data, const size_t size) + { + T *self = static_cast(this); + BufferSend_buffer(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data, size); + } + + void Send_buffer(const std::vector &data) + { + this->Send_buffer(data.data(), data.size()); + } }; void BufferRecvStringValidate(std::string &buffer, StringValidationSettings settings); @@ -259,6 +271,23 @@ public: buffer.assign((const char *) &this->GetBuffer()[pos], size); pos += (decltype(pos)) size; } + + /** + * Reads a length-prefixed binary buffer from the packet. + * @return The binary buffer. + */ + std::vector Recv_buffer() + { + uint16 length = this->Recv_uint16(); + + if (!this->CanRecvBytes(length, true)) return {}; + + auto &pos = static_cast(this)->GetDeserialisationPosition(); + std::vector buffer { &this->GetBuffer()[pos], &this->GetBuffer()[pos + length] }; + pos += length; + + return buffer; + } }; #endif /* SERIALISATION_HPP */