Serialisation: Adjust method names/types

This commit is contained in:
Jonathan G Rennison
2023-06-14 18:16:23 +01:00
parent 4b807e091a
commit 11a3dc287b
8 changed files with 38 additions and 30 deletions

View File

@@ -16,6 +16,7 @@
#include <vector>
#include <string>
#include <limits>
void BufferSend_bool (std::vector<byte> &buffer, size_t limit, bool data);
void BufferSend_uint8 (std::vector<byte> &buffer, size_t limit, uint8 data);
@@ -23,8 +24,8 @@ void BufferSend_uint16(std::vector<byte> &buffer, size_t limit, uint16 data);
void BufferSend_uint32(std::vector<byte> &buffer, size_t limit, uint32 data);
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);
size_t BufferSend_binary_until_full(std::vector<byte> &buffer, size_t limit, const byte *begin, const byte *end);
void BufferSend_binary(std::vector<byte> &buffer, size_t limit, const byte *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>
@@ -65,18 +66,23 @@ struct BufferSerialisationHelper {
BufferSend_string(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data);
}
size_t Send_bytes(const byte *begin, const byte *end)
size_t Send_binary_until_full(const byte *begin, const byte *end)
{
T *self = static_cast<T *>(this);
return BufferSend_bytes(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), begin, end);
return BufferSend_binary_until_full(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), begin, end);
}
void Send_binary(const char *data, const size_t size)
void Send_binary(const byte *data, const size_t size)
{
T *self = static_cast<T *>(this);
BufferSend_binary(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data, size);
}
void Send_binary(const byte *data, const byte *end)
{
this->Send_binary(data, end - data);
}
void Send_buffer(const byte *data, const size_t size)
{
T *self = static_cast<T *>(this);
@@ -247,7 +253,7 @@ public:
* @param buffer The buffer to put the data into.
* @param size The size of the data.
*/
void Recv_binary(char *buffer, size_t size)
void Recv_binary(byte *buffer, size_t size)
{
if (!this->CanRecvBytes(size, true)) return;
@@ -262,14 +268,16 @@ public:
* @param buffer The buffer to put the data into.
* @param size The size of the data.
*/
void Recv_binary(std::string &buffer, size_t size)
std::vector<uint8> Recv_binary(size_t size)
{
if (!this->CanRecvBytes(size, true)) return;
if (!this->CanRecvBytes(size, true)) return {};
auto &pos = static_cast<T *>(this)->GetDeserialisationPosition();
buffer.assign((const char *) &this->GetBuffer()[pos], size);
std::vector<uint8> buffer { &this->GetBuffer()[pos], &this->GetBuffer()[pos + size] };
pos += (decltype(pos)) size;
return buffer;
}
/**
@@ -290,4 +298,13 @@ public:
}
};
struct BufferSerialiser : public BufferSerialisationHelper<BufferSerialiser> {
std::vector<byte> &buffer;
BufferSerialiser(std::vector<byte> &buffer) : buffer(buffer) {}
std::vector<byte> &GetSerialisationBuffer() { return this->buffer; }
size_t GetSerialisationLimit() const { return std::numeric_limits<size_t>::max(); }
};
#endif /* SERIALISATION_HPP */