]> git.tdb.fi Git - libs/net.git/commitdiff
Support compound types in network packets
authorMikko Rasa <tdb@tdb.fi>
Sun, 9 Oct 2016 17:30:03 +0000 (20:30 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 9 Oct 2016 17:30:03 +0000 (20:30 +0300)
source/net/protocol.cpp
source/net/protocol.h

index 4d3f9628f9d1eddbc4a9bc81f9547bba6dbf153b..b24cef1f5e3f03ef067711271480c2eb1ab378cb 100644 (file)
@@ -29,9 +29,13 @@ void Protocol::add_packet(PacketDefBase *pdef)
 {
        PacketDefBase *&ptr = packet_class_defs[pdef->get_class_id()];
        if(ptr)
+       {
+               packet_id_defs.erase(ptr->get_id());
                delete ptr;
+       }
        ptr = pdef;
-       packet_id_defs[pdef->get_id()] = pdef;
+       if(unsigned id = pdef->get_id())
+               packet_id_defs[id] = pdef;
 }
 
 const Protocol::PacketDefBase &Protocol::get_packet_by_class_id(unsigned id) const
index f722a76c82a7cc73b83d4880b7ac8043f45bd889..0a8110c9518182f91ada3cfe48f0f09ecd579b98 100644 (file)
@@ -105,6 +105,23 @@ private:
                const char *deserialize(A &, const char *, const char *) const;
        };
 
+       template<typename C>
+       class CompoundSerializer
+       {
+       public:
+               typedef C ValueType;
+
+       private:
+               const CompoundTypeDef<C> &def;
+
+       public:
+               CompoundSerializer(const Protocol &);
+
+               std::string describe() const { return def.describe(); }
+               char *serialize(const C &, char *, char *) const;
+               const char *deserialize(C &, const char *, const char *) const;
+       };
+
        template<typename P, typename Head, typename S>
        class Serializer: public Head
        {
@@ -226,6 +243,9 @@ private:
        void add_packet(PacketDefBase *);
 
 protected:
+       template<typename P>
+       PacketDefBuilder<P, NullSerializer<P> > add(unsigned);
+
        template<typename P>
        PacketDefBuilder<P, NullSerializer<P> > add();
 
@@ -251,13 +271,19 @@ private:
 
 
 template<typename P>
-Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
+Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add(unsigned id)
 {
-       PacketTypeDef<P> *pdef = new PacketTypeDef<P>(next_packet_id++);
+       PacketTypeDef<P> *pdef = new PacketTypeDef<P>(id);
        add_packet(pdef);
        return PacketDefBuilder<P, NullSerializer<P> >(*this, *pdef, NullSerializer<P>());
 }
 
+template<typename P>
+Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
+{
+       return add<P>(next_packet_id++);
+}
+
 template<typename P>
 const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
 {
@@ -294,6 +320,13 @@ struct Protocol::BasicTraits
        typedef BasicSerializer<T> Serializer;
 };
 
+template<typename T>
+struct Protocol::Traits
+{
+       static const UInt16 signature = 'C';
+       typedef CompoundSerializer<T> Serializer;
+};
+
 template<> struct Protocol::Traits<Int8>: BasicTraits<Int8, 'I'> { };
 template<> struct Protocol::Traits<UInt8>: BasicTraits<UInt8, 'U'> { };
 template<> struct Protocol::Traits<Int16>: BasicTraits<Int16, 'I'> { };
@@ -383,6 +416,24 @@ const char *Protocol::ArraySerializer<A>::deserialize(A &array, const char *buf,
 }
 
 
+template<typename C>
+Protocol::CompoundSerializer<C>::CompoundSerializer(const Protocol &proto):
+       def(proto.get_packet_by_class<C>().get_compound())
+{ }
+
+template<typename C>
+char *Protocol::CompoundSerializer<C>::serialize(const C &com, char *buf, char *end) const
+{
+       return def.serialize(com, buf, end);
+}
+
+template<typename C>
+const char *Protocol::CompoundSerializer<C>::deserialize(C &com, const char *buf, const char *end) const
+{
+       return def.deserialize(com, buf, end);
+}
+
+
 template<typename P, typename Head, typename S>
 Protocol::Serializer<P, Head, S>::Serializer(const Head &h, Pointer p, const Protocol &proto):
        Head(h),