Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b482420d3a | |||
| 61d0ce3a47 | |||
| 5beb5e92a7 | |||
| 59aaf03421 | |||
| 03d799e064 | |||
| 8a5286d0ce | |||
| a194774925 | |||
| 8f32b09b17 | |||
| 60bb4ea06e | |||
| 2acbd76c5a | |||
| 468f5ce8a0 |
31
.github/workflows/ubuntu.yml
vendored
Normal file
31
.github/workflows/ubuntu.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
name: Linux arm64
|
||||||
|
run-name: Build And Test
|
||||||
|
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
|
||||||
|
env:
|
||||||
|
XMAKE_ROOT: y
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Check out repository code
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare XMake
|
||||||
|
uses: xmake-io/github-action-setup-xmake@v1
|
||||||
|
with:
|
||||||
|
xmake-version: latest
|
||||||
|
actions-cache-folder: '.xmake-cache'
|
||||||
|
actions-cache-key: 'ubuntu'
|
||||||
|
|
||||||
|
- name: XMake config
|
||||||
|
run: xmake f -p linux -y
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: xmake
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: xmake test
|
||||||
@@ -6,12 +6,13 @@ enum PacketId {
|
|||||||
UpgradeTower,
|
UpgradeTower,
|
||||||
};
|
};
|
||||||
|
|
||||||
#include <examples/KeepAlivePacket.h>
|
|
||||||
#include <examples/DisconnectPacket.h>
|
#include <examples/DisconnectPacket.h>
|
||||||
|
#include <examples/KeepAlivePacket.h>
|
||||||
#include <examples/UpgradeTowerPacket.h>
|
#include <examples/UpgradeTowerPacket.h>
|
||||||
|
|
||||||
// they must be in the same order as in the enum !
|
// they must be in the same order as in the enum !
|
||||||
using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket, UpgradeTowerPacket>;
|
using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket, UpgradeTowerPacket>;
|
||||||
|
|
||||||
#include <sp/default/DefaultPacketHandler.h>
|
#include <sp/default/DefaultPacketDispatcher.h>
|
||||||
#include <sp/default/DefaultPacketFactory.h>
|
#include <sp/default/DefaultPacketFactory.h>
|
||||||
|
#include <sp/default/DefaultPacketHandler.h>
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ class DataBuffer {
|
|||||||
K newKey;
|
K newKey;
|
||||||
V newValue;
|
V newValue;
|
||||||
*this >> newKey >> newValue;
|
*this >> newKey >> newValue;
|
||||||
data.insert({newKey, newValue});
|
data.emplace(newKey, newValue);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ class VarInt {
|
|||||||
std::uint64_t m_Value;
|
std::uint64_t m_Value;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static const std::uint64_t MAX_VALUE = static_cast<std::uint64_t>(-1) >> 8;
|
||||||
|
|
||||||
VarInt() : m_Value(0) {}
|
VarInt() : m_Value(0) {}
|
||||||
/**
|
/**
|
||||||
* \brief Construct a variable integer from a value
|
* \brief Construct a variable integer from a value
|
||||||
|
|||||||
15
include/sp/default/DefaultPacketDispatcher.h
Normal file
15
include/sp/default/DefaultPacketDispatcher.h
Normal 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
|
||||||
54
include/sp/extensions/Compress.h
Normal file
54
include/sp/extensions/Compress.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file Compression.h
|
||||||
|
* \brief File containing compress utilities
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <sp/common/DataBuffer.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace option {
|
||||||
|
|
||||||
|
struct ZlibCompress {
|
||||||
|
bool m_Enabled = true;
|
||||||
|
std::size_t m_CompressionThreshold = 64;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace option
|
||||||
|
} // namespace sp
|
||||||
|
|
||||||
|
#include <sp/io/IOInterface.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace zlib {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Compress some data
|
||||||
|
* \param buffer the data to compress
|
||||||
|
* \return the compressed data
|
||||||
|
*/
|
||||||
|
DataBuffer Compress(const DataBuffer& buffer, std::size_t a_CompressionThreshold = 64);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \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 io {
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class MessageEncapsulator<option::ZlibCompress> {
|
||||||
|
public:
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const option::ZlibCompress& a_Option);
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const option::ZlibCompress& a_Option);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
41
include/sp/extensions/Encrypt.h
Normal file
41
include/sp/extensions/Encrypt.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file Encrypt.h
|
||||||
|
* \brief File containing encrypt utilities
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sp/common/DataBuffer.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace option {
|
||||||
|
|
||||||
|
struct TlsEncrypt {
|
||||||
|
bool m_Enabled = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace option
|
||||||
|
} // namespace sp
|
||||||
|
|
||||||
|
#include <sp/io/IOInterface.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace ssl {
|
||||||
|
|
||||||
|
// encrypt
|
||||||
|
|
||||||
|
// decrypt
|
||||||
|
|
||||||
|
} // namespace ssl
|
||||||
|
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class MessageEncapsulator<option::TlsEncrypt> {
|
||||||
|
public:
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const option::TlsEncrypt& a_Option);
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const option::TlsEncrypt& a_Option);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
5
include/sp/extensions/Extensions.h
Normal file
5
include/sp/extensions/Extensions.h
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __has_include(<sp/extensions/Compress.h>)
|
||||||
|
#include <sp/extensions/Compress.h>
|
||||||
|
#endif
|
||||||
33
include/sp/io/File.h
Normal file
33
include/sp/io/File.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <sp/io/IOInterface.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
struct FileTag {
|
||||||
|
enum OpenMode {
|
||||||
|
In = 1,
|
||||||
|
Out = 1 << 1,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class IOInterface<FileTag> {
|
||||||
|
private:
|
||||||
|
std::unique_ptr<std::ifstream> m_FileInput;
|
||||||
|
std::unique_ptr<std::ofstream> m_FileOutput;
|
||||||
|
|
||||||
|
public:
|
||||||
|
IOInterface(const std::string& a_FilePath, unsigned int a_OpenMode);
|
||||||
|
IOInterface(IOInterface&& other);
|
||||||
|
|
||||||
|
DataBuffer Read(std::size_t a_Amount);
|
||||||
|
void Write(const sp::DataBuffer& a_Data);
|
||||||
|
};
|
||||||
|
|
||||||
|
using File = IOInterface<FileTag>;
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
58
include/sp/io/IOInterface.h
Normal file
58
include/sp/io/IOInterface.h
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <sp/common/DataBuffer.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
template <typename IOTag>
|
||||||
|
class IOInterface {
|
||||||
|
public:
|
||||||
|
DataBuffer Read(std::size_t a_Amount);
|
||||||
|
void Write(const DataBuffer& a_Data);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TOption>
|
||||||
|
class MessageEncapsulator {
|
||||||
|
public:
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOption& a_Option);
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const TOption& a_Option);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
|
class Stream {
|
||||||
|
protected:
|
||||||
|
MessageDispatcher m_Dispatcher;
|
||||||
|
IOInterface<IOTag> m_Interface;
|
||||||
|
std::tuple<TOptions...> m_Options;
|
||||||
|
|
||||||
|
using MessageBase = typename MessageDispatcher::MessageBaseType;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Stream() {}
|
||||||
|
Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options);
|
||||||
|
Stream(Stream&& a_Stream);
|
||||||
|
|
||||||
|
void RecieveMessages();
|
||||||
|
void SendMessage(const MessageBase& a_Message);
|
||||||
|
|
||||||
|
|
||||||
|
template <typename TOption>
|
||||||
|
TOption& GetOption() {
|
||||||
|
return std::get<TOption>(m_Options);
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageDispatcher& GetDispatcher() {
|
||||||
|
return m_Dispatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOptions&... a_Options);
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const TOptions&... a_Options);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
|
|
||||||
|
#include <sp/io/IOInterfaceImpl.inl>
|
||||||
128
include/sp/io/IOInterfaceImpl.inl
Normal file
128
include/sp/io/IOInterfaceImpl.inl
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sp/extensions/Compress.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
template <typename... TOptions>
|
||||||
|
struct MessageEncapsulatorPack {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct MessageEncapsulatorPack<> {
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data) {
|
||||||
|
return a_Data;
|
||||||
|
}
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data) {
|
||||||
|
return a_Data;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TOption, typename... TOptions>
|
||||||
|
struct MessageEncapsulatorPack<TOption, TOptions...> {
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOption& a_Option, const TOptions&... a_Options) {
|
||||||
|
DataBuffer data = io::MessageEncapsulator<TOption>::Encapsulate(a_Data, a_Option);
|
||||||
|
return MessageEncapsulatorPack<TOptions...>::Encapsulate(data, a_Options...);
|
||||||
|
}
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data, const TOption& a_Option, const TOptions&... a_Options) {
|
||||||
|
DataBuffer data = io::MessageEncapsulator<TOption>::Decapsulate(a_Data, a_Option);
|
||||||
|
return MessageEncapsulatorPack<TOptions...>::Decapsulate(data, a_Options...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
|
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options) :
|
||||||
|
m_Interface(std::move(a_Interface)), m_Options(std::make_tuple<TOptions...>(std::move(a_Options)...)) {}
|
||||||
|
|
||||||
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
|
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(
|
||||||
|
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>&& a_Stream) :
|
||||||
|
m_Dispatcher(std::move(a_Stream.m_Dispatcher)), m_Interface(std::move(a_Stream.m_Interface)) {}
|
||||||
|
|
||||||
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
|
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::SendMessage(const MessageBase& a_Message) {
|
||||||
|
DataBuffer data = a_Message.Write();
|
||||||
|
DataBuffer encapsulated = std::apply([&data](const auto&... a_Options) {
|
||||||
|
return Encapsulate(data, a_Options...);
|
||||||
|
}, m_Options);
|
||||||
|
DataBuffer finalData;
|
||||||
|
finalData << VarInt{encapsulated.GetSize()} << encapsulated;
|
||||||
|
m_Interface.Write(finalData);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
|
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessages() {
|
||||||
|
while (true) {
|
||||||
|
// reading the first VarInt part byte by byte
|
||||||
|
std::uint64_t lenghtValue = 0;
|
||||||
|
unsigned int readPos = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
static constexpr int SEGMENT_BITS = (1 << 7) - 1;
|
||||||
|
static constexpr int CONTINUE_BIT = 1 << 7;
|
||||||
|
|
||||||
|
DataBuffer buffer = m_Interface.Read(sizeof(std::uint8_t));
|
||||||
|
|
||||||
|
// eof
|
||||||
|
if (buffer.GetSize() == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::uint8_t part;
|
||||||
|
buffer >> part;
|
||||||
|
lenghtValue |= static_cast<std::uint64_t>(part & SEGMENT_BITS) << readPos;
|
||||||
|
|
||||||
|
if ((part & CONTINUE_BIT) == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
readPos += 7;
|
||||||
|
|
||||||
|
if (readPos >= 8 * sizeof(lenghtValue))
|
||||||
|
throw std::runtime_error("VarInt is too big");
|
||||||
|
}
|
||||||
|
|
||||||
|
// nothing to read
|
||||||
|
if (lenghtValue == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
DataBuffer buffer;
|
||||||
|
buffer = m_Interface.Read(lenghtValue);
|
||||||
|
|
||||||
|
buffer = std::apply([&buffer, lenghtValue](const auto&... a_Options) {
|
||||||
|
return Decapsulate(buffer, a_Options...);
|
||||||
|
}, m_Options);
|
||||||
|
|
||||||
|
VarInt packetType;
|
||||||
|
buffer >> packetType;
|
||||||
|
|
||||||
|
static const MessageFactory messageFactory;
|
||||||
|
|
||||||
|
std::unique_ptr<MessageBase> message = messageFactory.CreateMessage(packetType.GetValue());
|
||||||
|
|
||||||
|
assert(message != nullptr);
|
||||||
|
|
||||||
|
message->Read(buffer);
|
||||||
|
|
||||||
|
GetDispatcher().Dispatch(*message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
|
DataBuffer Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Encapsulate(
|
||||||
|
const DataBuffer& a_Data, const TOptions&... a_Options) {
|
||||||
|
return details::MessageEncapsulatorPack<TOptions...>::Encapsulate(a_Data, a_Options...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
|
DataBuffer Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Decapsulate(
|
||||||
|
DataBuffer& a_Data, const TOptions&... a_Options) {
|
||||||
|
return details::MessageEncapsulatorPack<TOptions...>::Decapsulate(a_Data, a_Options...);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
23
include/sp/io/Memory.h
Normal file
23
include/sp/io/Memory.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sp/io/IOInterface.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
struct MemoryTag {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class IOInterface<MemoryTag> {
|
||||||
|
private:
|
||||||
|
sp::DataBuffer m_VirtualIO;
|
||||||
|
|
||||||
|
public:
|
||||||
|
sp::DataBuffer Read(std::size_t a_Amount);
|
||||||
|
void Write(const sp::DataBuffer& a_Data);
|
||||||
|
};
|
||||||
|
|
||||||
|
using Memory = IOInterface<MemoryTag>;
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
57
include/sp/protocol/MessageDispatcher.h
Normal file
57
include/sp/protocol/MessageDispatcher.h
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file MessageDispatcher.h
|
||||||
|
* \brief File containing the sp::MessageDispatcher class
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
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:
|
||||||
|
using MessageBaseType = MessageBase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \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 sp
|
||||||
@@ -5,45 +5,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include <sp/protocol/message/ArrayFillerImpl.h>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
template <typename TBase, typename TTMessages>
|
template <typename TBase, typename TTMessages>
|
||||||
class MessageFactory {
|
class MessageFactory {
|
||||||
public:
|
public:
|
||||||
@@ -51,7 +16,7 @@ class MessageFactory {
|
|||||||
|
|
||||||
MessageFactory() : m_Factory(details::ArrayFiller<TBase, TTMessages>::ArrayCreate()) {}
|
MessageFactory() : m_Factory(details::ArrayFiller<TBase, TTMessages>::ArrayCreate()) {}
|
||||||
|
|
||||||
std::unique_ptr<TBase> CreateMessage(IdType id) {
|
std::unique_ptr<TBase> CreateMessage(IdType id) const {
|
||||||
if (id >= m_Factory.size())
|
if (id >= m_Factory.size())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
return m_Factory.at(id)();
|
return m_Factory.at(id)();
|
||||||
|
|||||||
39
include/sp/protocol/message/ArrayFillerImpl.h
Normal file
39
include/sp/protocol/message/ArrayFillerImpl.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
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
|
||||||
|
} // namespace sp
|
||||||
34
include/sp/protocol/message/MessageDispatcherImpl.inl
Normal file
34
include/sp/protocol/message/MessageDispatcherImpl.inl
Normal 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);
|
||||||
|
}
|
||||||
@@ -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,19 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helper
|
||||||
|
DataBuffer Write() const {
|
||||||
|
DataBuffer buffer;
|
||||||
|
this->Write(buffer);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void WriteImpl(DataBuffer& buffer) = 0;
|
virtual void WriteImpl(DataBuffer& buffer) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handler functionality chunk
|
// Handler functionality chunk
|
||||||
@@ -85,12 +92,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,10 +116,17 @@ 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);
|
buffer << VarInt{this->GetId()};
|
||||||
this->WriteImpl(buffer);
|
this->WriteImpl(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helper
|
||||||
|
DataBuffer Write() const {
|
||||||
|
DataBuffer buffer;
|
||||||
|
this->Write(buffer);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
74
src/sp/common/ByteSwapping.cpp
Normal file
74
src/sp/common/ByteSwapping.cpp
Normal 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
|
||||||
89
src/sp/extensions/Compress.cpp
Normal file
89
src/sp/extensions/Compress.cpp
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
#include <sp/extensions/Compress.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <sp/common/VarInt.h>
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
|
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(static_cast<Bytef*>(result.data()), static_cast<uLongf*>(&uncompressedSize), static_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(static_cast<Bytef*>(result.data()), &compressedSize, static_cast<const Bytef*>(source), static_cast<uLong>(size));
|
||||||
|
result.Resize(compressedSize); // Resize to cut useless data
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
DataBuffer Compress(const DataBuffer& buffer, std::size_t a_CompressionThreshold) {
|
||||||
|
DataBuffer packet;
|
||||||
|
|
||||||
|
if (buffer.GetSize() < a_CompressionThreshold) {
|
||||||
|
// Don't compress since it's a small packet
|
||||||
|
packet << VarInt{0};
|
||||||
|
packet << buffer;
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
DataBuffer compressedData = Deflate(buffer.data(), buffer.GetSize());
|
||||||
|
VarInt uncompressedDataLength = buffer.GetSize();
|
||||||
|
|
||||||
|
if (compressedData.GetSize() >= buffer.GetSize()) {
|
||||||
|
// the compression is overkill so we don't send the compressed buffer
|
||||||
|
packet << VarInt{0};
|
||||||
|
packet << buffer;
|
||||||
|
} else {
|
||||||
|
packet << uncompressedDataLength;
|
||||||
|
packet << compressedData;
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace zlib
|
||||||
|
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
DataBuffer MessageEncapsulator<option::ZlibCompress>::Encapsulate(const DataBuffer& a_Data, const option::ZlibCompress& a_Option) {
|
||||||
|
static constexpr std::size_t MAX_COMPRESS_THRESHOLD = VarInt::MAX_VALUE;
|
||||||
|
return zlib::Compress(a_Data, a_Option.m_Enabled ? a_Option.m_CompressionThreshold : MAX_COMPRESS_THRESHOLD);
|
||||||
|
}
|
||||||
|
|
||||||
|
DataBuffer MessageEncapsulator<option::ZlibCompress>::Decapsulate(DataBuffer& a_Data, const option::ZlibCompress& a_Option) {
|
||||||
|
return zlib::Decompress(a_Data, a_Data.GetSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
3
src/sp/extensions/Encrypt.cpp
Normal file
3
src/sp/extensions/Encrypt.cpp
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#include <sp/extensions/Encrypt.h>
|
||||||
|
|
||||||
|
#include <openssl/ssl.h>
|
||||||
31
src/sp/io/File.cpp
Normal file
31
src/sp/io/File.cpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include <sp/io/File.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
File::IOInterface(const std::string& a_FilePath, unsigned int a_OpenMode) {
|
||||||
|
if (a_OpenMode & FileTag::OpenMode::In)
|
||||||
|
m_FileInput = std::make_unique<std::ifstream>(a_FilePath, std::ios::binary);
|
||||||
|
if (a_OpenMode & FileTag::OpenMode::Out)
|
||||||
|
m_FileOutput = std::make_unique<std::ofstream>(a_FilePath, std::ios::binary);
|
||||||
|
}
|
||||||
|
|
||||||
|
File::IOInterface(File&& other) :
|
||||||
|
m_FileOutput(std::move(other.m_FileOutput)), m_FileInput(std::move(other.m_FileInput)) {}
|
||||||
|
|
||||||
|
DataBuffer File::Read(std::size_t a_Amount) {
|
||||||
|
DataBuffer buffer;
|
||||||
|
buffer.Resize(a_Amount);
|
||||||
|
assert(m_FileInput != nullptr);
|
||||||
|
m_FileInput->read(reinterpret_cast<char*>(buffer.data()), a_Amount);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void File::Write(const sp::DataBuffer& a_Data) {
|
||||||
|
assert(m_FileOutput != nullptr);
|
||||||
|
m_FileOutput->write(reinterpret_cast<const char*>(a_Data.data()), a_Data.GetSize());
|
||||||
|
m_FileOutput->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
16
src/sp/io/Memory.cpp
Normal file
16
src/sp/io/Memory.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <sp/io/Memory.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
sp::DataBuffer Memory::Read(std::size_t a_Amount) {
|
||||||
|
DataBuffer data;
|
||||||
|
m_VirtualIO.ReadSome(data, a_Amount > m_VirtualIO.GetRemaining() ? m_VirtualIO.GetRemaining() : a_Amount);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
void Memory::Write(const sp::DataBuffer& a_Data) {
|
||||||
|
m_VirtualIO << a_Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
|
} // namespace sp
|
||||||
42
test/test_file.cpp
Normal file
42
test/test_file.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <examples/PacketExample.h>
|
||||||
|
#include <sp/io/File.h>
|
||||||
|
|
||||||
|
class CustomPacketHandler : public sp::PacketHandler {
|
||||||
|
void Handle(const KeepAlivePacket& packet) {
|
||||||
|
std::cout << "KeepAlive handled ! " << packet.GetKeepAliveId() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Handle(const DisconnectPacket& packet) {
|
||||||
|
std::cout << "Disconnect handled ! " << packet.GetReason() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Handle(const UpgradeTowerPacket& packet) {
|
||||||
|
std::cout << "UpgradeTower handled !\n";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::PacketFactory, sp::option::ZlibCompress>;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
auto handler = std::make_shared<CustomPacketHandler>();
|
||||||
|
|
||||||
|
FileStream stream(sp::io::File{"test.txt", sp::io::FileTag::In | sp::io::FileTag::Out}, {});
|
||||||
|
stream.GetDispatcher().RegisterHandler(PacketId::Disconnect, handler);
|
||||||
|
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
|
||||||
|
|
||||||
|
stream.SendMessage(KeepAlivePacket{96});
|
||||||
|
stream.SendMessage(KeepAlivePacket{69});
|
||||||
|
stream.SendMessage(DisconnectPacket{
|
||||||
|
"This is in the "
|
||||||
|
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
|
||||||
|
stream.GetOption<sp::option::ZlibCompress>().m_Enabled = false;
|
||||||
|
stream.SendMessage(DisconnectPacket{
|
||||||
|
"This is in the "
|
||||||
|
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
|
||||||
|
|
||||||
|
stream.RecieveMessages();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
40
test/test_io.cpp
Normal file
40
test/test_io.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <examples/PacketExample.h>
|
||||||
|
#include <sp/io/Memory.h>
|
||||||
|
|
||||||
|
using DataBufferStream = sp::io::Stream<sp::io::MemoryTag, sp::PacketDispatcher, sp::PacketFactory>;
|
||||||
|
|
||||||
|
class CustomPacketHandler : public sp::PacketHandler {
|
||||||
|
void Handle(const KeepAlivePacket& packet) {
|
||||||
|
std::cout << "KeepAlive handled ! " << packet.GetKeepAliveId() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Handle(const DisconnectPacket& packet) {
|
||||||
|
std::cout << "Disconnect handled ! " << packet.GetReason() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Handle(const UpgradeTowerPacket& packet) {
|
||||||
|
std::cout << "UpgradeTower handled !\n";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
auto handler = std::make_shared<CustomPacketHandler>();
|
||||||
|
|
||||||
|
DataBufferStream stream;
|
||||||
|
stream.GetDispatcher().RegisterHandler(PacketId::Disconnect, handler);
|
||||||
|
|
||||||
|
// this should not be dispatched
|
||||||
|
stream.SendMessage(KeepAlivePacket{96});
|
||||||
|
stream.RecieveMessages();
|
||||||
|
|
||||||
|
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
|
||||||
|
|
||||||
|
stream.SendMessage(KeepAlivePacket{69});
|
||||||
|
stream.RecieveMessages();
|
||||||
|
stream.SendMessage(DisconnectPacket{"A valid reason"});
|
||||||
|
stream.RecieveMessages();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -3,6 +3,9 @@
|
|||||||
#include <examples/PacketExample.h>
|
#include <examples/PacketExample.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include <sp/default/DefaultPacketDispatcher.h>
|
||||||
|
#include <sp/extensions/Extensions.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,11 +25,10 @@ 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();
|
||||||
msg->Write(buffer);
|
|
||||||
|
|
||||||
std::uint8_t msgId;
|
std::uint8_t msgId;
|
||||||
buffer >> msgId;
|
buffer >> msgId;
|
||||||
@@ -34,7 +36,7 @@ int main() {
|
|||||||
auto upgradeTower2 = std::make_unique<UpgradeTowerPacket>();
|
auto upgradeTower2 = std::make_unique<UpgradeTowerPacket>();
|
||||||
upgradeTower2->Read(buffer);
|
upgradeTower2->Read(buffer);
|
||||||
|
|
||||||
std::cout << "Test : " << (unsigned) upgradeTower2->GetTowerId() << "\n";
|
std::cout << "Test : " << (unsigned)upgradeTower2->GetTowerId() << "\n";
|
||||||
|
|
||||||
sp::PacketFactory factory;
|
sp::PacketFactory factory;
|
||||||
auto packet = factory.CreateMessage(msgId);
|
auto packet = factory.CreateMessage(msgId);
|
||||||
@@ -43,7 +45,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;
|
||||||
}
|
}
|
||||||
68
xmake.lua
68
xmake.lua
@@ -2,11 +2,73 @@ add_rules("mode.debug", "mode.release")
|
|||||||
|
|
||||||
set_languages("c++17")
|
set_languages("c++17")
|
||||||
|
|
||||||
target("SimpleProtocolLib")
|
local modules = {
|
||||||
|
Compression = {
|
||||||
|
Option = "zlib",
|
||||||
|
Deps = {"zlib"},
|
||||||
|
Includes = {"include/(sp/extensions/Compress.h)"},
|
||||||
|
Sources = {"src/sp/extensions/Compress.cpp"}
|
||||||
|
},
|
||||||
|
Encryption = {
|
||||||
|
Option = "ssl",
|
||||||
|
Deps = {"openssl"},
|
||||||
|
Includes = {"include/(sp/extensions/Encrypt.h)"},
|
||||||
|
Sources = {"src/sp/extensions/Encrypt.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("SimpleProtocol-" .. name)
|
||||||
add_includedirs("include")
|
add_includedirs("include")
|
||||||
add_headerfiles("include/(sp/**.h)")
|
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.Deps) do
|
||||||
|
add_packages(package)
|
||||||
|
end
|
||||||
set_group("Library")
|
set_group("Library")
|
||||||
|
set_kind("$(kind)")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
target("SimpleProtocol")
|
||||||
|
add_includedirs("include")
|
||||||
add_files("src/sp/**.cpp")
|
add_files("src/sp/**.cpp")
|
||||||
|
|
||||||
|
local includeFolders = {"common", "default", "io", "protocol"}
|
||||||
|
for _, folder in ipairs(includeFolders) do
|
||||||
|
add_headerfiles("include/(sp/" .. folder .. "/**.h)")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- adding extensions
|
||||||
|
for name, module in table.orderpairs(modules) do
|
||||||
|
if module.Deps and has_config(module.Option) then
|
||||||
|
add_deps("SimpleProtocol-" .. name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- we don't want extensions
|
||||||
|
remove_files("src/sp/extensions/**.cpp")
|
||||||
|
set_group("Library")
|
||||||
set_kind("$(kind)")
|
set_kind("$(kind)")
|
||||||
|
|
||||||
-- Tests
|
-- Tests
|
||||||
@@ -18,7 +80,7 @@ for _, file in ipairs(os.files("test/**.cpp")) do
|
|||||||
add_files(file)
|
add_files(file)
|
||||||
add_includedirs("include")
|
add_includedirs("include")
|
||||||
|
|
||||||
add_deps("SimpleProtocolLib")
|
add_deps("SimpleProtocol")
|
||||||
|
|
||||||
add_tests("compile_and_run")
|
add_tests("compile_and_run")
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user