zlib support #9

Merged
Persson-dev merged 8 commits from zlib into main 2025-03-01 18:20:51 +00:00
5 changed files with 36 additions and 23 deletions
Showing only changes of commit 254de7a9ee - Show all commits

View File

@@ -21,6 +21,8 @@ class VarInt {
std::uint64_t m_Value;
public:
static const std::uint64_t MAX_VALUE = static_cast<std::uint64_t>(-1) >> 8;
VarInt() : m_Value(0) {}
/**
* \brief Construct a variable integer from a value

View File

@@ -25,16 +25,23 @@ 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);
Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options);
Stream(Stream&& a_Stream);
void RecieveMessages(const TOptions&... a_Options);
void SendMessage(const MessageBase& a_Message, const TOptions&... a_Options);
void RecieveMessages();
void SendMessage(const MessageBase& a_Message);
template <typename TOption>
TOption& GetOption() {
return std::get<TOption>(m_Options);
}
MessageDispatcher& GetDispatcher() {
return m_Dispatcher;

View File

@@ -37,8 +37,8 @@ struct MessageEncapsulatorPack<TOption, TOptions...> {
namespace io {
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(IOInterface<IOTag>&& a_Interface) :
m_Interface(std::move(a_Interface)) {}
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(
@@ -46,17 +46,18 @@ Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::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, const TOptions&... a_Options) {
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::SendMessage(const MessageBase& a_Message) {
DataBuffer data = a_Message.Write();
DataBuffer encapsulated = Encapsulate(data, a_Options...);
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(const TOptions&... a_Options) {
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessages() {
while (true) {
// reading the first VarInt part byte by byte
std::uint64_t lenghtValue = 0;
@@ -92,7 +93,9 @@ void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessa
DataBuffer buffer;
buffer = m_Interface.Read(lenghtValue);
buffer = Decapsulate(buffer, lenghtValue, a_Options...);
buffer = std::apply([&buffer, lenghtValue](const auto&... a_Options) {
return Decapsulate(buffer, lenghtValue, a_Options...);
}, m_Options);
VarInt packetType;
buffer >> packetType;

View File

@@ -77,12 +77,13 @@ DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) {
namespace io {
DataBuffer MessageEncapsulator<option::ZlibCompress>::Encapsulate(const DataBuffer& a_Data, const option::ZlibCompress& a_Option) {
return a_Option.m_Enabled ? zlib::Compress(a_Data, a_Option.m_CompressionThreshold) : a_Data;
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, std::size_t a_Length, const option::ZlibCompress& a_Option) {
return a_Option.m_Enabled ? zlib::Decompress(a_Data, a_Length) : a_Data;
return zlib::Decompress(a_Data, a_Length);
}
} // namespace io

View File

@@ -22,21 +22,21 @@ using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::Pac
int main() {
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::KeepAlive, handler);
auto zlibOptions = sp::option::ZlibCompress{};
stream.SendMessage(KeepAlivePacket{96}, zlibOptions);
stream.SendMessage(KeepAlivePacket{69}, zlibOptions);
stream.SendMessage(
DisconnectPacket{
stream.SendMessage(KeepAlivePacket{96});
stream.SendMessage(KeepAlivePacket{69});
stream.SendMessage(DisconnectPacket{
"This is in the "
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"},
zlibOptions);
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
stream.GetOption<sp::option::ZlibCompress>().m_Enabled = false;
stream.SendMessage(DisconnectPacket{
"This is in the "
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
stream.RecieveMessages(zlibOptions);
stream.RecieveMessages();
return 0;
}