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:
@@ -6,12 +6,13 @@ enum PacketId {
|
||||
UpgradeTower,
|
||||
};
|
||||
|
||||
#include <examples/KeepAlivePacket.h>
|
||||
#include <examples/DisconnectPacket.h>
|
||||
#include <examples/KeepAlivePacket.h>
|
||||
#include <examples/UpgradeTowerPacket.h>
|
||||
|
||||
// they must be in the same order as in the enum !
|
||||
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/DefaultPacketHandler.h>
|
||||
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
|
||||
41
include/sp/io/IOInterface.h
Normal file
41
include/sp/io/IOInterface.h
Normal 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>
|
||||
78
include/sp/io/IOInterfaceImpl.inl
Normal file
78
include/sp/io/IOInterfaceImpl.inl
Normal 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
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
|
||||
@@ -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
|
||||
|
||||
@@ -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)();
|
||||
|
||||
@@ -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 {};
|
||||
|
||||
@@ -36,3 +36,4 @@ struct ArrayFiller<TBase, TMessage> {
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
@@ -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
|
||||
|
||||
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
|
||||
36
test/test_file.cpp
Normal file
36
test/test_file.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <examples/PacketExample.h>
|
||||
#include <sp/io/File.h>
|
||||
|
||||
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";
|
||||
}
|
||||
};
|
||||
|
||||
using FileStream = sp::io::Stream<sp::io::FileTag, sp::PacketDispatcher, sp::PacketFactory>;
|
||||
|
||||
int main() {
|
||||
auto handler = std::make_shared<CustomPacketHandler>();
|
||||
|
||||
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::KeepAlive, handler);
|
||||
|
||||
stream.SendMessage(KeepAlivePacket{96});
|
||||
stream.SendMessage(KeepAlivePacket{69});
|
||||
stream.SendMessage(DisconnectPacket{"This is in the file !"});
|
||||
|
||||
stream.RecieveMessages();
|
||||
|
||||
return 0;
|
||||
}
|
||||
40
test/test_io.cpp
Normal file
40
test/test_io.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <examples/PacketExample.h>
|
||||
#include <sp/io/Memory.h>
|
||||
|
||||
using DataBufferStream = sp::io::Stream<sp::io::MemoryTag, 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();
|
||||
stream.SendMessage(DisconnectPacket{"I don't know"});
|
||||
stream.RecieveMessages();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -3,8 +3,8 @@
|
||||
#include <examples/PacketExample.h>
|
||||
#include <memory>
|
||||
|
||||
#include <sp/extensions/Extensions.h>
|
||||
#include <sp/default/DefaultPacketDispatcher.h>
|
||||
#include <sp/extensions/Extensions.h>
|
||||
|
||||
class KeepAliveHandler : public sp::PacketHandler {
|
||||
void Handle(const KeepAlivePacket& packet) {
|
||||
@@ -28,8 +28,7 @@ int main() {
|
||||
auto handler = std::make_shared<KeepAliveHandler>();
|
||||
msg->Dispatch(*handler);
|
||||
|
||||
sp::DataBuffer buffer;
|
||||
msg->Write(buffer);
|
||||
sp::DataBuffer buffer = msg->Write();
|
||||
|
||||
std::uint8_t msgId;
|
||||
buffer >> msgId;
|
||||
@@ -37,7 +36,7 @@ int main() {
|
||||
auto upgradeTower2 = std::make_unique<UpgradeTowerPacket>();
|
||||
upgradeTower2->Read(buffer);
|
||||
|
||||
std::cout << "Test : " << (unsigned) upgradeTower2->GetTowerId() << "\n";
|
||||
std::cout << "Test : " << (unsigned)upgradeTower2->GetTowerId() << "\n";
|
||||
|
||||
sp::PacketFactory factory;
|
||||
auto packet = factory.CreateMessage(msgId);
|
||||
|
||||
11
xmake.lua
11
xmake.lua
@@ -47,9 +47,16 @@ end
|
||||
|
||||
target("SimpleProtocolLib")
|
||||
add_includedirs("include")
|
||||
add_headerfiles("include/(sp/common/**.h)", "include/(sp/common/**.h)", "include/(sp/common/**.h)")
|
||||
add_files("src/sp/**.cpp")
|
||||
|
||||
local includeFolders = {"common", "default", "io", "protocol"}
|
||||
for _, folder in ipairs(includeFolders) do
|
||||
add_headerfiles("include/(sp/" .. folder .. "/**.h)")
|
||||
end
|
||||
|
||||
-- we don't want extensions
|
||||
remove_files("src/sp/extensions/**.cpp")
|
||||
set_group("Library")
|
||||
add_files("src/sp/common/*.cpp")
|
||||
set_kind("$(kind)")
|
||||
|
||||
-- Tests
|
||||
|
||||
Reference in New Issue
Block a user