6 Commits

Author SHA1 Message Date
8a5286d0ce move ArrayFiller in its own file 2025-02-25 20:30:26 +01:00
a194774925 add MessageDispatcher 2025-02-25 20:29:59 +01:00
8f32b09b17 add extensions include 2025-02-25 18:29:55 +01:00
60bb4ea06e begin compression module 2025-02-25 14:06:56 +01:00
2acbd76c5a byteswapping: move declaration into source file 2025-02-23 21:57:10 +01:00
468f5ce8a0 add const 2025-02-23 13:20:45 +01:00
14 changed files with 424 additions and 102 deletions

View File

@@ -1,26 +1,10 @@
#pragma once #pragma once
#ifdef _WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <cstdint> #include <cstdint>
namespace sp { namespace sp {
template <typename T> bool IsSystemBigEndian();
void SwapBytes(T& value) {
char* ptr = reinterpret_cast<char*>(&value);
std::reverse(ptr, ptr + sizeof(T));
}
bool IsSystemBigEndian() {
static constexpr std::uint16_t test = 10;
static const bool isBigEndian = reinterpret_cast<const std::uint8_t*>(&test)[1] == 10;
return isBigEndian;
}
/** /**
* \brief Serialize value to (network byte order) big endian * \brief Serialize value to (network byte order) big endian
@@ -29,21 +13,13 @@ template <typename T>
void ToNetwork(T& value) {} void ToNetwork(T& value) {}
template <> template <>
void ToNetwork<std::uint16_t>(std::uint16_t& value) { void ToNetwork<std::uint16_t>(std::uint16_t& value);
value = htons(value);
}
template <> template <>
void ToNetwork<std::uint32_t>(std::uint32_t& value) { void ToNetwork<std::uint32_t>(std::uint32_t& value);
value = htonl(value);
}
template <> template <>
void ToNetwork<std::uint64_t>(std::uint64_t& value) { void ToNetwork<std::uint64_t>(std::uint64_t& value);
if (IsSystemBigEndian())
return;
SwapBytes(value);
}
/** /**
* \brief Deserialize value from (network byte order) big endian * \brief Deserialize value from (network byte order) big endian
@@ -52,21 +28,13 @@ template <typename T>
void FromNetwork(T& value) {} void FromNetwork(T& value) {}
template <> template <>
void FromNetwork<std::uint16_t>(std::uint16_t& value) { void FromNetwork<std::uint16_t>(std::uint16_t& value);
value = ntohs(value);
}
template <> template <>
void FromNetwork<std::uint32_t>(std::uint32_t& value) { void FromNetwork<std::uint32_t>(std::uint32_t& value);
value = ntohl(value);
}
template <> template <>
void FromNetwork<std::uint64_t>(std::uint64_t& value) { void FromNetwork<std::uint64_t>(std::uint64_t& value);
if (IsSystemBigEndian())
return;
SwapBytes(value);
}
/** /**
* \brief Swap bytes if the value is any kind of integer * \brief Swap bytes if the value is any kind of integer
@@ -75,18 +43,12 @@ template <typename T>
void TrySwapBytes(T& value) {} void TrySwapBytes(T& value) {}
template <> template <>
void TrySwapBytes<std::uint16_t>(std::uint16_t& value) { void TrySwapBytes<std::uint16_t>(std::uint16_t& value);
SwapBytes(value);
}
template <> template <>
void TrySwapBytes<std::uint32_t>(std::uint32_t& value) { void TrySwapBytes<std::uint32_t>(std::uint32_t& value);
SwapBytes(value);
}
template <> template <>
void TrySwapBytes<std::uint64_t>(std::uint64_t& value) { void TrySwapBytes<std::uint64_t>(std::uint64_t& value);
SwapBytes(value);
}
} // namespace sp } // namespace sp

View File

@@ -0,0 +1,15 @@
#pragma once
#include <sp/default/DefaultPacket.h>
#include <sp/default/DefaultPacketHandler.h>
#include <sp/protocol/MessageDispatcher.h>
namespace sp {
using PacketDispatcher = MessageDispatcher<
PacketMessage::ParsedOptions::MsgIdType,
PacketMessage,
PacketMessage::ParsedOptions::HandlerType::HandlerT
>;
} // namespace sp

View File

@@ -0,0 +1,37 @@
#pragma once
/**
* \file Compression.h
* \brief File containing compress utilities
*/
#include <cstdint>
#include <sp/common/DataBuffer.h>
namespace sp {
namespace zlib {
/**
* \brief Compress some data
* \param buffer the data to compress
* \return the compressed data
*/
DataBuffer Compress(const DataBuffer& buffer);
/**
* \brief Reads the packet lenght and uncompress it
* \param buffer the data to uncompress
* \return the uncompressed data
*/
DataBuffer Decompress(DataBuffer& buffer);
/**
* \brief Uncompress some data
* \param buffer the data to uncompress
* \param packetLength lenght of data
* \return the uncompressed data
*/
DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength);
} // namespace zlib
} // namespace sp

View File

@@ -0,0 +1,5 @@
#pragma once
#if __has_include(<sp/extensions/Compress.h>)
#include <sp/extensions/Compress.h>
#endif

View File

@@ -0,0 +1,54 @@
#pragma once
/**
* \file MessageDispatcher.h
* \brief File containing the sp::MessageDispatcher class
*/
#include <map>
namespace sp {
/**
* \class MessageDispatcher
* \brief Class used to dispatch messages
*/
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
class MessageDispatcher {
private:
std::map<MessageIdType, std::vector<std::shared_ptr<MessageHandler>>> m_Handlers;
public:
/**
* \brief Constructor
*/
MessageDispatcher() {}
/**
* \brief Dispatch a packet
* \param packet The packet to dispatch
*/
void Dispatch(const MessageBase& a_Message);
/**
* \brief Register a packet handler
* \param type The packet type
* \param handler The packet handler
*/
void RegisterHandler(MessageIdType a_MessageType, const std::shared_ptr<MessageHandler>& a_Handler);
/**
* \brief Unregister a packet handler
* \param type The packet type
* \param handler The packet handler
*/
void UnregisterHandler(MessageIdType a_MessageType, const std::shared_ptr<MessageHandler>& a_Handler);
/**
* \brief Unregister a packet handler
* \param handler The packet handler
*/
void UnregisterHandler(const std::shared_ptr<MessageHandler>& a_Handler);
};
#include <sp/protocol/message/MessageDispatcherImpl.inl>
} // namespace blitz

View File

@@ -7,42 +7,7 @@
namespace sp { namespace sp {
namespace details { #include <sp/protocol/message/ArrayFillerImpl.inl>
template <typename TBase>
using ArrayType = std::vector<std::function<std::unique_ptr<TBase>(void)>>;
template <typename TBase, typename... TMessages>
struct ArrayFiller {};
template <typename TBase, typename... TMessages>
struct ArrayFiller<TBase, std::tuple<TMessages...>> {
static ArrayType<TBase> ArrayCreate() {
ArrayType<TBase> array;
array.reserve(sizeof...(TMessages));
ArrayFiller<TBase, TMessages...>::ArrayAppend(array);
return array;
}
};
template <typename TBase, typename TMessage, typename... TMessages>
struct ArrayFiller<TBase, TMessage, TMessages...> {
static void ArrayAppend(details::ArrayType<TBase>& array) {
ArrayFiller<TBase, TMessage>::ArrayAppend(array);
ArrayFiller<TBase, TMessages...>::ArrayAppend(array);
}
};
template <typename TBase, typename TMessage>
struct ArrayFiller<TBase, TMessage> {
static void ArrayAppend(details::ArrayType<TBase>& array) {
array.push_back([]() -> std::unique_ptr<TBase> { return std::make_unique<TMessage>(); });
}
};
} // namespace details
template <typename TBase, typename TTMessages> template <typename TBase, typename TTMessages>
class MessageFactory { class MessageFactory {

View File

@@ -0,0 +1,38 @@
#pragma once
namespace details {
template <typename TBase>
using ArrayType = std::vector<std::function<std::unique_ptr<TBase>(void)>>;
template <typename TBase, typename... TMessages>
struct ArrayFiller {};
template <typename TBase, typename... TMessages>
struct ArrayFiller<TBase, std::tuple<TMessages...>> {
static ArrayType<TBase> ArrayCreate() {
ArrayType<TBase> array;
array.reserve(sizeof...(TMessages));
ArrayFiller<TBase, TMessages...>::ArrayAppend(array);
return array;
}
};
template <typename TBase, typename TMessage, typename... TMessages>
struct ArrayFiller<TBase, TMessage, TMessages...> {
static void ArrayAppend(details::ArrayType<TBase>& array) {
ArrayFiller<TBase, TMessage>::ArrayAppend(array);
ArrayFiller<TBase, TMessages...>::ArrayAppend(array);
}
};
template <typename TBase, typename TMessage>
struct ArrayFiller<TBase, TMessage> {
static void ArrayAppend(details::ArrayType<TBase>& array) {
array.push_back([]() -> std::unique_ptr<TBase> { return std::make_unique<TMessage>(); });
}
};
} // namespace details

View File

@@ -0,0 +1,34 @@
#pragma once
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::RegisterHandler(MessageIdType a_MessageType, const std::shared_ptr<MessageHandler>& a_Handler) {
auto found = std::find(m_Handlers[a_MessageType].begin(), m_Handlers[a_MessageType].end(), a_Handler);
if (found == m_Handlers[a_MessageType].end())
m_Handlers[a_MessageType].push_back(a_Handler);
}
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::UnregisterHandler(MessageIdType a_MessageType, const std::shared_ptr<MessageHandler>& a_Handler) {
auto found = std::find(m_Handlers[a_MessageType].begin(), m_Handlers[a_MessageType].end(), a_Handler);
if (found != m_Handlers[a_MessageType].end())
m_Handlers[a_MessageType].erase(found);
}
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::UnregisterHandler(const std::shared_ptr<MessageHandler>& a_Handler) {
for (auto& pair : m_Handlers) {
if (pair.second.empty())
continue;
MessageIdType type = pair.first;
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), a_Handler), m_Handlers[type].end());
}
}
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::Dispatch(const MessageBase& a_Message) {
MessageIdType type = a_Message.GetId();
for (auto& handler : m_Handlers[type])
a_Message.Dispatch(*handler);
}

View File

@@ -30,7 +30,7 @@ class MessageInterfaceBigEndian : public TBase {
} }
template <typename T> template <typename T>
void WriteData(T value, DataBuffer& buffer) { void WriteData(T value, DataBuffer& buffer) const {
ToNetwork(value); ToNetwork(value);
buffer << value; buffer << value;
} }
@@ -71,12 +71,12 @@ class MessageInterfaceReadBase : public TBase {
template <typename TBase> template <typename TBase>
class MessageInterfaceWriteBase : public TBase { class MessageInterfaceWriteBase : public TBase {
public: public:
void Write(DataBuffer& buffer) { void Write(DataBuffer& buffer) const {
WriteImpl(buffer); WriteImpl(buffer);
} }
protected: protected:
virtual void WriteImpl(DataBuffer& buffer) = 0; virtual void WriteImpl(DataBuffer& buffer) const = 0;
}; };
// Handler functionality chunk // Handler functionality chunk
@@ -85,12 +85,12 @@ class MessageInterfaceHandlerBase : public TBase {
public: public:
using HandlerType = typename THandler::HandlerT; using HandlerType = typename THandler::HandlerT;
void Dispatch(HandlerType& handler) { void Dispatch(HandlerType& handler) const {
DispatchImpl(handler); DispatchImpl(handler);
} }
protected: protected:
virtual void DispatchImpl(HandlerType& handler) = 0; virtual void DispatchImpl(HandlerType& handler) const = 0;
}; };
// Validity functionality chunk // Validity functionality chunk
@@ -109,7 +109,7 @@ class MessageInterfaceValidityBase : public TBase {
template <typename TBase> template <typename TBase>
class MessageInterfaceWriteIdBase : public TBase { class MessageInterfaceWriteIdBase : public TBase {
public: public:
void Write(DataBuffer& buffer) { void Write(DataBuffer& buffer) const {
this->WriteData(this->GetId(), buffer); this->WriteData(this->GetId(), buffer);
this->WriteImpl(buffer); this->WriteImpl(buffer);
} }

View File

@@ -26,8 +26,8 @@ class MessageImplDispatchBase : public TBase {
using Handler = typename TBase::HandlerType; using Handler = typename TBase::HandlerType;
protected: protected:
virtual void DispatchImpl(Handler& handler) override { virtual void DispatchImpl(Handler& handler) const override {
handler.Handle(static_cast<TActual&>(*this)); handler.Handle(static_cast<const TActual&>(*this));
} }
}; };
@@ -113,13 +113,13 @@ class MessageImplFieldsWriteBase : public TBase {
private: private:
// normal writing // normal writing
template <typename TField> template <typename TField>
void WriteField(Field<TField, 0>& field, DataBuffer& buffer) { void WriteField(const Field<TField, 0>& field, DataBuffer& buffer) const {
this->WriteData(field.GetValue(), buffer); this->WriteData(field.GetValue(), buffer);
} }
// writing field in bitfield // writing field in bitfield
template <typename TFieldType, typename TField, int IAlignment> template <typename TFieldType, typename TField, int IAlignment>
void WriteField(Field<TField, IAlignment>& field, TFieldType& data, std::size_t offset) { void WriteField(const Field<TField, IAlignment>& field, TFieldType& data, std::size_t offset) const {
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8; static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
// we suppose that the first element is at the highest bits // we suppose that the first element is at the highest bits
data |= (field.GetValue() & ((1 << IAlignment) - 1)) << TotalBitCount - IAlignment - offset; data |= (field.GetValue() & ((1 << IAlignment) - 1)) << TotalBitCount - IAlignment - offset;
@@ -127,7 +127,7 @@ class MessageImplFieldsWriteBase : public TBase {
// writing bitfield // writing bitfield
template <typename TContainer, typename TFirst, typename... TFields> template <typename TContainer, typename TFirst, typename... TFields>
void WriteField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) { void WriteField(const Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) const {
TContainer data = 0; TContainer data = 0;
std::size_t offset = 0; std::size_t offset = 0;
TupleForEach( TupleForEach(
@@ -139,9 +139,9 @@ class MessageImplFieldsWriteBase : public TBase {
this->WriteData(data, buffer); this->WriteData(data, buffer);
} }
void WriteImpl(DataBuffer& buffer) override { void WriteImpl(DataBuffer& buffer) const override {
auto& allFields = this->GetFields(); auto& allFields = this->GetFields();
TupleForEach([&buffer, this](auto& field) { this->WriteField(field, buffer); }, allFields); TupleForEach([&buffer, this](const auto& field) { this->WriteField(field, buffer); }, allFields);
} }
}; };

View File

@@ -0,0 +1,74 @@
#include <sp/common/ByteSwapping.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <algorithm>
namespace sp {
template <typename T>
void SwapBytes(T& value) {
char* ptr = reinterpret_cast<char*>(&value);
std::reverse(ptr, ptr + sizeof(T));
}
bool IsSystemBigEndian() {
static constexpr std::uint16_t test = 10;
static const bool isBigEndian = reinterpret_cast<const std::uint8_t*>(&test)[1] == 10;
return isBigEndian;
}
template <>
void ToNetwork<std::uint16_t>(std::uint16_t& value) {
value = htons(value);
}
template <>
void ToNetwork<std::uint32_t>(std::uint32_t& value) {
value = htonl(value);
}
template <>
void ToNetwork<std::uint64_t>(std::uint64_t& value) {
if (IsSystemBigEndian())
return;
SwapBytes(value);
}
template <>
void FromNetwork<std::uint16_t>(std::uint16_t& value) {
value = ntohs(value);
}
template <>
void FromNetwork<std::uint32_t>(std::uint32_t& value) {
value = ntohl(value);
}
template <>
void FromNetwork<std::uint64_t>(std::uint64_t& value) {
if (IsSystemBigEndian())
return;
SwapBytes(value);
}
template <>
void TrySwapBytes<std::uint16_t>(std::uint16_t& value) {
SwapBytes(value);
}
template <>
void TrySwapBytes<std::uint32_t>(std::uint32_t& value) {
SwapBytes(value);
}
template <>
void TrySwapBytes<std::uint64_t>(std::uint64_t& value) {
SwapBytes(value);
}
} // namespace sp

View File

@@ -0,0 +1,86 @@
#include <sp/extensions/Compress.h>
#include <cassert>
#include <sp/common/VarInt.h>
#include <zlib.h>
#define COMPRESSION_THRESHOLD 64
namespace sp {
namespace zlib {
static DataBuffer Inflate(const std::uint8_t* source, std::size_t size, std::size_t uncompressedSize) {
DataBuffer result;
result.Resize(uncompressedSize);
uncompress(reinterpret_cast<Bytef*>(result.data()), reinterpret_cast<uLongf*>(&uncompressedSize),
reinterpret_cast<const Bytef*>(source), static_cast<uLong>(size));
assert(result.GetSize() == uncompressedSize);
return result;
}
static DataBuffer Deflate(const std::uint8_t* source, std::size_t size) {
DataBuffer result;
uLongf compressedSize = size;
result.Resize(size); // Resize for the compressed data to fit into
compress(
reinterpret_cast<Bytef*>(result.data()), &compressedSize, reinterpret_cast<const Bytef*>(source), static_cast<uLong>(size));
result.Resize(compressedSize); // Resize to cut useless data
return result;
}
DataBuffer Compress(const DataBuffer& buffer) {
DataBuffer packet;
if (buffer.GetSize() < COMPRESSION_THRESHOLD) {
// Don't compress since it's a small packet
VarInt compressedDataLength = 0;
std::uint64_t packetLength = compressedDataLength.GetSerializedLength() + buffer.GetSize();
packet << packetLength;
packet << compressedDataLength;
packet << buffer;
return packet;
}
DataBuffer compressedData = Deflate(buffer.data(), buffer.GetSize());
VarInt uncompressedDataLength = buffer.GetSize();
std::uint64_t packetLength = uncompressedDataLength.GetSerializedLength() + compressedData.GetSize();
packet << packetLength;
packet << uncompressedDataLength;
packet.WriteSome(compressedData.data(), compressedData.GetSize());
return packet;
}
DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) {
VarInt uncompressedLength;
buffer >> uncompressedLength;
std::uint64_t compressedLength = packetLength - uncompressedLength.GetSerializedLength();
if (uncompressedLength.GetValue() == 0) {
// Data already uncompressed. Nothing to do
DataBuffer ret;
buffer.ReadSome(ret, compressedLength);
return ret;
}
assert(buffer.GetReadOffset() + compressedLength <= buffer.GetSize());
return Inflate(buffer.data() + buffer.GetReadOffset(), compressedLength, uncompressedLength.GetValue());
}
DataBuffer Decompress(DataBuffer& buffer) {
std::uint64_t packetLength;
buffer >> packetLength;
return Decompress(buffer, packetLength);
}
} // namespace zlib
} // namespace sp

View File

@@ -3,6 +3,9 @@
#include <examples/PacketExample.h> #include <examples/PacketExample.h>
#include <memory> #include <memory>
#include <sp/extensions/Extensions.h>
#include <sp/default/DefaultPacketDispatcher.h>
class KeepAliveHandler : public sp::PacketHandler { class KeepAliveHandler : public sp::PacketHandler {
void Handle(const KeepAlivePacket& packet) { void Handle(const KeepAlivePacket& packet) {
std::cout << "KeepAlive handled !\n"; std::cout << "KeepAlive handled !\n";
@@ -22,8 +25,8 @@ int main() {
sp::PacketMessage* msg = upgradeTower.get(); sp::PacketMessage* msg = upgradeTower.get();
KeepAliveHandler handler; auto handler = std::make_shared<KeepAliveHandler>();
msg->Dispatch(handler); msg->Dispatch(*handler);
sp::DataBuffer buffer; sp::DataBuffer buffer;
msg->Write(buffer); msg->Write(buffer);
@@ -43,7 +46,13 @@ int main() {
return 1; return 1;
} }
std::cout << (unsigned)packet->GetId() << std::endl; std::cout << (unsigned)packet->GetId() << std::endl;
packet->Dispatch(handler); packet->Dispatch(*handler);
sp::PacketDispatcher dispatcher;
dispatcher.RegisterHandler(PacketId::KeepAlive, handler);
dispatcher.Dispatch(*packet);
dispatcher.UnregisterHandler(PacketId::KeepAlive, handler);
dispatcher.UnregisterHandler(handler);
return 0; return 0;
} }

View File

@@ -2,11 +2,54 @@ add_rules("mode.debug", "mode.release")
set_languages("c++17") set_languages("c++17")
local modules = {
Compression = {
Option = "zlib",
Deps = {"zlib"},
Packages = {"zlib"},
Includes = {"include/(sp/extensions/Compress.h)"},
Sources = {"src/sp/extensions/Compress.cpp"}
}
}
-- Map modules to options
for name, module in table.orderpairs(modules) do
if module.Option then
option(module.Option, { description = "Enables the " .. name .. " module", default = true, category = "Modules" })
end
end
-- Add modules requirements
for name, module in table.orderpairs(modules) do
if module.Deps then
add_requires(module.Deps)
end
end
-- Add modules targets
for name, module in table.orderpairs(modules) do
if module.Deps and has_config(module.Option) then
target("SimpleProtocolLib-" .. name)
add_includedirs("include")
for _, include in table.orderpairs(module.Includes) do
add_headerfiles(include)
end
for _, source in table.orderpairs(module.Sources) do
add_files(source)
end
for _, package in table.orderpairs(module.Packages) do
add_packages(package)
end
set_group("Library")
set_kind("$(kind)")
end
end
target("SimpleProtocolLib") target("SimpleProtocolLib")
add_includedirs("include") add_includedirs("include")
add_headerfiles("include/(sp/**.h)") add_headerfiles("include/(sp/common/**.h)", "include/(sp/common/**.h)", "include/(sp/common/**.h)")
set_group("Library") set_group("Library")
add_files("src/sp/**.cpp") add_files("src/sp/common/*.cpp")
set_kind("$(kind)") set_kind("$(kind)")
-- Tests -- Tests