3 Commits

Author SHA1 Message Date
6a52b7fe2a fix compression 2025-02-26 15:09:23 +01:00
68fcd514a3 xmake: rename targets 2025-02-26 12:34:58 +01:00
5f9cc86ad2 xmake: add target extensions deps 2025-02-26 12:33:19 +01:00
11 changed files with 34 additions and 215 deletions

View File

@@ -1,31 +0,0 @@
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

View File

@@ -166,7 +166,7 @@ class DataBuffer {
K newKey; K newKey;
V newValue; V newValue;
*this >> newKey >> newValue; *this >> newKey >> newValue;
data.emplace(newKey, newValue); data.insert({newKey, newValue});
} }
return *this; return *this;
} }

View File

@@ -21,8 +21,6 @@ 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

View File

@@ -8,19 +8,6 @@
#include <cstdint> #include <cstdint>
#include <sp/common/DataBuffer.h> #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 sp {
namespace zlib { namespace zlib {
@@ -40,15 +27,4 @@ DataBuffer Compress(const DataBuffer& buffer, std::size_t a_CompressionThreshold
DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength); DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength);
} // namespace zlib } // 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 } // namespace sp

View File

@@ -1,41 +0,0 @@
#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

View File

@@ -13,43 +13,25 @@ class IOInterface {
void Write(const DataBuffer& a_Data); void Write(const DataBuffer& a_Data);
}; };
template <typename TOption> template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
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 { class Stream {
protected: protected:
MessageDispatcher m_Dispatcher; MessageDispatcher m_Dispatcher;
IOInterface<IOTag> m_Interface; IOInterface<IOTag> m_Interface;
std::tuple<TOptions...> m_Options;
using MessageBase = typename MessageDispatcher::MessageBaseType; using MessageBase = typename MessageDispatcher::MessageBaseType;
public: public:
Stream() {} Stream() {}
Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options); Stream(IOInterface<IOTag>&& a_Interface);
Stream(Stream&& a_Stream); Stream(Stream&& a_Stream);
void RecieveMessages(); void RecieveMessages();
void SendMessage(const MessageBase& a_Message); void SendMessage(const MessageBase& a_Message);
template <typename TOption>
TOption& GetOption() {
return std::get<TOption>(m_Options);
}
MessageDispatcher& GetDispatcher() { MessageDispatcher& GetDispatcher() {
return m_Dispatcher; 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 io

View File

@@ -1,64 +1,30 @@
#pragma once #pragma once
#include <sp/extensions/Compress.h>
#include <stdexcept> #include <stdexcept>
#include <sp/extensions/Compress.h>
namespace sp { 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 { namespace io {
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
Stream<IOTag, MessageDispatcher, MessageFactory>::Stream(IOInterface<IOTag>&& a_Interface) : m_Interface(std::move(a_Interface)) {}
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions> template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options) : Stream<IOTag, MessageDispatcher, MessageFactory>::Stream(Stream<IOTag, MessageDispatcher, MessageFactory>&& a_Stream) :
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)) {} 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> template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::SendMessage(const MessageBase& a_Message) { void Stream<IOTag, MessageDispatcher, MessageFactory>::SendMessage(const MessageBase& a_Message) {
// TODO: process compress + encryption
DataBuffer data = a_Message.Write(); DataBuffer data = a_Message.Write();
DataBuffer encapsulated = std::apply([&data](const auto&... a_Options) { DataBuffer compressed = zlib::Compress(data);
return Encapsulate(data, a_Options...); m_Interface.Write(compressed);
}, m_Options);
DataBuffer finalData;
finalData << VarInt{encapsulated.GetSize()} << encapsulated;
m_Interface.Write(finalData);
} }
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions> template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessages() { void Stream<IOTag, MessageDispatcher, MessageFactory>::RecieveMessages() {
// TODO: process compress + encryption
while (true) { while (true) {
// reading the first VarInt part byte by byte // reading the first VarInt part byte by byte
std::uint64_t lenghtValue = 0; std::uint64_t lenghtValue = 0;
unsigned int readPos = 0; unsigned int readPos = 0;
@@ -93,9 +59,9 @@ void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessa
DataBuffer buffer; DataBuffer buffer;
buffer = m_Interface.Read(lenghtValue); buffer = m_Interface.Read(lenghtValue);
buffer = std::apply([&buffer, lenghtValue](const auto&... a_Options) { // TODO: process compress + encryption
return Decapsulate(buffer, a_Options...);
}, m_Options); buffer = zlib::Decompress(buffer, lenghtValue);
VarInt packetType; VarInt packetType;
buffer >> packetType; buffer >> packetType;
@@ -112,17 +78,5 @@ void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessa
} }
} }
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 io
} // namespace sp } // namespace sp

View File

@@ -34,13 +34,21 @@ DataBuffer Compress(const DataBuffer& buffer, std::size_t a_CompressionThreshold
if (buffer.GetSize() < a_CompressionThreshold) { if (buffer.GetSize() < a_CompressionThreshold) {
// Don't compress since it's a small packet // Don't compress since it's a small packet
packet << VarInt{0}; VarInt compressedDataLength = 0;
VarInt packetLength = compressedDataLength.GetSerializedLength() + buffer.GetSize();
packet << packetLength;
packet << compressedDataLength;
packet << buffer; packet << buffer;
return packet; return packet;
} }
DataBuffer compressedData = Deflate(buffer.data(), buffer.GetSize()); DataBuffer compressedData = Deflate(buffer.data(), buffer.GetSize());
VarInt uncompressedDataLength = buffer.GetSize(); VarInt uncompressedDataLength = buffer.GetSize();
VarInt packetLength = uncompressedDataLength.GetSerializedLength() + compressedData.GetSize();
packet << packetLength;
if (compressedData.GetSize() >= buffer.GetSize()) { if (compressedData.GetSize() >= buffer.GetSize()) {
// the compression is overkill so we don't send the compressed buffer // the compression is overkill so we don't send the compressed buffer
@@ -73,17 +81,4 @@ DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) {
} }
} // namespace zlib } // 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 } // namespace sp

View File

@@ -1,3 +0,0 @@
#include <sp/extensions/Encrypt.h>
#include <openssl/ssl.h>

View File

@@ -17,24 +17,18 @@ class CustomPacketHandler : public sp::PacketHandler {
} }
}; };
using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::PacketFactory, sp::option::ZlibCompress>; using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::PacketFactory>;
int main() { int main() {
auto handler = std::make_shared<CustomPacketHandler>(); auto handler = std::make_shared<CustomPacketHandler>();
FileStream stream(sp::io::File{"test.txt", sp::io::FileTag::In | sp::io::FileTag::Out}, {}); 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::Disconnect, handler);
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler); stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
stream.SendMessage(KeepAlivePacket{96}); stream.SendMessage(KeepAlivePacket{96});
stream.SendMessage(KeepAlivePacket{69}); stream.SendMessage(KeepAlivePacket{69});
stream.SendMessage(DisconnectPacket{ stream.SendMessage(DisconnectPacket{"This is in the fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
"This is in the "
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
stream.GetOption<sp::option::ZlibCompress>().m_Enabled = false;
stream.SendMessage(DisconnectPacket{
"This is in the "
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
stream.RecieveMessages(); stream.RecieveMessages();

View File

@@ -6,14 +6,9 @@ local modules = {
Compression = { Compression = {
Option = "zlib", Option = "zlib",
Deps = {"zlib"}, Deps = {"zlib"},
Packages = {"zlib"},
Includes = {"include/(sp/extensions/Compress.h)"}, Includes = {"include/(sp/extensions/Compress.h)"},
Sources = {"src/sp/extensions/Compress.cpp"} Sources = {"src/sp/extensions/Compress.cpp"}
},
Encryption = {
Option = "ssl",
Deps = {"openssl"},
Includes = {"include/(sp/extensions/Encrypt.h)"},
Sources = {"src/sp/extensions/Encrypt.cpp"}
} }
} }
@@ -42,7 +37,7 @@ for name, module in table.orderpairs(modules) do
for _, source in table.orderpairs(module.Sources) do for _, source in table.orderpairs(module.Sources) do
add_files(source) add_files(source)
end end
for _, package in table.orderpairs(module.Deps) do for _, package in table.orderpairs(module.Packages) do
add_packages(package) add_packages(package)
end end
set_group("Library") set_group("Library")