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

@@ -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 <memory>
namespace sp {
@@ -19,6 +20,8 @@ class MessageDispatcher {
std::map<MessageIdType, std::vector<std::shared_ptr<MessageHandler>>> m_Handlers;
public:
using MessageBaseType = MessageBase;
/**
* \brief Constructor
*/
@@ -51,4 +54,4 @@ class MessageDispatcher {
#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()) {}
std::unique_ptr<TBase> CreateMessage(IdType id) {
std::unique_ptr<TBase> CreateMessage(IdType id) const {
if (id >= m_Factory.size())
return nullptr;
return m_Factory.at(id)();

View File

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