Serialisation: Add helpers to send/recv length-prefixed binary buffers
This commit is contained in:
@@ -137,6 +137,22 @@ void BufferSend_binary(std::vector<byte> &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<byte> &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);
|
||||
|
@@ -25,6 +25,7 @@ void BufferSend_uint64(std::vector<byte> &buffer, size_t limit, uint64 data);
|
||||
void BufferSend_string(std::vector<byte> &buffer, size_t limit, const std::string_view data);
|
||||
size_t BufferSend_bytes (std::vector<byte> &buffer, size_t limit, const byte *begin, const byte *end);
|
||||
void BufferSend_binary(std::vector<byte> &buffer, size_t limit, const char *data, const size_t size);
|
||||
void BufferSend_buffer(std::vector<byte> &buffer, size_t limit, const byte *data, const size_t size);
|
||||
|
||||
template <typename T>
|
||||
struct BufferSerialisationHelper {
|
||||
@@ -75,6 +76,17 @@ struct BufferSerialisationHelper {
|
||||
T *self = static_cast<T *>(this);
|
||||
BufferSend_binary(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data, size);
|
||||
}
|
||||
|
||||
void Send_buffer(const byte *data, const size_t size)
|
||||
{
|
||||
T *self = static_cast<T *>(this);
|
||||
BufferSend_buffer(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data, size);
|
||||
}
|
||||
|
||||
void Send_buffer(const std::vector<byte> &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<uint8> Recv_buffer()
|
||||
{
|
||||
uint16 length = this->Recv_uint16();
|
||||
|
||||
if (!this->CanRecvBytes(length, true)) return {};
|
||||
|
||||
auto &pos = static_cast<T *>(this)->GetDeserialisationPosition();
|
||||
std::vector<uint8> buffer { &this->GetBuffer()[pos], &this->GetBuffer()[pos + length] };
|
||||
pos += length;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* SERIALISATION_HPP */
|
||||
|
Reference in New Issue
Block a user