Serialisation: Add view/span variants of binary receive methods
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
#define SERIALISATION_HPP
|
#define SERIALISATION_HPP
|
||||||
|
|
||||||
#include "bitmath_func.hpp"
|
#include "bitmath_func.hpp"
|
||||||
|
#include "span_type.hpp"
|
||||||
#include "../string_type.h"
|
#include "../string_type.h"
|
||||||
#include "../string_func.h"
|
#include "../string_func.h"
|
||||||
|
|
||||||
@@ -264,19 +265,48 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads binary data.
|
* Returns view of binary data.
|
||||||
* @param buffer The buffer to put the data into.
|
|
||||||
* @param size The size of the data.
|
* @param size The size of the data.
|
||||||
|
* @return The view of the data.
|
||||||
*/
|
*/
|
||||||
std::vector<uint8> Recv_binary(size_t size)
|
span<const uint8> Recv_binary_view(size_t size)
|
||||||
{
|
{
|
||||||
if (!this->CanRecvBytes(size, true)) return {};
|
if (!this->CanRecvBytes(size, true)) return {};
|
||||||
|
|
||||||
auto &pos = static_cast<T *>(this)->GetDeserialisationPosition();
|
auto &pos = static_cast<T *>(this)->GetDeserialisationPosition();
|
||||||
|
|
||||||
std::vector<uint8> buffer { &this->GetBuffer()[pos], &this->GetBuffer()[pos + size] };
|
span<const uint8> view { &this->GetBuffer()[pos], size };
|
||||||
pos += (decltype(pos)) size;
|
pos += (decltype(pos)) size;
|
||||||
|
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads binary data.
|
||||||
|
* @param size The size of the data.
|
||||||
|
* @return The binary buffer.
|
||||||
|
*/
|
||||||
|
std::vector<uint8> Recv_binary(size_t size)
|
||||||
|
{
|
||||||
|
span<const uint8> view = this->Recv_binary_view(size);
|
||||||
|
|
||||||
|
return { view.begin(), view.end() };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a view of a length-prefixed binary buffer from the packet.
|
||||||
|
* @return The binary buffer.
|
||||||
|
*/
|
||||||
|
span<const uint8> Recv_buffer_view()
|
||||||
|
{
|
||||||
|
uint16 length = this->Recv_uint16();
|
||||||
|
|
||||||
|
if (!this->CanRecvBytes(length, true)) return {};
|
||||||
|
|
||||||
|
auto &pos = static_cast<T *>(this)->GetDeserialisationPosition();
|
||||||
|
span<const uint8> buffer { &this->GetBuffer()[pos], length };
|
||||||
|
pos += length;
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,15 +316,9 @@ public:
|
|||||||
*/
|
*/
|
||||||
std::vector<uint8> Recv_buffer()
|
std::vector<uint8> Recv_buffer()
|
||||||
{
|
{
|
||||||
uint16 length = this->Recv_uint16();
|
span<const uint8> view = this->Recv_buffer_view();
|
||||||
|
|
||||||
if (!this->CanRecvBytes(length, true)) return {};
|
return { view.begin(), view.end() };
|
||||||
|
|
||||||
auto &pos = static_cast<T *>(this)->GetDeserialisationPosition();
|
|
||||||
std::vector<uint8> buffer { &this->GetBuffer()[pos], &this->GetBuffer()[pos + length] };
|
|
||||||
pos += length;
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user