Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
9374332cd2
|
|||
|
bce37f59df
|
|||
|
a1a4176801
|
@@ -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>
|
||||
|
||||
@@ -82,44 +84,6 @@ class DataBuffer {
|
||||
*/
|
||||
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
|
||||
@@ -154,52 +118,6 @@ class DataBuffer {
|
||||
*/
|
||||
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
|
||||
@@ -345,3 +263,5 @@ class DataBuffer {
|
||||
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
|
||||
|
||||
} // namespace sp
|
||||
|
||||
#include "DataBuffer.inl"
|
||||
201
include/sp/common/DataBuffer.inl
Normal file
201
include/sp/common/DataBuffer.inl
Normal 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
|
||||
83
include/sp/io/SerializableMessage.h
Normal file
83
include/sp/io/SerializableMessage.h
Normal 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
|
||||
@@ -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() {}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -79,7 +81,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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user