2 Commits

Author SHA1 Message Date
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
5 changed files with 260 additions and 90 deletions

View File

@@ -10,9 +10,11 @@
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <list>
#include <map> #include <map>
#include <sp/common/VarInt.h> #include <memory>
#include <sp/common/ByteSwapping.h> #include <sp/common/ByteSwapping.h>
#include <sp/common/VarInt.h>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -82,44 +84,6 @@ class DataBuffer {
*/ */
DataBuffer& operator<<(const DataBuffer& data); 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 * \brief Read data into a_Data
* \warning No endian checks * \warning No endian checks
@@ -154,52 +118,6 @@ class DataBuffer {
*/ */
DataBuffer& operator>>(std::string& str); 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 * \brief Write some data to the buffer
* \param buffer The buffer to write * \param buffer The buffer to write
@@ -345,3 +263,5 @@ class DataBuffer {
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer); std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
} // namespace sp } // namespace sp
#include "DataBuffer.inl"

View File

@@ -0,0 +1,201 @@
#pragma once
namespace sp {
/**
* \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& buffer, const std::shared_ptr<T>& data) {
return 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& buffer, const std::unique_ptr<T>& data) {
return 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& buffer, const std::vector<T>& data) {
buffer << VarInt{data.size()};
for (const auto& element : data) {
buffer << element;
}
return 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& buffer, const std::list<T>& data) {
buffer << VarInt{data.size()};
for (const auto& element : data) {
buffer << element;
}
return 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& buffer, const std::map<K, V>& data) {
buffer << VarInt{data.size()};
for (const auto& [key, value] : data) {
buffer << key << value;
}
return 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& buffer, const std::unordered_map<K, V>& data) {
buffer << VarInt{data.size()};
for (const auto& [key, value] : data) {
buffer << key << value;
}
return buffer;
}
/**
* \brief Append a pair to the buffer
* \param data The pair to append
*/
template <typename K, typename V>
DataBuffer& operator<<(DataBuffer& buffer, const std::pair<K, V>& data) {
return 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& buffer, const std::array<T, Size>& data) {
for (const auto& element : data) {
buffer << element;
}
return buffer;
}
/**
* \brief Read a pointer
*/
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
DataBuffer& operator>>(DataBuffer& buffer, std::shared_ptr<T>& data) {
data = std::make_shared<T>();
return buffer >> *data;
}
/**
* \brief Read a pointer
*/
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
DataBuffer& operator>>(DataBuffer& buffer, std::unique_ptr<T>& data) {
data = std::make_unique<T>();
return 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& buffer, std::vector<T>& data) {
VarInt arraySize;
buffer >> arraySize;
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
T newElement;
buffer >> newElement;
data.push_back(newElement);
}
return buffer;
}
/**
* \brief Read a list (size + data) from the buffer
* \pre The list is assumed to be empty
*/
template <typename T>
DataBuffer& operator>>(DataBuffer& buffer, std::list<T>& data) {
VarInt arraySize;
buffer >> arraySize;
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
T newElement;
buffer >> newElement;
data.push_back(newElement);
}
return 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& buffer, std::map<K, V>& data) {
VarInt mapSize;
buffer >> mapSize;
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
K newKey;
V newValue;
buffer >> newKey >> newValue;
data.emplace(newKey, newValue);
}
return 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& buffer, std::unordered_map<K, V>& data) {
VarInt mapSize;
buffer >> mapSize;
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
K newKey;
V newValue;
buffer >> newKey >> newValue;
data.emplace(newKey, newValue);
}
return buffer;
}
/**
* \brief Read a pair
*/
template <typename K, typename V>
DataBuffer& operator>>(DataBuffer& buffer, std::pair<K, V>& data) {
return buffer >> data.first >> data.second;
}
/**
* \brief Read an array from the buffer
*/
template <std::size_t Size, typename T>
DataBuffer& operator>>(DataBuffer& buffer, std::array<T, Size>& data) {
for (std::size_t i = 0; i < Size; i++) {
T newElement;
buffer >> newElement;
data[i] = newElement;
}
return buffer;
}
} // namespace sp

View File

@@ -0,0 +1,47 @@
#pragma once
#include <sp/protocol/MessageFactory.h>
namespace sp {
template <typename TMessageID, typename THandler>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::unique_ptr<MessageBase<TMessageID, THandler>>& a_Message) {
return a_Buffer << VarInt{static_cast<std::uint64_t>(a_Message->GetId())} << a_Message->Write();
}
template <typename TMessageID, typename THandler>
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::shared_ptr<MessageBase<TMessageID, THandler>>& a_Message) {
return a_Buffer << VarInt{static_cast<std::uint64_t>(a_Message->GetId())} << a_Message->Write();
}
template <typename TMessageID, typename THandler, typename TTMessages>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::unique_ptr<MessageBase<TMessageID, THandler>>& a_Message) {
using TBase = MessageBase<TMessageID, THandler>;
using MsgId = typename TBase::MessageIdType;
static MessageFactory<TBase, TTMessages> factory;
VarInt msgId;
a_Buffer >> msgId;
a_Message = factory.CreateMessage(MsgId(msgId.GetValue()));
a_Message->Read(a_Buffer);
return a_Buffer;
}
template <typename TMessageID, typename THandler, typename TTMessages>
DataBuffer& operator>>(DataBuffer& a_Buffer, std::shared_ptr<MessageBase<TMessageID, THandler>>& a_Message) {
using TBase = MessageBase<TMessageID, THandler>;
using MsgId = typename TBase::MessageIdType;
static MessageFactory<TBase, TTMessages> factory;
VarInt msgId;
a_Buffer >> msgId;
a_Message = std::shared_ptr<TBase>(factory.CreateMessage(MsgId(msgId.GetValue())).release());
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; using HandlerType = typename MessageBase::HandlerType;
template <typename... T> template <typename... T>
ConcreteMessage(const T&... args) : m_Data{args...} {} ConcreteMessage(T&&... args) : m_Data{std::move(args)...} {}
virtual ~ConcreteMessage() {} virtual ~ConcreteMessage() {}

View File

@@ -24,11 +24,13 @@ using Message = sp::ConcreteMessage<TData, PacketBase, ID>;
struct KeepAlivePacket { struct KeepAlivePacket {
sp::BitField<std::uint16_t, 12> one; sp::BitField<std::uint16_t, 12> one;
sp::BitField<std::uint16_t, 4> two; sp::BitField<std::uint16_t, 4> two;
std::shared_ptr<std::string> test;
}; };
struct MDCPacket { struct MDCPacket {
sp::BitField<std::uint16_t, 12> one; sp::BitField<std::uint16_t, 12> one;
sp::BitField<PacketID, 4> two; sp::BitField<PacketID, 4> two;
std::unique_ptr<std::string> test;
}; };
using KeepAliveMessage = Message<KeepAlivePacket, PacketID::KeepAlive>; using KeepAliveMessage = Message<KeepAlivePacket, PacketID::KeepAlive>;
@@ -41,10 +43,10 @@ class PacketHandler : public sp::GenericHandler<AllMessages> {};
class MyHandler : public PacketHandler { class MyHandler : public PacketHandler {
public: public:
virtual void Handle(const KeepAliveMessage& msg) override { 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 { 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>; using PacketStream = sp::MessageStream<PacketFactory>;
int main() { int main() {
KeepAliveMessage m{69, 5}; KeepAliveMessage m{69, 5, std::make_shared<std::string>("I'm a keepalive")};
// dispatch tests // dispatch tests
@@ -79,7 +81,7 @@ int main() {
PacketStream p(std::make_shared<sp::StdOuput>(file)); PacketStream p(std::make_shared<sp::StdOuput>(file));
p.WriteMessage(m); p.WriteMessage(m);
p.WriteMessage(MDCMessage{42, PacketID::MDC}); p.WriteMessage(MDCMessage{42, PacketID::MDC, std::make_unique<std::string>("Coucou")});
file.flush(); file.flush();