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

62
test/test_io.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include <iostream>
#include <examples/PacketExample.h>
#include <sp/io/IOInterface.h>
struct DBTag {};
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 {
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(); // here, it's non-blocking
stream.SendMessage(DisconnectPacket{"I don't know"});
stream.RecieveMessages(); // here, it's non-blocking
return 0;
}