Add generic IO #3
35
include/sp/io/FileIO.h
Normal file
35
include/sp/io/FileIO.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <sp/io/IOInterface.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
|
||||||
|
struct FileTag {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class IOInterface<FileTag> {
|
||||||
|
private:
|
||||||
|
std::ofstream m_FileOutput;
|
||||||
|
std::ifstream m_FileInput;
|
||||||
|
|
||||||
|
public:
|
||||||
|
IOInterface(const std::string& filePath) : m_FileOutput(filePath), m_FileInput(filePath) {}
|
||||||
|
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);
|
||||||
|
m_FileInput.read(reinterpret_cast<char*>(buffer.data()), a_Amount);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Write(const sp::DataBuffer& a_Data) {
|
||||||
|
m_FileOutput.write(reinterpret_cast<const char*>(a_Data.data()), a_Data.GetSize());
|
||||||
|
m_FileOutput.flush();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using FileIO = IOInterface<FileTag>;
|
||||||
|
|
||||||
|
} // namespace sp
|
||||||
@@ -74,6 +74,9 @@ void IOStream<IOTag, MessageDispatcher, MessageFactory>::RecieveMessages() {
|
|||||||
throw std::runtime_error("VarInt is too big");
|
throw std::runtime_error("VarInt is too big");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nothing to read
|
||||||
|
if (lenghtValue == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
DataBuffer buffer;
|
DataBuffer buffer;
|
||||||
buffer = m_Interface.Read(lenghtValue);
|
buffer = m_Interface.Read(lenghtValue);
|
||||||
|
|||||||
37
test/test_file.cpp
Normal file
37
test/test_file.cpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <examples/PacketExample.h>
|
||||||
|
#include <sp/io/FileIO.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::IOStream<sp::FileTag, sp::PacketDispatcher, sp::PacketFactory>;
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
auto handler = std::make_shared<CustomPacketHandler>();
|
||||||
|
|
||||||
|
FileStream stream(sp::FileIO{"test.txt"});
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user