compress refactor
This commit is contained in:
@@ -9,6 +9,12 @@
|
|||||||
#include <sp/common/DataBuffer.h>
|
#include <sp/common/DataBuffer.h>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
namespace option {
|
||||||
|
|
||||||
|
struct ZlibCompress {};
|
||||||
|
|
||||||
|
} // namespace option
|
||||||
|
|
||||||
namespace zlib {
|
namespace zlib {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -13,6 +13,13 @@ class IOInterface {
|
|||||||
void Write(const DataBuffer& a_Data);
|
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>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
|
||||||
class Stream {
|
class Stream {
|
||||||
protected:
|
protected:
|
||||||
@@ -32,6 +39,10 @@ class Stream {
|
|||||||
MessageDispatcher& GetDispatcher() {
|
MessageDispatcher& GetDispatcher() {
|
||||||
return m_Dispatcher;
|
return m_Dispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data);
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace io
|
} // namespace io
|
||||||
|
|||||||
@@ -1,10 +1,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <sp/extensions/Compress.h>
|
#include <sp/extensions/Compress.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
namespace io {
|
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>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
|
||||||
Stream<IOTag, MessageDispatcher, MessageFactory>::Stream(IOInterface<IOTag>&& a_Interface) : m_Interface(std::move(a_Interface)) {}
|
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>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
|
||||||
void Stream<IOTag, MessageDispatcher, MessageFactory>::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 compressed = zlib::Compress(data);
|
DataBuffer encapsulated = Encapsulate(data);
|
||||||
m_Interface.Write(compressed);
|
DataBuffer finalData;
|
||||||
|
finalData << VarInt{encapsulated.GetSize()} << encapsulated;
|
||||||
|
m_Interface.Write(finalData);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
|
||||||
void Stream<IOTag, MessageDispatcher, MessageFactory>::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
|
||||||
@@ -59,9 +71,7 @@ void Stream<IOTag, MessageDispatcher, MessageFactory>::RecieveMessages() {
|
|||||||
DataBuffer buffer;
|
DataBuffer buffer;
|
||||||
buffer = m_Interface.Read(lenghtValue);
|
buffer = m_Interface.Read(lenghtValue);
|
||||||
|
|
||||||
// TODO: process compress + encryption
|
buffer = Decapsulate(buffer, lenghtValue);
|
||||||
|
|
||||||
buffer = zlib::Decompress(buffer, lenghtValue);
|
|
||||||
|
|
||||||
VarInt packetType;
|
VarInt packetType;
|
||||||
buffer >> 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 io
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
@@ -34,21 +34,13 @@ 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
|
||||||
VarInt compressedDataLength = 0;
|
packet << VarInt{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
|
||||||
|
|||||||
Reference in New Issue
Block a user