zlib support #9

Merged
Persson-dev merged 8 commits from zlib into main 2025-03-01 18:20:51 +00:00
4 changed files with 48 additions and 17 deletions
Showing only changes of commit f6620dc522 - Show all commits

View File

@@ -9,6 +9,12 @@
#include <sp/common/DataBuffer.h>
namespace sp {
namespace option {
struct ZlibCompress {};
} // namespace option
namespace zlib {
/**

View File

@@ -13,6 +13,13 @@ class IOInterface {
void Write(const DataBuffer& a_Data);
};
template <typename IOTag>
class MessageEncapsulator {
public:
static DataBuffer Encapsulate(const DataBuffer& a_Data);
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length);
};
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
class Stream {
protected:
@@ -32,6 +39,10 @@ class Stream {
MessageDispatcher& GetDispatcher() {
return m_Dispatcher;
}
private:
static DataBuffer Encapsulate(const DataBuffer& a_Data);
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length);
};
} // namespace io

View File

@@ -1,10 +1,22 @@
#pragma once
#include <stdexcept>
#include <sp/extensions/Compress.h>
#include <stdexcept>
namespace sp {
namespace io {
template <>
class MessageEncapsulator<option::ZlibCompress> {
public:
static DataBuffer Encapsulate(const DataBuffer& a_Data) {
return zlib::Compress(a_Data);
}
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length) {
return zlib::Decompress(a_Data, a_Length);
}
};
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
Stream<IOTag, MessageDispatcher, MessageFactory>::Stream(IOInterface<IOTag>&& a_Interface) : m_Interface(std::move(a_Interface)) {}
@@ -14,15 +26,15 @@ Stream<IOTag, MessageDispatcher, MessageFactory>::Stream(Stream<IOTag, MessageDi
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
void Stream<IOTag, MessageDispatcher, MessageFactory>::SendMessage(const MessageBase& a_Message) {
// TODO: process compress + encryption
DataBuffer data = a_Message.Write();
DataBuffer compressed = zlib::Compress(data);
m_Interface.Write(compressed);
DataBuffer encapsulated = Encapsulate(data);
DataBuffer finalData;
finalData << VarInt{encapsulated.GetSize()} << encapsulated;
m_Interface.Write(finalData);
}
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
void Stream<IOTag, MessageDispatcher, MessageFactory>::RecieveMessages() {
// TODO: process compress + encryption
while (true) {
// reading the first VarInt part byte by byte
@@ -59,9 +71,7 @@ void Stream<IOTag, MessageDispatcher, MessageFactory>::RecieveMessages() {
DataBuffer buffer;
buffer = m_Interface.Read(lenghtValue);
// TODO: process compress + encryption
buffer = zlib::Decompress(buffer, lenghtValue);
buffer = Decapsulate(buffer, lenghtValue);
VarInt packetType;
buffer >> packetType;
@@ -78,5 +88,17 @@ void Stream<IOTag, MessageDispatcher, MessageFactory>::RecieveMessages() {
}
}
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
DataBuffer Stream<IOTag, MessageDispatcher, MessageFactory>::Encapsulate(const DataBuffer& a_Data) {
// TODO: process compress + encryption
return MessageEncapsulator<option::ZlibCompress>::Encapsulate(a_Data);
}
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
DataBuffer Stream<IOTag, MessageDispatcher, MessageFactory>::Decapsulate(DataBuffer& a_Data, std::size_t a_Length) {
// TODO: process compress + encryption
return MessageEncapsulator<option::ZlibCompress>::Decapsulate(a_Data, a_Length);
}
} // namespace io
} // namespace sp

View File

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