3 Commits

Author SHA1 Message Date
37ff881819 add file input/output 2025-02-26 00:07:06 +01:00
a2eb10ec6d add file interface 2025-02-26 00:01:51 +01:00
132c3c3c8d add base io interface 2025-02-25 23:25:12 +01:00
20 changed files with 190 additions and 435 deletions

View File

@@ -1,31 +0,0 @@
name: Linux arm64
run-name: Build And Test
on: [push]
env:
XMAKE_ROOT: y
jobs:
Build:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v3
- name: Prepare XMake
uses: xmake-io/github-action-setup-xmake@v1
with:
xmake-version: latest
actions-cache-folder: '.xmake-cache'
actions-cache-key: 'ubuntu'
- name: XMake config
run: xmake f -p linux -y
- name: Build
run: xmake
- name: Test
run: xmake test

View File

@@ -166,7 +166,7 @@ class DataBuffer {
K newKey; K newKey;
V newValue; V newValue;
*this >> newKey >> newValue; *this >> newKey >> newValue;
data.emplace(newKey, newValue); data.insert({newKey, newValue});
} }
return *this; return *this;
} }

View File

@@ -21,8 +21,6 @@ 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

View File

@@ -8,19 +8,6 @@
#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 {
@@ -29,7 +16,14 @@ 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, std::size_t a_CompressionThreshold = 64); DataBuffer Compress(const DataBuffer& buffer);
/**
* \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
@@ -40,15 +34,4 @@ DataBuffer Compress(const DataBuffer& buffer, std::size_t a_CompressionThreshold
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, const option::ZlibCompress& a_Option);
};
} // namespace io
} // namespace sp } // namespace sp

View File

@@ -1,41 +0,0 @@
#pragma once
/**
* \file Encrypt.h
* \brief File containing encrypt utilities
*/
#include <sp/common/DataBuffer.h>
namespace sp {
namespace option {
struct TlsEncrypt {
bool m_Enabled = true;
};
} // namespace option
} // namespace sp
#include <sp/io/IOInterface.h>
namespace sp {
namespace ssl {
// encrypt
// decrypt
} // namespace ssl
namespace io {
template <>
class MessageEncapsulator<option::TlsEncrypt> {
public:
static DataBuffer Encapsulate(const DataBuffer& a_Data, const option::TlsEncrypt& a_Option);
static DataBuffer Decapsulate(DataBuffer& a_Data, const option::TlsEncrypt& a_Option);
};
} // namespace io
} // namespace sp

View File

@@ -1,33 +0,0 @@
#pragma once
#include <fstream>
#include <sp/io/IOInterface.h>
namespace sp {
namespace io {
struct FileTag {
enum OpenMode {
In = 1,
Out = 1 << 1,
};
};
template <>
class IOInterface<FileTag> {
private:
std::unique_ptr<std::ifstream> m_FileInput;
std::unique_ptr<std::ofstream> m_FileOutput;
public:
IOInterface(const std::string& a_FilePath, unsigned int a_OpenMode);
IOInterface(IOInterface&& other);
DataBuffer Read(std::size_t a_Amount);
void Write(const sp::DataBuffer& a_Data);
};
using File = IOInterface<FileTag>;
} // namespace io
} // namespace sp

42
include/sp/io/FileIO.h Normal file
View File

@@ -0,0 +1,42 @@
#pragma once
#include <fstream>
#include <sp/io/IOInterface.h>
namespace sp {
struct FileTag {};
template <>
class IOInterface<FileTag> {
private:
std::unique_ptr<std::ifstream> m_FileInput;
std::unique_ptr<std::ofstream> m_FileOutput;
public:
IOInterface(const std::string& fileInput, const std::string& fileOutput) {
if (!fileInput.empty())
m_FileInput = std::make_unique<std::ifstream>(fileInput);
if (!fileOutput.empty())
m_FileOutput = std::make_unique<std::ofstream>(fileOutput);
}
IOInterface(IOInterface&& other) : m_FileOutput(std::move(other.m_FileOutput)), m_FileInput(std::move(other.m_FileInput)) {}
DataBuffer Read(std::size_t a_Amount) {
DataBuffer buffer;
buffer.Resize(a_Amount);
assert(m_FileInput != nullptr);
m_FileInput->read(reinterpret_cast<char*>(buffer.data()), a_Amount);
return buffer;
}
void Write(const sp::DataBuffer& a_Data) {
assert(m_FileOutput != nullptr);
m_FileOutput->write(reinterpret_cast<const char*>(a_Data.data()), a_Data.GetSize());
m_FileOutput->flush();
}
};
using FileIO = IOInterface<FileTag>;
} // namespace sp

View File

@@ -4,7 +4,6 @@
#include <sp/common/DataBuffer.h> #include <sp/common/DataBuffer.h>
namespace sp { namespace sp {
namespace io {
template <typename IOTag> template <typename IOTag>
class IOInterface { class IOInterface {
@@ -13,46 +12,90 @@ class IOInterface {
void Write(const DataBuffer& a_Data); void Write(const DataBuffer& a_Data);
}; };
template <typename TOption> template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
class MessageEncapsulator { class IOStream {
public:
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOption& a_Option);
static DataBuffer Decapsulate(DataBuffer& a_Data, const TOption& a_Option);
};
template <typename IOTag, typename MessageDispatcher, typename MessageFactory, typename... TOptions>
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;
using MsgIdType = typename MessageBase::MsgIdType;
public: public:
Stream() {} IOStream() {}
Stream(IOInterface<IOTag>&& a_Interface, TOptions&&... a_Options); IOStream(IOInterface<IOTag>&& a_Interface) : m_Interface(std::move(a_Interface)) {}
Stream(Stream&& a_Stream); IOStream(IOStream&& a_Stream) : m_Dispatcher(std::move(a_Stream.m_Dispatcher)), m_Interface(std::move(a_Stream.m_Interface)) {}
void RecieveMessages(); void RecieveMessages();
void SendMessage(const MessageBase& a_Message); 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;
} }
private:
static DataBuffer Encapsulate(const DataBuffer& a_Data, const TOptions&... a_Options);
static DataBuffer Decapsulate(DataBuffer& a_Data, const TOptions&... a_Options);
}; };
} // namespace io template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
} // namespace sp void IOStream<IOTag, MessageDispatcher, MessageFactory>::SendMessage(const MessageBase& a_Message) {
// TODO: process compress + encryption
DataBuffer data = a_Message.Write();
DataBuffer dataSize;
m_Interface.Write(dataSize << sp::VarInt{data.GetSize()} << data);
}
#include <sp/io/IOInterfaceImpl.inl> template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
void IOStream<IOTag, MessageDispatcher, MessageFactory>::RecieveMessages() {
// TODO: process compress + encryption
while (true) {
// reading the first VarInt part byte by byte
std::uint64_t lenghtValue = 0;
unsigned int readPos = 0;
while (true) {
static constexpr int SEGMENT_BITS = (1 << 7) - 1;
static constexpr int CONTINUE_BIT = 1 << 7;
DataBuffer buffer = m_Interface.Read(sizeof(std::uint8_t));
// if non-blocking call
if (buffer.GetSize() == 0)
return;
std::uint8_t part;
buffer >> part;
lenghtValue |= static_cast<std::uint64_t>(part & SEGMENT_BITS) << readPos;
if ((part & CONTINUE_BIT) == 0)
break;
readPos += 7;
if (readPos >= 8 * sizeof(lenghtValue))
throw std::runtime_error("VarInt is too big");
}
// nothing to read
if (lenghtValue == 0)
return;
DataBuffer buffer;
buffer = m_Interface.Read(lenghtValue);
// TODO: process compress + encryption
MsgIdType packetType;
buffer >> packetType;
static const MessageFactory messageFactory;
std::unique_ptr<MessageBase> message = messageFactory.CreateMessage(packetType);
assert(message != nullptr);
message->Read(buffer);
GetDispatcher().Dispatch(*message);
}
}
} // namespace sp

View File

@@ -1,128 +1 @@
#pragma once #pragma once
#include <sp/extensions/Compress.h>
#include <stdexcept>
namespace sp {
namespace details {
template <typename... TOptions>
struct MessageEncapsulatorPack {};
template <>
struct MessageEncapsulatorPack<> {
static DataBuffer Encapsulate(const DataBuffer& a_Data) {
return a_Data;
}
static DataBuffer Decapsulate(DataBuffer& a_Data) {
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, const TOption& a_Option, const TOptions&... a_Options) {
DataBuffer data = io::MessageEncapsulator<TOption>::Decapsulate(a_Data, a_Option);
return MessageEncapsulatorPack<TOptions...>::Decapsulate(data, 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, 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(
Stream<IOTag, MessageDispatcher, MessageFactory, TOptions...>&& a_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) {
DataBuffer data = a_Message.Write();
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() {
while (true) {
// reading the first VarInt part byte by byte
std::uint64_t lenghtValue = 0;
unsigned int readPos = 0;
while (true) {
static constexpr int SEGMENT_BITS = (1 << 7) - 1;
static constexpr int CONTINUE_BIT = 1 << 7;
DataBuffer buffer = m_Interface.Read(sizeof(std::uint8_t));
// eof
if (buffer.GetSize() == 0)
return;
std::uint8_t part;
buffer >> part;
lenghtValue |= static_cast<std::uint64_t>(part & SEGMENT_BITS) << readPos;
if ((part & CONTINUE_BIT) == 0)
break;
readPos += 7;
if (readPos >= 8 * sizeof(lenghtValue))
throw std::runtime_error("VarInt is too big");
}
// nothing to read
if (lenghtValue == 0)
return;
DataBuffer buffer;
buffer = m_Interface.Read(lenghtValue);
buffer = std::apply([&buffer, lenghtValue](const auto&... a_Options) {
return Decapsulate(buffer, a_Options...);
}, m_Options);
VarInt packetType;
buffer >> packetType;
static const MessageFactory messageFactory;
std::unique_ptr<MessageBase> message = messageFactory.CreateMessage(packetType.GetValue());
assert(message != nullptr);
message->Read(buffer);
GetDispatcher().Dispatch(*message);
}
}
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, const TOptions&... a_Options) {
return details::MessageEncapsulatorPack<TOptions...>::Decapsulate(a_Data, a_Options...);
}
} // namespace io
} // namespace sp

View File

@@ -1,23 +0,0 @@
#pragma once
#include <sp/io/IOInterface.h>
namespace sp {
namespace io {
struct MemoryTag {};
template <>
class IOInterface<MemoryTag> {
private:
sp::DataBuffer m_VirtualIO;
public:
sp::DataBuffer Read(std::size_t a_Amount);
void Write(const sp::DataBuffer& a_Data);
};
using Memory = IOInterface<MemoryTag>;
} // namespace io
} // namespace sp

View File

@@ -5,10 +5,10 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <sp/protocol/message/ArrayFillerImpl.h>
namespace sp { namespace sp {
#include <sp/protocol/message/ArrayFillerImpl.inl>
template <typename TBase, typename TTMessages> template <typename TBase, typename TTMessages>
class MessageFactory { class MessageFactory {
public: public:

View File

@@ -1,12 +1,12 @@
#pragma once #pragma once
namespace sp {
namespace details { namespace details {
template <typename TBase> template <typename TBase>
using ArrayType = std::vector<std::function<std::unique_ptr<TBase>(void)>>; using ArrayType = std::vector<std::function<std::unique_ptr<TBase>(void)>>;
template <typename TBase, typename... TMessages> template <typename TBase, typename... TMessages>
struct ArrayFiller {}; struct ArrayFiller {};
@@ -36,4 +36,3 @@ struct ArrayFiller<TBase, TMessage> {
}; };
} // namespace details } // namespace details
} // namespace sp

View File

@@ -117,7 +117,7 @@ template <typename TBase>
class MessageInterfaceWriteIdBase : public TBase { class MessageInterfaceWriteIdBase : public TBase {
public: public:
void Write(DataBuffer& buffer) const { void Write(DataBuffer& buffer) const {
buffer << VarInt{this->GetId()}; this->WriteData(this->GetId(), buffer);
this->WriteImpl(buffer); this->WriteImpl(buffer);
} }

View File

@@ -4,6 +4,8 @@
#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 {
@@ -11,8 +13,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(static_cast<Bytef*>(result.data()), static_cast<uLongf*>(&uncompressedSize), static_cast<const Bytef*>(source), uncompress(reinterpret_cast<Bytef*>(result.data()), reinterpret_cast<uLongf*>(&uncompressedSize),
static_cast<uLong>(size)); reinterpret_cast<const Bytef*>(source), static_cast<uLong>(size));
assert(result.GetSize() == uncompressedSize); assert(result.GetSize() == uncompressedSize);
return result; return result;
@@ -23,34 +25,35 @@ 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(static_cast<Bytef*>(result.data()), &compressedSize, static_cast<const Bytef*>(source), static_cast<uLong>(size)); compress(
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, std::size_t a_CompressionThreshold) { DataBuffer Compress(const DataBuffer& buffer) {
DataBuffer packet; DataBuffer packet;
if (buffer.GetSize() < a_CompressionThreshold) { if (buffer.GetSize() < COMPRESSION_THRESHOLD) {
// Don't compress since it's a small packet // Don't compress since it's a small packet
packet << VarInt{0}; VarInt compressedDataLength = 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();
if (compressedData.GetSize() >= buffer.GetSize()) { packet << packetLength;
// the compression is overkill so we don't send the compressed buffer
packet << VarInt{0};
packet << buffer;
} else {
packet << uncompressedDataLength; packet << uncompressedDataLength;
packet << compressedData; packet.WriteSome(compressedData.data(), compressedData.GetSize());
}
return packet; return packet;
} }
@@ -72,18 +75,12 @@ 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) {
std::uint64_t packetLength;
buffer >> packetLength;
return Decompress(buffer, packetLength);
}
} // namespace zlib } // namespace zlib
namespace io {
DataBuffer MessageEncapsulator<option::ZlibCompress>::Encapsulate(const DataBuffer& a_Data, const option::ZlibCompress& a_Option) {
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, const option::ZlibCompress& a_Option) {
return zlib::Decompress(a_Data, a_Data.GetSize());
}
} // namespace io
} // namespace sp } // namespace sp

View File

@@ -1,3 +0,0 @@
#include <sp/extensions/Encrypt.h>
#include <openssl/ssl.h>

View File

@@ -1,31 +0,0 @@
#include <sp/io/File.h>
namespace sp {
namespace io {
File::IOInterface(const std::string& a_FilePath, unsigned int a_OpenMode) {
if (a_OpenMode & FileTag::OpenMode::In)
m_FileInput = std::make_unique<std::ifstream>(a_FilePath, std::ios::binary);
if (a_OpenMode & FileTag::OpenMode::Out)
m_FileOutput = std::make_unique<std::ofstream>(a_FilePath, std::ios::binary);
}
File::IOInterface(File&& other) :
m_FileOutput(std::move(other.m_FileOutput)), m_FileInput(std::move(other.m_FileInput)) {}
DataBuffer File::Read(std::size_t a_Amount) {
DataBuffer buffer;
buffer.Resize(a_Amount);
assert(m_FileInput != nullptr);
m_FileInput->read(reinterpret_cast<char*>(buffer.data()), a_Amount);
return buffer;
}
void File::Write(const sp::DataBuffer& a_Data) {
assert(m_FileOutput != nullptr);
m_FileOutput->write(reinterpret_cast<const char*>(a_Data.data()), a_Data.GetSize());
m_FileOutput->flush();
}
} // namespace io
} // namespace sp

View File

@@ -1,16 +0,0 @@
#include <sp/io/Memory.h>
namespace sp {
namespace io {
sp::DataBuffer Memory::Read(std::size_t a_Amount) {
DataBuffer data;
m_VirtualIO.ReadSome(data, a_Amount > m_VirtualIO.GetRemaining() ? m_VirtualIO.GetRemaining() : a_Amount);
return data;
}
void Memory::Write(const sp::DataBuffer& a_Data) {
m_VirtualIO << a_Data;
}
} // namespace io
} // namespace sp

View File

@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <examples/PacketExample.h> #include <examples/PacketExample.h>
#include <sp/io/File.h> #include <sp/io/FileIO.h>
class CustomPacketHandler : public sp::PacketHandler { class CustomPacketHandler : public sp::PacketHandler {
void Handle(const KeepAlivePacket& packet) { void Handle(const KeepAlivePacket& packet) {
@@ -17,24 +17,19 @@ class CustomPacketHandler : public sp::PacketHandler {
} }
}; };
using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::PacketFactory, sp::option::ZlibCompress>; using FileStream = sp::IOStream<sp::FileTag, sp::PacketDispatcher, sp::PacketFactory>;
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::FileIO{"test.txt", "text.txt"});
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}); stream.SendMessage(KeepAlivePacket{96});
stream.SendMessage(KeepAlivePacket{69}); stream.SendMessage(KeepAlivePacket{69});
stream.SendMessage(DisconnectPacket{ stream.SendMessage(DisconnectPacket{"This is in the file !"});
"This is in the "
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
stream.GetOption<sp::option::ZlibCompress>().m_Enabled = false;
stream.SendMessage(DisconnectPacket{
"This is in the "
"fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiile !"});
stream.RecieveMessages(); stream.RecieveMessages();

View File

@@ -1,9 +1,31 @@
#include <iostream> #include <iostream>
#include <examples/PacketExample.h> #include <examples/PacketExample.h>
#include <sp/io/Memory.h> #include <sp/io/IOInterface.h>
using DataBufferStream = sp::io::Stream<sp::io::MemoryTag, sp::PacketDispatcher, sp::PacketFactory>; struct DBTag {};
template <>
class sp::IOInterface<DBTag> {
private:
sp::DataBuffer m_VirtualIO;
public:
sp::DataBuffer Read(std::size_t a_Amount) {
// since we are just testing it, we ignore reads that overflows
if (m_VirtualIO.GetReadOffset() + a_Amount > m_VirtualIO.GetSize())
return {};
DataBuffer data;
m_VirtualIO.ReadSome(data, a_Amount);
return data;
}
void Write(const sp::DataBuffer& a_Data) {
m_VirtualIO << a_Data;
}
};
using DataBufferStream = sp::IOStream<DBTag, sp::PacketDispatcher, sp::PacketFactory>;
class CustomPacketHandler : public sp::PacketHandler { class CustomPacketHandler : public sp::PacketHandler {
void Handle(const KeepAlivePacket& packet) { void Handle(const KeepAlivePacket& packet) {
@@ -32,9 +54,9 @@ int main() {
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler); stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
stream.SendMessage(KeepAlivePacket{69}); stream.SendMessage(KeepAlivePacket{69});
stream.RecieveMessages(); stream.RecieveMessages(); // here, it's non-blocking
stream.SendMessage(DisconnectPacket{"A valid reason"}); stream.SendMessage(DisconnectPacket{"I don't know"});
stream.RecieveMessages(); stream.RecieveMessages(); // here, it's non-blocking
return 0; return 0;
} }

View File

@@ -6,14 +6,9 @@ local modules = {
Compression = { Compression = {
Option = "zlib", Option = "zlib",
Deps = {"zlib"}, Deps = {"zlib"},
Packages = {"zlib"},
Includes = {"include/(sp/extensions/Compress.h)"}, Includes = {"include/(sp/extensions/Compress.h)"},
Sources = {"src/sp/extensions/Compress.cpp"} Sources = {"src/sp/extensions/Compress.cpp"}
},
Encryption = {
Option = "ssl",
Deps = {"openssl"},
Includes = {"include/(sp/extensions/Encrypt.h)"},
Sources = {"src/sp/extensions/Encrypt.cpp"}
} }
} }
@@ -34,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("SimpleProtocol-" .. name) target("SimpleProtocolLib-" .. 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)
@@ -42,7 +37,7 @@ for name, module in table.orderpairs(modules) do
for _, source in table.orderpairs(module.Sources) do for _, source in table.orderpairs(module.Sources) do
add_files(source) add_files(source)
end end
for _, package in table.orderpairs(module.Deps) do for _, package in table.orderpairs(module.Packages) do
add_packages(package) add_packages(package)
end end
set_group("Library") set_group("Library")
@@ -50,25 +45,11 @@ for name, module in table.orderpairs(modules) do
end end
end end
target("SimpleProtocol") target("SimpleProtocolLib")
add_includedirs("include") add_includedirs("include")
add_files("src/sp/**.cpp") add_headerfiles("include/(sp/common/**.h)", "include/(sp/common/**.h)", "include/(sp/common/**.h)")
local includeFolders = {"common", "default", "io", "protocol"}
for _, folder in ipairs(includeFolders) do
add_headerfiles("include/(sp/" .. folder .. "/**.h)")
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
remove_files("src/sp/extensions/**.cpp")
set_group("Library") set_group("Library")
add_files("src/sp/common/*.cpp")
set_kind("$(kind)") set_kind("$(kind)")
-- Tests -- Tests
@@ -80,7 +61,7 @@ for _, file in ipairs(os.files("test/**.cpp")) do
add_files(file) add_files(file)
add_includedirs("include") add_includedirs("include")
add_deps("SimpleProtocol") add_deps("SimpleProtocolLib")
add_tests("compile_and_run") add_tests("compile_and_run")
end end