5 Commits

Author SHA1 Message Date
b8dafa4eb1 refactor DataBuffer
All checks were successful
Linux arm64 / Build (push) Successful in 16s
2025-08-01 12:44:05 +02:00
c5b3281be7 remove unused include 2025-08-01 12:41:22 +02:00
9374332cd2 refactor SerializableMessage
All checks were successful
Linux arm64 / Build (push) Successful in 16s
2025-07-31 17:56:01 +02:00
bce37f59df allow message serialization through DataBuffer
All checks were successful
Linux arm64 / Build (push) Successful in 15s
2025-07-31 15:01:37 +02:00
a1a4176801 add more DataBuffer serialization types
All checks were successful
Linux arm64 / Build (push) Successful in 17s
2025-07-31 14:12:33 +02:00
10 changed files with 394 additions and 161 deletions

View File

@@ -10,9 +10,11 @@
#include <cassert>
#include <cstdint>
#include <cstring>
#include <list>
#include <map>
#include <sp/common/VarInt.h>
#include <memory>
#include <sp/common/ByteSwapping.h>
#include <sp/common/VarInt.h>
#include <string>
#include <vector>
@@ -59,67 +61,6 @@ class DataBuffer {
std::memcpy(&m_Buffer[end_pos], &a_Data, size);
}
/**
* \brief Append data to the buffer (converted to big endian)
*/
template <typename T>
DataBuffer& operator<<(T a_Data) {
SwapBytes(a_Data);
Append(a_Data);
return *this;
}
/**
* \brief Append a string to the buffer
* \warning Don't use it for binary data !
* \param str The string to append
*/
DataBuffer& operator<<(const std::string& str);
/**
* \brief Append data to the buffer from another const buffer
* \param data The buffer to append
*/
DataBuffer& operator<<(const DataBuffer& data);
/**
* \brief Append a vector to the buffer by first writing the size
* \param data The vector to append
*/
template <typename T>
DataBuffer& operator<<(const std::vector<T>& data) {
*this << VarInt{data.size()};
for (const auto& element : data) {
*this << element;
}
return *this;
}
/**
* \brief Append a map to the buffer by first writing the size
* \param data The map to append
*/
template <typename K, typename V>
DataBuffer& operator<<(const std::map<K, V>& data) {
*this << VarInt{data.size()};
for (const auto& element : data) {
*this << element.first << element.second;
}
return *this;
}
/**
* \brief Append an array to the buffer by first writing the size
* \param data The buffer to append
*/
template <typename T, std::size_t Size>
DataBuffer& operator<<(const std::array<T, Size>& data) {
for (const auto& element : data) {
*this << element;
}
return *this;
}
/**
* \brief Read data into a_Data
* \warning No endian checks
@@ -131,75 +72,6 @@ class DataBuffer {
m_ReadOffset += sizeof(T);
}
/**
* \brief Read some data from the buffer and assign to desired variable
*/
template <typename T>
DataBuffer& operator>>(T& a_Data) {
Read(a_Data);
SwapBytes(a_Data);
return *this;
}
/**
* \brief Read some data from the buffer and assign to the new buffer
* \param data The buffer to assign
*/
DataBuffer& operator>>(DataBuffer& data);
/**
* \brief Read a string from the buffer
* \param str The string to assign in the new buffer
* \warning Don't use it for binary data !
*/
DataBuffer& operator>>(std::string& str);
/**
* \brief Read a vector (size + data) from the buffer
* \pre The vector is assumed to be empty
*/
template <typename T>
DataBuffer& operator>>(std::vector<T>& data) {
VarInt arraySize;
*this >> arraySize;
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
T newElement;
*this >> newElement;
data.push_back(newElement);
}
return *this;
}
/**
* \brief Read a map (size + data) from the buffer
* \pre The map is assumed to be empty
*/
template <typename K, typename V>
DataBuffer& operator>>(std::map<K, V>& data) {
VarInt mapSize;
*this >> mapSize;
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
K newKey;
V newValue;
*this >> newKey >> newValue;
data.emplace(newKey, newValue);
}
return *this;
}
/**
* \brief Read an array from the buffer
*/
template <std::size_t Size, typename T>
DataBuffer& operator>>(std::array<T, Size>& data) {
for (std::size_t i = 0; i < Size; i++) {
T newElement;
*this >> newElement;
data[i] = newElement;
}
return *this;
}
/**
* \brief Write some data to the buffer
* \param buffer The buffer to write
@@ -251,7 +123,6 @@ class DataBuffer {
m_Buffer.reserve(amount);
}
/**
* \brief Clear the buffer
*/
@@ -333,6 +204,8 @@ class DataBuffer {
return m_Buffer == other.m_Buffer;
}
void insert(iterator a_DestBegin, const_iterator a_SrcBegin, const_iterator a_SrcEnd);
iterator begin();
iterator end();
const_iterator begin() const;
@@ -340,8 +213,9 @@ class DataBuffer {
};
/**
* \brief Operator << to write a DataBuffer to an ostream
* \brief Append data to the buffer from another const buffer
* \param data The buffer to append
*/
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
DataBuffer& operator<<(DataBuffer& a_Buffer, const DataBuffer& data);
} // namespace sp

View File

@@ -0,0 +1,268 @@
#pragma once
#include <sp/common/DataBuffer.h>
#include <boost/pfr.hpp>
namespace sp {
template<typename T>
using is_default_serializable = std::bool_constant<(std::is_class_v<T> && std::is_aggregate_v<T>) || !std::is_class_v<T>>;
template<typename T>
static constexpr bool is_default_serializable_v = is_default_serializable<T>::value;
/**
* \brief Append data to the buffer (converted to big endian)
*/
template <typename T, typename = typename std::enable_if_t<is_default_serializable_v<T>>>
DataBuffer& operator<<(DataBuffer& a_Buffer, const T& a_Data) {
if constexpr (std::is_class_v<T>) {
boost::pfr::for_each_field(a_Data, [&a_Buffer](const auto& a_Field) { a_Buffer << a_Field; });
} else {
a_Buffer.Append(a_Data);
SwapBytes(a_Buffer.data() + a_Buffer.GetReadOffset() - sizeof(T), a_Buffer.data() + a_Buffer.GetReadOffset());
}
return a_Buffer;
}
/**
* \brief Append a string to the buffer
* \warning Don't use it for binary data !
* \param str The string to append
*/
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::string& str);
/**
* \brief Operator << to write a DataBuffer to an ostream
*/
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
/**
* \brief Append a pointer to the buffer
* \param data The data to append
*/
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::shared_ptr<T>& data) {
return a_Buffer << *data;
}
/**
* \brief Append a pointer to the buffer
* \param data The data to append
*/
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::unique_ptr<T>& data) {
return a_Buffer << *data;
}
/**
* \brief Append a vector to the buffer by first writing the size
* \param data The vector to append
*/
template <typename T>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::vector<T>& data) {
a_Buffer << VarInt{data.size()};
for (const auto& element : data) {
a_Buffer << element;
}
return a_Buffer;
}
/**
* \brief Append a list to the buffer by first writing the size
* \param data The list to append
*/
template <typename T>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::list<T>& data) {
a_Buffer << VarInt{data.size()};
for (const auto& element : data) {
a_Buffer << element;
}
return a_Buffer;
}
/**
* \brief Append a map to the buffer by first writing the size
* \param data The map to append
*/
template <typename K, typename V>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::map<K, V>& data) {
a_Buffer << VarInt{data.size()};
for (const auto& [key, value] : data) {
a_Buffer << key << value;
}
return a_Buffer;
}
/**
* \brief Append a map to the buffer by first writing the size
* \param data The map to append
*/
template <typename K, typename V>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::unordered_map<K, V>& data) {
a_Buffer << VarInt{data.size()};
for (const auto& [key, value] : data) {
a_Buffer << key << value;
}
return a_Buffer;
}
/**
* \brief Append a pair to the buffer
* \param data The pair to append
*/
template <typename K, typename V>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::pair<K, V>& data) {
return a_Buffer << data.first << data.second;
}
/**
* \brief Append an array to the buffer by first writing the size
* \param data The buffer to append
*/
template <typename T, std::size_t Size>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::array<T, Size>& data) {
for (const auto& element : data) {
a_Buffer << element;
}
return a_Buffer;
}
/**
* \brief Read some data from the buffer and assign to desired variable
*/
template <typename T, typename = typename std::enable_if_t<is_default_serializable_v<T>>>
DataBuffer& operator>>(DataBuffer& a_Buffer, T& a_Data) {
if constexpr (std::is_class_v<T>) {
boost::pfr::for_each_field(a_Data, [&a_Buffer](auto& a_Field) { a_Buffer >> a_Field; });
} else {
a_Buffer.Read(a_Data);
SwapBytes(a_Data);
}
return a_Buffer;
}
/**
* \brief Read some data from the buffer and assign to the new buffer
* \param data The buffer to assign
*/
DataBuffer& operator>>(DataBuffer& a_Buffer, DataBuffer& data);
/**
* \brief Read a string from the buffer
* \param str The string to assign in the new buffer
* \warning Don't use it for binary data !
*/
DataBuffer& operator>>(DataBuffer& a_Buffer, std::string& str);
/**
* \brief Read a pointer
*/
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::shared_ptr<T>& data) {
data = std::make_shared<T>();
return a_Buffer >> *data;
}
/**
* \brief Read a pointer
*/
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::unique_ptr<T>& data) {
data = std::make_unique<T>();
return a_Buffer >> *data;
}
/**
* \brief Read a vector (size + data) from the buffer
* \pre The vector is assumed to be empty
*/
template <typename T>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::vector<T>& data) {
VarInt arraySize;
a_Buffer >> arraySize;
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
T newElement;
a_Buffer >> newElement;
data.push_back(newElement);
}
return a_Buffer;
}
/**
* \brief Read a list (size + data) from the buffer
* \pre The list is assumed to be empty
*/
template <typename T>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::list<T>& data) {
VarInt arraySize;
a_Buffer >> arraySize;
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
T newElement;
a_Buffer >> newElement;
data.push_back(newElement);
}
return a_Buffer;
}
/**
* \brief Read a map (size + data) from the buffer
* \pre The map is assumed to be empty
*/
template <typename K, typename V>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::map<K, V>& data) {
VarInt mapSize;
a_Buffer >> mapSize;
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
K newKey;
V newValue;
a_Buffer >> newKey >> newValue;
data.emplace(newKey, newValue);
}
return a_Buffer;
}
/**
* \brief Read a map (size + data) from the buffer
* \pre The map is assumed to be empty
*/
template <typename K, typename V>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::unordered_map<K, V>& data) {
VarInt mapSize;
a_Buffer >> mapSize;
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
K newKey;
V newValue;
a_Buffer >> newKey >> newValue;
data.emplace(newKey, newValue);
}
return a_Buffer;
}
/**
* \brief Read a pair
*/
template <typename K, typename V>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::pair<K, V>& data) {
return a_Buffer >> data.first >> data.second;
}
/**
* \brief Read an array from the buffer
*/
template <std::size_t Size, typename T>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::array<T, Size>& data) {
for (std::size_t i = 0; i < Size; i++) {
T newElement;
a_Buffer >> newElement;
data[i] = newElement;
}
return a_Buffer;
}
} // namespace sp

View File

@@ -1,6 +1,5 @@
#pragma once
#include <sp/common/NonCopyable.h>
#include <sp/io/IoInterface.h>
namespace sp {

View File

@@ -1,7 +1,7 @@
#pragma once
#include <boost/pfr.hpp>
#include <sp/io/BitBuffer.h>
#include <sp/common/DataBufferOperators.h>
namespace sp {
namespace details {

View File

@@ -8,7 +8,10 @@ namespace sp {
template <typename TMessageFactory>
DataBuffer MessageStream<TMessageFactory>::ReadAndDecapsulate() {
VarInt messageLength;
messageLength.Read([this](std::uint8_t& data) { m_Stream->Read(1) >> data; });
messageLength.Read([this](std::uint8_t& data) {
DataBuffer buffer = m_Stream->Read(1);
data = *buffer.data();
});
std::size_t amount = messageLength.GetValue();
DataBuffer buffer = m_Stream->Read(amount);

View File

@@ -0,0 +1,83 @@
#pragma once
#include <sp/common/DataBuffer.h>
namespace sp {
template <typename TMessageFactory>
class SerializableMessage {
using MessageBaseType = typename TMessageFactory::MessageBaseType;
using HandlerType = typename MessageBaseType::HandlerType;
using MessageIdType = typename MessageBaseType::MessageIdType;
private:
std::unique_ptr<MessageBaseType> m_Message;
public:
SerializableMessage(std::unique_ptr<MessageBaseType>&& a_MessagePtr) : m_Message(std::move(a_MessagePtr)) {}
operator MessageBaseType&() {
return *m_Message;
}
SerializableMessage& operator=(std::unique_ptr<MessageBaseType>&& a_MessagePtr) {
m_Message = std::move(a_MessagePtr);
return *this;
}
MessageIdType GetId() const {
return m_Message->GetId();
}
void Dispatch(HandlerType& handler) const {
m_Message->Dispatch(handler);
}
void Read(DataBuffer& a_Buffer) {
m_Message->Read(a_Buffer);
}
DataBuffer Write() const {
return m_Message->Write();
}
};
template <typename TMessageFactory>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::unique_ptr<SerializableMessage<TMessageFactory>>& a_Message) {
return a_Buffer << VarInt{static_cast<std::uint64_t>(a_Message->GetId())} << a_Message->Write();
}
template <typename TMessageFactory>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::shared_ptr<SerializableMessage<TMessageFactory>>& a_Message) {
return a_Buffer << VarInt{static_cast<std::uint64_t>(a_Message->GetId())} << a_Message->Write();
}
template <typename TMessageFactory>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::unique_ptr<SerializableMessage<TMessageFactory>>& a_Message) {
using MsgId = typename TMessageFactory::IdType;
static TMessageFactory factory;
VarInt msgId;
a_Buffer >> msgId;
a_Message = std::make_unique<SerializableMessage<TMessageFactory>>(std::move(factory.CreateMessage(MsgId(msgId.GetValue()))));
a_Message->Read(a_Buffer);
return a_Buffer;
}
template <typename TMessageFactory>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::shared_ptr<SerializableMessage<TMessageFactory>>& a_Message) {
using MsgId = typename TMessageFactory::IdType;
static TMessageFactory factory;
VarInt msgId;
a_Buffer >> msgId;
a_Message = std::make_shared<SerializableMessage<TMessageFactory>>(std::move(factory.CreateMessage(MsgId(msgId.GetValue()))));
a_Message->Read(a_Buffer);
return a_Buffer;
}
} // namespace sp

View File

@@ -13,7 +13,7 @@ class ConcreteMessage : public MessageBase {
using HandlerType = typename MessageBase::HandlerType;
template <typename... T>
ConcreteMessage(const T&... args) : m_Data{args...} {}
ConcreteMessage(T&&... args) : m_Data{std::move(args)...} {}
virtual ~ConcreteMessage() {}

View File

@@ -21,33 +21,33 @@ DataBuffer::DataBuffer(const DataBuffer& other, Data::difference_type offset) :
std::copy(other.m_Buffer.begin() + offset, other.m_Buffer.end(), std::back_inserter(m_Buffer));
}
DataBuffer& DataBuffer::operator<<(const std::string& str) {
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::string& str) {
std::size_t strlen = str.length() + 1; // including null character
Resize(GetSize() + strlen);
std::memcpy(m_Buffer.data() + GetSize() - strlen, str.data(), strlen);
return *this;
a_Buffer.Resize(a_Buffer.GetSize() + strlen);
std::memcpy(a_Buffer.data() + a_Buffer.GetSize() - strlen, str.data(), strlen);
return a_Buffer;
}
DataBuffer& DataBuffer::operator<<(const DataBuffer& data) {
m_Buffer.insert(m_Buffer.end(), data.begin(), data.end());
return *this;
DataBuffer& operator<<(DataBuffer& a_Buffer, const DataBuffer& data) {
a_Buffer.insert(a_Buffer.end(), data.begin(), data.end());
return a_Buffer;
}
DataBuffer& DataBuffer::operator>>(std::string& str) {
std::size_t stringSize = strlen(reinterpret_cast<const char*>(m_Buffer.data()) + m_ReadOffset) + 1; // including null character
DataBuffer& operator>>(DataBuffer& a_Buffer, std::string& str) {
std::size_t stringSize = strlen(reinterpret_cast<const char*>(a_Buffer.data()) + a_Buffer.GetReadOffset()) + 1; // including null character
str.resize(stringSize);
std::copy(m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset),
m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset + stringSize), str.begin());
m_ReadOffset += stringSize;
std::copy(a_Buffer.begin() + static_cast<DataBuffer::difference_type>(a_Buffer.GetReadOffset()),
a_Buffer.begin() + static_cast<DataBuffer::difference_type>(a_Buffer.GetReadOffset() + stringSize), str.begin());
a_Buffer.SetReadOffset(a_Buffer.GetReadOffset() + stringSize);
str.resize(stringSize - 1);
return *this;
return a_Buffer;
}
DataBuffer& DataBuffer::operator>>(DataBuffer& data) {
data.Resize(GetSize() - m_ReadOffset);
std::copy(m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset), m_Buffer.end(), data.begin());
m_ReadOffset = m_Buffer.size();
return *this;
DataBuffer& operator>>(DataBuffer& a_Buffer, DataBuffer& data) {
data.Resize(a_Buffer.GetSize() - a_Buffer.GetReadOffset());
std::copy(a_Buffer.begin() + static_cast<DataBuffer::difference_type>(a_Buffer.GetReadOffset()), a_Buffer.end(), data.begin());
a_Buffer.SetReadOffset(a_Buffer.GetSize());
return a_Buffer;
}
void DataBuffer::WriteSome(const char* buffer, std::size_t amount) {
@@ -147,4 +147,8 @@ void DataBuffer::WriteFile(const std::string& fileName) const {
file.flush();
}
void DataBuffer::insert(iterator a_DestBegin, const_iterator a_SrcBegin, const_iterator a_SrcEnd) {
m_Buffer.insert(a_DestBegin, a_SrcBegin, a_SrcEnd);
}
} // namespace sp

View File

@@ -1,6 +1,7 @@
#include <sp/common/VarInt.h>
#include <sp/common/DataBuffer.h>
#include <sp/common/DataBufferOperators.h>
#include <stdexcept>
namespace sp {

View File

@@ -24,11 +24,13 @@ using Message = sp::ConcreteMessage<TData, PacketBase, ID>;
struct KeepAlivePacket {
sp::BitField<std::uint16_t, 12> one;
sp::BitField<std::uint16_t, 4> two;
std::shared_ptr<std::string> test;
};
struct MDCPacket {
sp::BitField<std::uint16_t, 12> one;
sp::BitField<PacketID, 4> two;
std::unique_ptr<std::string> test;
};
using KeepAliveMessage = Message<KeepAlivePacket, PacketID::KeepAlive>;
@@ -41,10 +43,10 @@ class PacketHandler : public sp::GenericHandler<AllMessages> {};
class MyHandler : public PacketHandler {
public:
virtual void Handle(const KeepAliveMessage& msg) override {
std::cout << "I recieved a keep alive : " << *msg->one << " : " << *msg->two << "\n";
std::cout << "I recieved a keep alive : " << *msg->one << " : " << *msg->two << " : " << (msg->test ? *msg->test : "nullptr") << "\n";
}
virtual void Handle(const MDCMessage& msg) override {
std::cout << "I recieved a mdc : " << *msg->one << " : " << static_cast<unsigned>(*msg->two) << "\n";
std::cout << "I recieved a mdc : " << *msg->one << " : " << static_cast<unsigned>(*msg->two) << " : " << *msg->test << "\n";
}
};
@@ -55,7 +57,7 @@ using PacketFactory = sp::MessageFactory<PacketBase, AllMessages>;
using PacketStream = sp::MessageStream<PacketFactory>;
int main() {
KeepAliveMessage m{69, 5};
KeepAliveMessage m{69, 5, std::make_shared<std::string>("I'm a keepalive")};
// dispatch tests
@@ -69,7 +71,6 @@ int main() {
d.Dispatch(*message);
// write tests
auto compress = std::make_shared<sp::ZlibCompress>();
@@ -79,7 +80,7 @@ int main() {
PacketStream p(std::make_shared<sp::StdOuput>(file));
p.WriteMessage(m);
p.WriteMessage(MDCMessage{42, PacketID::MDC});
p.WriteMessage(MDCMessage{42, PacketID::MDC, std::make_unique<std::string>("Coucou")});
file.flush();