From c2e9e03b191a6ffe44a83be32aadf2a325491c02 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 15 Jan 2023 09:36:07 +0200 Subject: [PATCH] Add a base id parameter to Protocol's serialization functions This allows multiplexing protocols on a single connection by externally adjusting their packet ID. --- source/net/protocol.cpp | 4 ++-- source/net/protocol.h | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index 63cc6f1..c611292 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -44,13 +44,13 @@ const Protocol::PacketDefBase &Protocol::get_packet_by_id(unsigned id) const return *get_item(packet_id_defs, id); } -size_t Protocol::dispatch(ReceiverBase &rcv, const char *buf, size_t size) const +size_t Protocol::dispatch(ReceiverBase &rcv, const char *buf, size_t size, unsigned base_id) const { PacketHeader header; const char *ptr = header_def.deserialize(header, buf, buf+size); if(header.length>size) throw bad_packet("truncated"); - const PacketDefBase &pdef = get_packet_by_id(header.type); + const PacketDefBase &pdef = get_packet_by_id(header.type-base_id); ptr = pdef.dispatch(rcv, ptr, ptr+header.length); return ptr-buf; } diff --git a/source/net/protocol.h b/source/net/protocol.h index 8068d32..0721ec5 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -226,10 +226,10 @@ protected: public: template - std::size_t serialize(const P &, char *, std::size_t) const; + std::size_t serialize(const P &, char *, std::size_t, unsigned = 0) const; std::size_t get_packet_size(const char *, std::size_t) const; - std::size_t dispatch(ReceiverBase &, const char *, std::size_t) const; + std::size_t dispatch(ReceiverBase &, const char *, std::size_t, unsigned = 0) const; std::uint64_t get_hash() const; }; @@ -265,14 +265,14 @@ const Protocol::PacketTypeDef

&Protocol::get_packet_by_class() const } template -std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const +std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size, unsigned base_id) const { const PacketTypeDef

&pdef = get_packet_by_class

(); if(!pdef.get_id()) throw std::invalid_argument("no packet id"); char *ptr = pdef.serialize(pkt, buf+4, buf+size); size = ptr-buf; - header_def.serialize(PacketHeader(pdef.get_id(), size), buf, buf+4); + header_def.serialize(PacketHeader(base_id+pdef.get_id(), size), buf, buf+4); return size; } -- 2.43.0