]> git.tdb.fi Git - libs/net.git/commitdiff
Provide access to packet IDs in Protcool
authorMikko Rasa <tdb@tdb.fi>
Sun, 15 Jan 2023 08:04:25 +0000 (10:04 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 15 Jan 2023 08:04:25 +0000 (10:04 +0200)
source/net/protocol.cpp
source/net/protocol.h

index c6112923c9692db7f4b646f11d68e0a8e6e753a0..358966f643152804ebc0e1b68a3c193179e2a4de 100644 (file)
@@ -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
index 0721ec5b5403adebb4013e54a07358700beebecc..248b97ea5a1d14cb4fde4589ccaf355685370019 100644 (file)
@@ -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<typename T, std::uint8_t K>
        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<PacketHeader> header_def;
        unsigned next_packet_id;
        std::map<unsigned, std::unique_ptr<PacketDefBase>> packet_class_defs;
@@ -225,9 +226,18 @@ protected:
        const PacketTypeDef<P> &get_packet_by_class() const;
 
 public:
+       template<typename P>
+       bool has_packet() const { return packet_class_defs.count(get_packet_class_id<P>()); }
+
+       template<typename P>
+       unsigned get_packet_id() const { return get_item(packet_class_defs, get_packet_class_id<P>())->get_id(); }
+
+       unsigned get_max_packet_id() const;
+
        template<typename P>
        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;