diff --git a/src/core/serialisation.hpp b/src/core/serialisation.hpp index 13bd45b0be..71b7d8731b 100644 --- a/src/core/serialisation.hpp +++ b/src/core/serialisation.hpp @@ -11,6 +11,7 @@ #define SERIALISATION_HPP #include "bitmath_func.hpp" +#include "span_type.hpp" #include "../string_type.h" #include "../string_func.h" @@ -264,19 +265,48 @@ public: } /** - * Reads binary data. - * @param buffer The buffer to put the data into. + * Returns view of binary data. * @param size The size of the data. + * @return The view of the data. */ - std::vector Recv_binary(size_t size) + span Recv_binary_view(size_t size) { if (!this->CanRecvBytes(size, true)) return {}; auto &pos = static_cast(this)->GetDeserialisationPosition(); - std::vector buffer { &this->GetBuffer()[pos], &this->GetBuffer()[pos + size] }; + span view { &this->GetBuffer()[pos], size }; pos += (decltype(pos)) size; + return view; + } + + /** + * Reads binary data. + * @param size The size of the data. + * @return The binary buffer. + */ + std::vector Recv_binary(size_t size) + { + span 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 Recv_buffer_view() + { + uint16 length = this->Recv_uint16(); + + if (!this->CanRecvBytes(length, true)) return {}; + + auto &pos = static_cast(this)->GetDeserialisationPosition(); + span buffer { &this->GetBuffer()[pos], length }; + pos += length; + return buffer; } @@ -286,15 +316,9 @@ public: */ std::vector Recv_buffer() { - uint16 length = this->Recv_uint16(); + span view = this->Recv_buffer_view(); - 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; + return { view.begin(), view.end() }; } };