From: Mikko Rasa Date: Sun, 15 Jan 2023 08:04:25 +0000 (+0200) Subject: Provide access to packet IDs in Protcool X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=449a2f3417748761f94f3002b1c15819c4d83365;p=libs%2Fnet.git Provide access to packet IDs in Protcool --- diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index c611292..358966f 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -44,6 +44,13 @@ const Protocol::PacketDefBase &Protocol::get_packet_by_id(unsigned id) const return *get_item(packet_id_defs, id); } +unsigned Protocol::get_max_packet_id() const +{ + if(packet_id_defs.empty()) + return 0; + return prev(packet_id_defs.end())->first; +} + size_t Protocol::dispatch(ReceiverBase &rcv, const char *buf, size_t size, unsigned base_id) const { PacketHeader header; @@ -55,13 +62,18 @@ size_t Protocol::dispatch(ReceiverBase &rcv, const char *buf, size_t size, unsig return ptr-buf; } -size_t Protocol::get_packet_size(const char *buf, size_t size) const +bool Protocol::get_packet_header(PacketHeader &header, const char *buf, size_t size) const { if(size<4) - return 0; - PacketHeader header; + return false; header_def.deserialize(header, buf, buf+size); - return header.length; + return true; +} + +size_t Protocol::get_packet_size(const char *buf, size_t size) const +{ + PacketHeader header; + return (get_packet_header(header, buf, size) ? header.length : 0); } uint64_t Protocol::get_hash() const diff --git a/source/net/protocol.h b/source/net/protocol.h index 0721ec5..248b97e 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -29,6 +29,16 @@ public: class MSPNET_API Protocol { +public: + struct PacketHeader + { + std::uint16_t type = 0; + std::uint16_t length = 0; + + PacketHeader() = default; + PacketHeader(std::uint16_t, std::uint16_t); + }; + private: template struct BasicTraits; @@ -180,15 +190,6 @@ private: auto fields(T1 P::*first, T2 P::*second, Rest P::*...rest) { return fields(first).fields(second, rest...); } }; - struct PacketHeader - { - std::uint16_t type = 0; - std::uint16_t length = 0; - - PacketHeader() = default; - PacketHeader(std::uint16_t, std::uint16_t); - }; - PacketTypeDef header_def; unsigned next_packet_id; std::map> packet_class_defs; @@ -225,9 +226,18 @@ protected: const PacketTypeDef

&get_packet_by_class() const; public: + template + bool has_packet() const { return packet_class_defs.count(get_packet_class_id

()); } + + template + unsigned get_packet_id() const { return get_item(packet_class_defs, get_packet_class_id

())->get_id(); } + + unsigned get_max_packet_id() const; + template std::size_t serialize(const P &, char *, std::size_t, unsigned = 0) const; + bool get_packet_header(PacketHeader &, const char *, std::size_t) const; std::size_t get_packet_size(const char *, std::size_t) const; std::size_t dispatch(ReceiverBase &, const char *, std::size_t, unsigned = 0) const;