Add generic IO #3
33
include/sp/io/File.h
Normal file
33
include/sp/io/File.h
Normal 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
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
#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
|
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#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,7 +14,7 @@ class IOInterface {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
|
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
|
||||||
class IOStream {
|
class Stream {
|
||||||
protected:
|
protected:
|
||||||
MessageDispatcher m_Dispatcher;
|
MessageDispatcher m_Dispatcher;
|
||||||
IOInterface<IOTag> m_Interface;
|
IOInterface<IOTag> m_Interface;
|
||||||
@@ -22,9 +23,9 @@ class IOStream {
|
|||||||
using MsgIdType = typename MessageBase::MsgIdType;
|
using MsgIdType = typename MessageBase::MsgIdType;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IOStream() {}
|
Stream() {}
|
||||||
IOStream(IOInterface<IOTag>&& a_Interface) : m_Interface(std::move(a_Interface)) {}
|
Stream(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)) {}
|
Stream(Stream&& 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);
|
||||||
@@ -34,68 +35,7 @@ class IOStream {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename IOTag, typename MessageDispatcher, typename MessageFactory>
|
} // namespace io
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
} // namespace sp
|
||||||
|
|
||||||
|
#include <sp/io/IOInterfaceImpl.inl>
|
||||||
@@ -1 +1,73 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
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
23
include/sp/io/Memory.h
Normal 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
|
||||||
31
src/sp/io/File.cpp
Normal file
31
src/sp/io/File.cpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#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
|
||||||
16
src/sp/io/Memory.cpp
Normal file
16
src/sp/io/Memory.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#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
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <examples/PacketExample.h>
|
#include <examples/PacketExample.h>
|
||||||
#include <sp/io/FileIO.h>
|
#include <sp/io/File.h>
|
||||||
|
|
||||||
class CustomPacketHandler : public sp::PacketHandler {
|
class CustomPacketHandler : public sp::PacketHandler {
|
||||||
void Handle(const KeepAlivePacket& packet) {
|
void Handle(const KeepAlivePacket& packet) {
|
||||||
@@ -17,13 +17,12 @@ class CustomPacketHandler : public sp::PacketHandler {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
using FileStream = sp::IOStream<sp::FileTag, sp::PacketDispatcher, sp::PacketFactory>;
|
using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::PacketFactory>;
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
auto handler = std::make_shared<CustomPacketHandler>();
|
auto handler = std::make_shared<CustomPacketHandler>();
|
||||||
|
|
||||||
FileStream stream(sp::FileIO{"test.txt", "text.txt"});
|
FileStream stream(sp::io::File{"test.txt", sp::io::FileTag::In | sp::io::FileTag::Out});
|
||||||
stream.GetDispatcher().RegisterHandler(PacketId::Disconnect, handler);
|
stream.GetDispatcher().RegisterHandler(PacketId::Disconnect, handler);
|
||||||
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
|
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +1,9 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <examples/PacketExample.h>
|
#include <examples/PacketExample.h>
|
||||||
#include <sp/io/IOInterface.h>
|
#include <sp/io/Memory.h>
|
||||||
|
|
||||||
struct DBTag {};
|
using DataBufferStream = sp::io::Stream<sp::io::MemoryTag, sp::PacketDispatcher, sp::PacketFactory>;
|
||||||
|
|
||||||
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) {
|
||||||
@@ -54,9 +32,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(); // here, it's non-blocking
|
stream.RecieveMessages();
|
||||||
stream.SendMessage(DisconnectPacket{"I don't know"});
|
stream.SendMessage(DisconnectPacket{"I don't know"});
|
||||||
stream.RecieveMessages(); // here, it's non-blocking
|
stream.RecieveMessages();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user