zlib support #9
@@ -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
|
||||||
|
|||||||
@@ -25,16 +25,23 @@ 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);
|
Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options);
|
||||||
Stream(Stream&& a_Stream);
|
Stream(Stream&& a_Stream);
|
||||||
|
|
||||||
void RecieveMessages(const TOptions&... a_Options);
|
void RecieveMessages();
|
||||||
void SendMessage(const MessageBase& a_Message, const TOptions&... a_Options);
|
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;
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ struct MessageEncapsulatorPack<TOption, TOptions...> {
|
|||||||
namespace io {
|
namespace io {
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(IOInterface<IOTag>&& a_Interface) :
|
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options) :
|
||||||
m_Interface(std::move(a_Interface)) {}
|
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>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::Stream(
|
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)) {}
|
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, typename... TOptions>
|
||||||
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::SendMessage(
|
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::SendMessage(const MessageBase& a_Message) {
|
||||||
const MessageBase& a_Message, const TOptions&... a_Options) {
|
|
||||||
DataBuffer data = a_Message.Write();
|
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;
|
DataBuffer finalData;
|
||||||
finalData << VarInt{encapsulated.GetSize()} << encapsulated;
|
finalData << VarInt{encapsulated.GetSize()} << encapsulated;
|
||||||
m_Interface.Write(finalData);
|
m_Interface.Write(finalData);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
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) {
|
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;
|
||||||
@@ -92,7 +93,9 @@ void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessa
|
|||||||
DataBuffer buffer;
|
DataBuffer buffer;
|
||||||
buffer = m_Interface.Read(lenghtValue);
|
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;
|
VarInt packetType;
|
||||||
buffer >> packetType;
|
buffer >> packetType;
|
||||||
|
|||||||
@@ -77,12 +77,13 @@ DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) {
|
|||||||
namespace io {
|
namespace io {
|
||||||
|
|
||||||
DataBuffer MessageEncapsulator<option::ZlibCompress>::Encapsulate(const DataBuffer& a_Data, const option::ZlibCompress& a_Option) {
|
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 MessageEncapsulator<option::ZlibCompress>::Decapsulate(
|
||||||
DataBuffer& a_Data, std::size_t a_Length, const option::ZlibCompress& a_Option) {
|
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
|
} // namespace io
|
||||||
|
|||||||
@@ -22,21 +22,21 @@ using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::Pac
|
|||||||
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);
|
||||||
|
|
||||||
auto zlibOptions = sp::option::ZlibCompress{};
|
stream.SendMessage(KeepAlivePacket{96});
|
||||||
|
stream.SendMessage(KeepAlivePacket{69});
|
||||||
stream.SendMessage(KeepAlivePacket{96}, zlibOptions);
|
stream.SendMessage(DisconnectPacket{
|
||||||
stream.SendMessage(KeepAlivePacket{69}, zlibOptions);
|
|
||||||
stream.SendMessage(
|
|
||||||
DisconnectPacket{
|
|
||||||
"This is in the "
|
"This is in the "
|
||||||
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"},
|
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
|
||||||
zlibOptions);
|
stream.GetOption<sp::option::ZlibCompress>().m_Enabled = false;
|
||||||
|
stream.SendMessage(DisconnectPacket{
|
||||||
|
"This is in the "
|
||||||
|
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
|
||||||
|
|
||||||
stream.RecieveMessages(zlibOptions);
|
stream.RecieveMessages();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user