add base io interface

This commit is contained in:
2025-02-25 23:25:12 +01:00
parent 8a5286d0ce
commit 132c3c3c8d
8 changed files with 187 additions and 9 deletions

View File

@@ -6,12 +6,13 @@ enum PacketId {
UpgradeTower, UpgradeTower,
}; };
#include <examples/KeepAlivePacket.h>
#include <examples/DisconnectPacket.h> #include <examples/DisconnectPacket.h>
#include <examples/KeepAlivePacket.h>
#include <examples/UpgradeTowerPacket.h> #include <examples/UpgradeTowerPacket.h>
// they must be in the same order as in the enum ! // they must be in the same order as in the enum !
using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket, UpgradeTowerPacket>; using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket, UpgradeTowerPacket>;
#include <sp/default/DefaultPacketHandler.h> #include <sp/default/DefaultPacketDispatcher.h>
#include <sp/default/DefaultPacketFactory.h> #include <sp/default/DefaultPacketFactory.h>
#include <sp/default/DefaultPacketHandler.h>

View File

@@ -0,0 +1,98 @@
#pragma once
#include <memory>
#include <sp/common/DataBuffer.h>
namespace sp {
template <typename IOTag>
class IOInterface {
public:
DataBuffer Read(std::size_t a_Amount);
void Write(const DataBuffer& a_Data);
};
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
class IOStream {
protected:
MessageDispatcher m_Dispatcher;
IOInterface<IOTag> m_Interface;
using MessageBase = typename MessageDispatcher::MessageBaseType;
using MsgIdType = typename MessageBase::MsgIdType;
public:
IOStream() {}
IOStream(IOInterface<IOTag>&& a_Interface) : m_Interface(std::move(a_Interface)) {}
IOStream(IOStream&& a_Stream) : m_Dispatcher(std::move(a_Stream.m_Dispatcher)), m_Interface(std::move(a_Stream.m_Interface)) {}
void RecieveMessages();
void SendMessage(const MessageBase& a_Message);
MessageDispatcher& GetDispatcher() {
return m_Dispatcher;
}
};
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
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);
}
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");
}
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

@@ -0,0 +1 @@
#pragma once

View File

@@ -6,6 +6,7 @@
*/ */
#include <map> #include <map>
#include <memory>
namespace sp { namespace sp {
@@ -19,6 +20,8 @@ class MessageDispatcher {
std::map<MessageIdType, std::vector<std::shared_ptr<MessageHandler>>> m_Handlers; std::map<MessageIdType, std::vector<std::shared_ptr<MessageHandler>>> m_Handlers;
public: public:
using MessageBaseType = MessageBase;
/** /**
* \brief Constructor * \brief Constructor
*/ */
@@ -51,4 +54,4 @@ class MessageDispatcher {
#include <sp/protocol/message/MessageDispatcherImpl.inl> #include <sp/protocol/message/MessageDispatcherImpl.inl>
} // namespace blitz } // namespace sp

View File

@@ -16,7 +16,7 @@ class MessageFactory {
MessageFactory() : m_Factory(details::ArrayFiller<TBase, TTMessages>::ArrayCreate()) {} MessageFactory() : m_Factory(details::ArrayFiller<TBase, TTMessages>::ArrayCreate()) {}
std::unique_ptr<TBase> CreateMessage(IdType id) { std::unique_ptr<TBase> CreateMessage(IdType id) const {
if (id >= m_Factory.size()) if (id >= m_Factory.size())
return nullptr; return nullptr;
return m_Factory.at(id)(); return m_Factory.at(id)();

View File

@@ -75,6 +75,13 @@ class MessageInterfaceWriteBase : public TBase {
WriteImpl(buffer); WriteImpl(buffer);
} }
// helper
DataBuffer Write() const {
DataBuffer buffer;
this->Write(buffer);
return buffer;
}
protected: protected:
virtual void WriteImpl(DataBuffer& buffer) const = 0; virtual void WriteImpl(DataBuffer& buffer) const = 0;
}; };
@@ -113,6 +120,13 @@ class MessageInterfaceWriteIdBase : public TBase {
this->WriteData(this->GetId(), buffer); this->WriteData(this->GetId(), buffer);
this->WriteImpl(buffer); this->WriteImpl(buffer);
} }
// helper
DataBuffer Write() const {
DataBuffer buffer;
this->Write(buffer);
return buffer;
}
}; };
} // namespace details } // namespace details

62
test/test_io.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include <iostream>
#include <examples/PacketExample.h>
#include <sp/io/IOInterface.h>
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 {
void Handle(const KeepAlivePacket& packet) {
std::cout << "KeepAlive handled ! " << packet.GetKeepAliveId() << "\n";
}
void Handle(const DisconnectPacket& packet) {
std::cout << "Disconnect handled ! " << packet.GetReason() << "\n";
}
void Handle(const UpgradeTowerPacket& packet) {
std::cout << "UpgradeTower handled !\n";
}
};
int main() {
auto handler = std::make_shared<CustomPacketHandler>();
DataBufferStream stream;
stream.GetDispatcher().RegisterHandler(PacketId::Disconnect, handler);
// this should not be dispatched
stream.SendMessage(KeepAlivePacket{96});
stream.RecieveMessages();
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
stream.SendMessage(KeepAlivePacket{69});
stream.RecieveMessages(); // here, it's non-blocking
stream.SendMessage(DisconnectPacket{"I don't know"});
stream.RecieveMessages(); // here, it's non-blocking
return 0;
}

View File

@@ -3,8 +3,8 @@
#include <examples/PacketExample.h> #include <examples/PacketExample.h>
#include <memory> #include <memory>
#include <sp/extensions/Extensions.h>
#include <sp/default/DefaultPacketDispatcher.h> #include <sp/default/DefaultPacketDispatcher.h>
#include <sp/extensions/Extensions.h>
class KeepAliveHandler : public sp::PacketHandler { class KeepAliveHandler : public sp::PacketHandler {
void Handle(const KeepAlivePacket& packet) { void Handle(const KeepAlivePacket& packet) {
@@ -28,8 +28,7 @@ int main() {
auto handler = std::make_shared<KeepAliveHandler>(); auto handler = std::make_shared<KeepAliveHandler>();
msg->Dispatch(*handler); msg->Dispatch(*handler);
sp::DataBuffer buffer; sp::DataBuffer buffer = msg->Write();
msg->Write(buffer);
std::uint8_t msgId; std::uint8_t msgId;
buffer >> msgId; buffer >> msgId;