Compare commits
5 Commits
v2.0.0
...
9fa023f716
| Author | SHA1 | Date | |
|---|---|---|---|
| 9fa023f716 | |||
| f6620dc522 | |||
| 6a52b7fe2a | |||
| 68fcd514a3 | |||
| 5f9cc86ad2 |
@@ -8,6 +8,19 @@
|
|||||||
#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 {
|
||||||
|
|
||||||
@@ -16,14 +29,7 @@ namespace zlib {
|
|||||||
* \param buffer the data to compress
|
* \param buffer the data to compress
|
||||||
* \return the compressed data
|
* \return the compressed data
|
||||||
*/
|
*/
|
||||||
DataBuffer Compress(const DataBuffer& buffer);
|
DataBuffer Compress(const DataBuffer& buffer, std::size_t a_CompressionThreshold = 64);
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Reads the packet lenght and uncompress it
|
|
||||||
* \param buffer the data to uncompress
|
|
||||||
* \return the uncompressed data
|
|
||||||
*/
|
|
||||||
DataBuffer Decompress(DataBuffer& buffer);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Uncompress some data
|
* \brief Uncompress some data
|
||||||
@@ -34,4 +40,15 @@ DataBuffer Decompress(DataBuffer& buffer);
|
|||||||
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, std::size_t a_Length, const option::ZlibCompress& a_Option);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
@@ -13,7 +13,14 @@ class IOInterface {
|
|||||||
void Write(const DataBuffer& a_Data);
|
void Write(const DataBuffer& a_Data);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
|
template <typename TOption>
|
||||||
|
class MessageEncapsulator {
|
||||||
|
public:
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOption& a_Option);
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length, 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;
|
||||||
@@ -26,12 +33,16 @@ class Stream {
|
|||||||
Stream(IOInterface<IOTag>&& a_Interface);
|
Stream(IOInterface<IOTag>&& a_Interface);
|
||||||
Stream(Stream&& a_Stream);
|
Stream(Stream&& a_Stream);
|
||||||
|
|
||||||
void RecieveMessages();
|
void RecieveMessages(const TOptions&... a_Options);
|
||||||
void SendMessage(const MessageBase& a_Message);
|
void SendMessage(const MessageBase& a_Message, const TOptions&... a_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, std::size_t a_Length, const TOptions&... a_Options);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace io
|
} // namespace io
|
||||||
|
|||||||
@@ -1,29 +1,63 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <sp/extensions/Compress.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
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>
|
namespace details {
|
||||||
Stream<IOTag, MessageDispatcher, MessageFactory>::Stream(Stream<IOTag, MessageDispatcher, MessageFactory>&& a_Stream) :
|
|
||||||
|
template <typename... TOptions>
|
||||||
|
struct MessageEncapsulatorPack {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct MessageEncapsulatorPack<> {
|
||||||
|
static DataBuffer Encapsulate(const DataBuffer& a_Data) {
|
||||||
|
return a_Data;
|
||||||
|
}
|
||||||
|
static DataBuffer Decapsulate(DataBuffer& a_Data, std::size_t a_Length) {
|
||||||
|
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, std::size_t a_Length, const TOption& a_Option, const TOptions&... a_Options) {
|
||||||
|
DataBuffer data = io::MessageEncapsulator<TOption>::Decapsulate(a_Data, a_Length, a_Option);
|
||||||
|
return MessageEncapsulatorPack<TOptions...>::Decapsulate(data, data.GetSize(), a_Options...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
|
||||||
|
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)) {}
|
||||||
|
|
||||||
|
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>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
|
||||||
void Stream<IOTag, MessageDispatcher, MessageFactory>::SendMessage(const MessageBase& a_Message) {
|
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::SendMessage(
|
||||||
// TODO: process compress + encryption
|
const MessageBase& a_Message, const TOptions&... a_Options) {
|
||||||
DataBuffer data = a_Message.Write();
|
DataBuffer data = a_Message.Write();
|
||||||
DataBuffer dataSize;
|
DataBuffer encapsulated = Encapsulate(data, a_Options...);
|
||||||
m_Interface.Write(dataSize << sp::VarInt{data.GetSize()} << data);
|
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, typename... TOptions>
|
||||||
void Stream<IOTag, MessageDispatcher, MessageFactory>::RecieveMessages() {
|
void Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>::RecieveMessages(const TOptions&... a_Options) {
|
||||||
// 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;
|
||||||
@@ -58,7 +92,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, a_Options...);
|
||||||
|
|
||||||
VarInt packetType;
|
VarInt packetType;
|
||||||
buffer >> packetType;
|
buffer >> packetType;
|
||||||
@@ -75,5 +109,17 @@ void Stream<IOTag, MessageDispatcher, MessageFactory>::RecieveMessages() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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, std::size_t a_Length, const TOptions&... a_Options) {
|
||||||
|
return details::MessageEncapsulatorPack<TOptions...>::Decapsulate(a_Data, a_Length, a_Options...);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace io
|
} // namespace io
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
@@ -4,8 +4,6 @@
|
|||||||
#include <sp/common/VarInt.h>
|
#include <sp/common/VarInt.h>
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
|
|
||||||
#define COMPRESSION_THRESHOLD 64
|
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
namespace zlib {
|
namespace zlib {
|
||||||
|
|
||||||
@@ -13,8 +11,8 @@ static DataBuffer Inflate(const std::uint8_t* source, std::size_t size, std::siz
|
|||||||
DataBuffer result;
|
DataBuffer result;
|
||||||
result.Resize(uncompressedSize);
|
result.Resize(uncompressedSize);
|
||||||
|
|
||||||
uncompress(reinterpret_cast<Bytef*>(result.data()), reinterpret_cast<uLongf*>(&uncompressedSize),
|
uncompress(static_cast<Bytef*>(result.data()), static_cast<uLongf*>(&uncompressedSize), static_cast<const Bytef*>(source),
|
||||||
reinterpret_cast<const Bytef*>(source), static_cast<uLong>(size));
|
static_cast<uLong>(size));
|
||||||
|
|
||||||
assert(result.GetSize() == uncompressedSize);
|
assert(result.GetSize() == uncompressedSize);
|
||||||
return result;
|
return result;
|
||||||
@@ -25,35 +23,34 @@ static DataBuffer Deflate(const std::uint8_t* source, std::size_t size) {
|
|||||||
uLongf compressedSize = size;
|
uLongf compressedSize = size;
|
||||||
|
|
||||||
result.Resize(size); // Resize for the compressed data to fit into
|
result.Resize(size); // Resize for the compressed data to fit into
|
||||||
compress(
|
compress(static_cast<Bytef*>(result.data()), &compressedSize, static_cast<const Bytef*>(source), static_cast<uLong>(size));
|
||||||
reinterpret_cast<Bytef*>(result.data()), &compressedSize, reinterpret_cast<const Bytef*>(source), static_cast<uLong>(size));
|
|
||||||
result.Resize(compressedSize); // Resize to cut useless data
|
result.Resize(compressedSize); // Resize to cut useless data
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBuffer Compress(const DataBuffer& buffer) {
|
DataBuffer Compress(const DataBuffer& buffer, std::size_t a_CompressionThreshold) {
|
||||||
DataBuffer packet;
|
DataBuffer packet;
|
||||||
|
|
||||||
if (buffer.GetSize() < COMPRESSION_THRESHOLD) {
|
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};
|
||||||
std::uint64_t 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();
|
||||||
std::uint64_t packetLength = uncompressedDataLength.GetSerializedLength() + compressedData.GetSize();
|
|
||||||
|
|
||||||
packet << packetLength;
|
if (compressedData.GetSize() >= buffer.GetSize()) {
|
||||||
packet << uncompressedDataLength;
|
// the compression is overkill so we don't send the compressed buffer
|
||||||
packet.WriteSome(compressedData.data(), compressedData.GetSize());
|
packet << VarInt{0};
|
||||||
|
packet << buffer;
|
||||||
|
} else {
|
||||||
|
packet << uncompressedDataLength;
|
||||||
|
packet << compressedData;
|
||||||
|
}
|
||||||
|
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,12 +72,18 @@ DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) {
|
|||||||
return Inflate(buffer.data() + buffer.GetReadOffset(), compressedLength, uncompressedLength.GetValue());
|
return Inflate(buffer.data() + buffer.GetReadOffset(), compressedLength, uncompressedLength.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBuffer Decompress(DataBuffer& buffer) {
|
} // namespace zlib
|
||||||
std::uint64_t packetLength;
|
|
||||||
buffer >> packetLength;
|
|
||||||
|
|
||||||
return Decompress(buffer, 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace zlib
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace io
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class CustomPacketHandler : public sp::PacketHandler {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::PacketFactory>;
|
using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::PacketFactory, sp::option::ZlibCompress>;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
auto handler = std::make_shared<CustomPacketHandler>();
|
auto handler = std::make_shared<CustomPacketHandler>();
|
||||||
@@ -26,11 +26,17 @@ int main() {
|
|||||||
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});
|
auto zlibOptions = sp::option::ZlibCompress{};
|
||||||
stream.SendMessage(KeepAlivePacket{69});
|
|
||||||
stream.SendMessage(DisconnectPacket{"This is in the file !"});
|
|
||||||
|
|
||||||
stream.RecieveMessages();
|
stream.SendMessage(KeepAlivePacket{96}, zlibOptions);
|
||||||
|
stream.SendMessage(KeepAlivePacket{69}, zlibOptions);
|
||||||
|
stream.SendMessage(
|
||||||
|
DisconnectPacket{
|
||||||
|
"This is in the "
|
||||||
|
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"},
|
||||||
|
zlibOptions);
|
||||||
|
|
||||||
|
stream.RecieveMessages(zlibOptions);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ int main() {
|
|||||||
|
|
||||||
stream.SendMessage(KeepAlivePacket{69});
|
stream.SendMessage(KeepAlivePacket{69});
|
||||||
stream.RecieveMessages();
|
stream.RecieveMessages();
|
||||||
stream.SendMessage(DisconnectPacket{"I don't know"});
|
stream.SendMessage(DisconnectPacket{"A valid reason"});
|
||||||
stream.RecieveMessages();
|
stream.RecieveMessages();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
13
xmake.lua
13
xmake.lua
@@ -29,7 +29,7 @@ end
|
|||||||
-- Add modules targets
|
-- Add modules targets
|
||||||
for name, module in table.orderpairs(modules) do
|
for name, module in table.orderpairs(modules) do
|
||||||
if module.Deps and has_config(module.Option) then
|
if module.Deps and has_config(module.Option) then
|
||||||
target("SimpleProtocolLib-" .. name)
|
target("SimpleProtocol-" .. name)
|
||||||
add_includedirs("include")
|
add_includedirs("include")
|
||||||
for _, include in table.orderpairs(module.Includes) do
|
for _, include in table.orderpairs(module.Includes) do
|
||||||
add_headerfiles(include)
|
add_headerfiles(include)
|
||||||
@@ -45,7 +45,7 @@ for name, module in table.orderpairs(modules) do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
target("SimpleProtocolLib")
|
target("SimpleProtocol")
|
||||||
add_includedirs("include")
|
add_includedirs("include")
|
||||||
add_files("src/sp/**.cpp")
|
add_files("src/sp/**.cpp")
|
||||||
|
|
||||||
@@ -54,6 +54,13 @@ target("SimpleProtocolLib")
|
|||||||
add_headerfiles("include/(sp/" .. folder .. "/**.h)")
|
add_headerfiles("include/(sp/" .. folder .. "/**.h)")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- adding extensions
|
||||||
|
for name, module in table.orderpairs(modules) do
|
||||||
|
if module.Deps and has_config(module.Option) then
|
||||||
|
add_deps("SimpleProtocol-" .. name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- we don't want extensions
|
-- we don't want extensions
|
||||||
remove_files("src/sp/extensions/**.cpp")
|
remove_files("src/sp/extensions/**.cpp")
|
||||||
set_group("Library")
|
set_group("Library")
|
||||||
@@ -68,7 +75,7 @@ for _, file in ipairs(os.files("test/**.cpp")) do
|
|||||||
add_files(file)
|
add_files(file)
|
||||||
add_includedirs("include")
|
add_includedirs("include")
|
||||||
|
|
||||||
add_deps("SimpleProtocolLib")
|
add_deps("SimpleProtocol")
|
||||||
|
|
||||||
add_tests("compile_and_run")
|
add_tests("compile_and_run")
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user