From: Mikko Rasa Date: Sun, 9 Oct 2016 17:30:03 +0000 (+0300) Subject: Support compound types in network packets X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c2d202f9b482be7b439436970d6366c369837107;p=libs%2Fnet.git Support compound types in network packets --- diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index 4d3f962..b24cef1 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -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 diff --git a/source/net/protocol.h b/source/net/protocol.h index f722a76..0a8110c 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -105,6 +105,23 @@ private: const char *deserialize(A &, const char *, const char *) const; }; + template + class CompoundSerializer + { + public: + typedef C ValueType; + + private: + const CompoundTypeDef &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 class Serializer: public Head { @@ -226,6 +243,9 @@ private: void add_packet(PacketDefBase *); protected: + template + PacketDefBuilder > add(unsigned); + template PacketDefBuilder > add(); @@ -251,13 +271,19 @@ private: template -Protocol::PacketDefBuilder > Protocol::add() +Protocol::PacketDefBuilder > Protocol::add(unsigned id) { - PacketTypeDef

*pdef = new PacketTypeDef

(next_packet_id++); + PacketTypeDef

*pdef = new PacketTypeDef

(id); add_packet(pdef); return PacketDefBuilder >(*this, *pdef, NullSerializer

()); } +template +Protocol::PacketDefBuilder > Protocol::add() +{ + return add

(next_packet_id++); +} + template const Protocol::PacketTypeDef

&Protocol::get_packet_by_class() const { @@ -294,6 +320,13 @@ struct Protocol::BasicTraits typedef BasicSerializer Serializer; }; +template +struct Protocol::Traits +{ + static const UInt16 signature = 'C'; + typedef CompoundSerializer Serializer; +}; + template<> struct Protocol::Traits: BasicTraits { }; template<> struct Protocol::Traits: BasicTraits { }; template<> struct Protocol::Traits: BasicTraits { }; @@ -383,6 +416,24 @@ const char *Protocol::ArraySerializer::deserialize(A &array, const char *buf, } +template +Protocol::CompoundSerializer::CompoundSerializer(const Protocol &proto): + def(proto.get_packet_by_class().get_compound()) +{ } + +template +char *Protocol::CompoundSerializer::serialize(const C &com, char *buf, char *end) const +{ + return def.serialize(com, buf, end); +} + +template +const char *Protocol::CompoundSerializer::deserialize(C &com, const char *buf, const char *end) const +{ + return def.deserialize(com, buf, end); +} + + template Protocol::Serializer::Serializer(const Head &h, Pointer p, const Protocol &proto): Head(h),