Add generic IO (#3)

Reviewed-on: #3
Co-authored-by: Persson-dev <sim16.prib@gmail.com>
Co-committed-by: Persson-dev <sim16.prib@gmail.com>
This commit was merged in pull request #3.
This commit is contained in:
2025-02-26 09:29:31 +00:00
committed by Simon Pribylski
parent 8a5286d0ce
commit 03d799e064
15 changed files with 338 additions and 15 deletions

33
include/sp/io/File.h Normal file
View File

@@ -0,0 +1,33 @@
#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

View File

@@ -0,0 +1,41 @@
#pragma once
#include <memory>
#include <sp/common/DataBuffer.h>
namespace sp {
namespace io {
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 Stream {
protected:
MessageDispatcher m_Dispatcher;
IOInterface<IOTag> m_Interface;
using MessageBase = typename MessageDispatcher::MessageBaseType;
using MsgIdType = typename MessageBase::MsgIdType;
public:
Stream() {}
Stream(IOInterface<IOTag>&& a_Interface);
Stream(Stream&& a_Stream);
void RecieveMessages();
void SendMessage(const MessageBase& a_Message);
MessageDispatcher& GetDispatcher() {
return m_Dispatcher;
}
};
} // namespace io
} // namespace sp
#include <sp/io/IOInterfaceImpl.inl>

View File

@@ -0,0 +1,78 @@
#pragma once
#include <stdexcept>
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>
Stream<IOTag, MessageDispatcher, MessageFactory>::Stream(Stream<IOTag, MessageDispatcher, MessageFactory>&& 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>
void Stream<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 Stream<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));
// 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);
// 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 io
} // namespace sp

23
include/sp/io/Memory.h Normal file
View File

@@ -0,0 +1,23 @@
#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

@@ -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

@@ -5,9 +5,9 @@
#include <iostream>
#include <memory>
namespace sp {
#include <sp/protocol/message/ArrayFillerImpl.h>
#include <sp/protocol/message/ArrayFillerImpl.inl>
namespace sp {
template <typename TBase, typename TTMessages>
class MessageFactory {
@@ -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

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

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