refactor io

This commit is contained in:
2025-02-26 10:14:06 +01:00
parent b77c650fdf
commit 605a310ede
9 changed files with 190 additions and 140 deletions

View File

@@ -1,31 +1,9 @@
#include <iostream>
#include <examples/PacketExample.h>
#include <sp/io/IOInterface.h>
#include <sp/io/Memory.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>;
using DataBufferStream = sp::io::Stream<sp::io::MemoryTag, sp::PacketDispatcher, sp::PacketFactory>;
class CustomPacketHandler : public sp::PacketHandler {
void Handle(const KeepAlivePacket& packet) {
@@ -54,9 +32,9 @@ int main() {
stream.GetDispatcher().RegisterHandler(PacketId::KeepAlive, handler);
stream.SendMessage(KeepAlivePacket{69});
stream.RecieveMessages(); // here, it's non-blocking
stream.RecieveMessages();
stream.SendMessage(DisconnectPacket{"I don't know"});
stream.RecieveMessages(); // here, it's non-blocking
stream.RecieveMessages();
return 0;
}