]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/protocol.h
Add a dynamic receiver class for more flexible packet handling
[libs/net.git] / source / net / protocol.h
index 248b97ea5a1d14cb4fde4589ccaf355685370019..5051338f3911558c8e2ee19fe2d0665c0e10eec3 100644 (file)
@@ -147,6 +147,7 @@ private:
                virtual unsigned get_class_id() const = 0;
                unsigned get_id() const { return id; }
                virtual std::uint64_t get_hash() const = 0;
+               virtual const char *deserialize(Variant &, const char *, const char *) const = 0;
                virtual const char *dispatch(ReceiverBase &, const char *, const char *) const = 0;
        };
 
@@ -169,6 +170,7 @@ private:
                std::uint64_t get_hash() const override { return serializer->get_hash(); }
                char *serialize(const P &, char *, char *) const;
                const char *deserialize(P &, const char *, const char *) const;
+               const char *deserialize(Variant &, const char *, const char *) const override;
                const char *dispatch(ReceiverBase &, const char *, const char *) const override;
        };
 
@@ -191,12 +193,12 @@ private:
        };
 
        PacketTypeDef<PacketHeader> header_def;
-       unsigned next_packet_id;
+       unsigned next_packet_id = 1;
        std::map<unsigned, std::unique_ptr<PacketDefBase>> packet_class_defs;
        std::map<unsigned, PacketDefBase *> packet_id_defs;
 
 protected:
-       Protocol(unsigned = 1);
+       Protocol();
 
 private:
        static unsigned get_next_packet_class_id();
@@ -207,12 +209,6 @@ private:
        void add_packet(std::unique_ptr<PacketDefBase>);
 
 protected:
-       template<typename P>
-       PacketDefBuilder<P, Serializer<P>> add(unsigned);
-
-       template<typename P, typename T, typename... Rest>
-       auto add(unsigned id, T P::*field, Rest P::*...rest) { return add<P>(id).fields(field, rest...); }
-
        template<typename P>
        PacketDefBuilder<P, Serializer<P>> add();
 
@@ -253,20 +249,14 @@ unsigned Protocol::get_packet_class_id()
 }
 
 template<typename P>
-Protocol::PacketDefBuilder<P, Protocol::Serializer<P>> Protocol::add(unsigned id)
+Protocol::PacketDefBuilder<P, Protocol::Serializer<P>> Protocol::add()
 {
-       std::unique_ptr<PacketTypeDef<P>> pdef = std::make_unique<PacketTypeDef<P>>(id);
+       std::unique_ptr<PacketTypeDef<P>> pdef = std::make_unique<PacketTypeDef<P>>(next_packet_id++);
        PacketDefBuilder<P, Serializer<P>> next(*this, *pdef, Serializer<P>());
        add_packet(move(pdef));
        return next;
 }
 
-template<typename P>
-Protocol::PacketDefBuilder<P, Protocol::Serializer<P>> Protocol::add()
-{
-       return add<P>(next_packet_id++);
-}
-
 template<typename P>
 const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
 {
@@ -443,6 +433,15 @@ const char *Protocol::PacketTypeDef<P>::deserialize(P &pkt, const char *buf, con
        return serializer->deserialize(pkt, buf, end);
 }
 
+template<typename P>
+const char *Protocol::PacketTypeDef<P>::deserialize(Variant &var_pkt, const char *buf, const char *end) const
+{
+       P pkt;
+       const char *ptr = serializer->deserialize(pkt, buf, end);
+       var_pkt = std::move(pkt);
+       return ptr;
+}
+
 template<typename P>
 const char *Protocol::PacketTypeDef<P>::dispatch(ReceiverBase &rcv, const char *buf, const char *end) const
 {