X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnet%2Fprotocol.h;h=cd68d5148735024eaa77f9ef1ebe06a16064de72;hb=ee01d294f9078fbf17dc6a0a645ad3a342c49b1a;hp=f722a76c82a7cc73b83d4880b7ac8043f45bd889;hpb=df1b7561be0d4a57e964c783c01060c54864ec60;p=libs%2Fnet.git diff --git a/source/net/protocol.h b/source/net/protocol.h index f722a76..cd68d51 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -1,10 +1,10 @@ #ifndef MSP_NET_PROTOCOL_H_ #define MSP_NET_PROTOCOL_H_ +#include #include #include #include -#include #include "receiver.h" namespace Msp { @@ -14,7 +14,6 @@ class bad_packet: public std::runtime_error { public: bad_packet(const std::string &w): std::runtime_error(w) { } - virtual ~bad_packet() throw() { } }; @@ -22,14 +21,13 @@ class buffer_error: public std::runtime_error { public: buffer_error(const std::string &w): std::runtime_error(w) { } - virtual ~buffer_error() throw() { } }; class Protocol { private: - template + template struct BasicTraits; template @@ -48,10 +46,9 @@ private: template struct CompoundDef: public CompoundTypeDef { - S *serializer; + S serializer; CompoundDef(const S &); - virtual ~CompoundDef(); virtual std::string describe() const; virtual char *serialize(const C &, char *, char *) const; @@ -77,7 +74,7 @@ private: typedef std::string ValueType; private: - BasicSerializer length_serializer; + BasicSerializer length_serializer; public: StringSerializer(const Protocol &); @@ -94,7 +91,7 @@ private: typedef A ValueType; private: - BasicSerializer length_serializer; + BasicSerializer length_serializer; typename Traits::Serializer element_serializer; public: @@ -105,6 +102,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 { @@ -149,8 +163,6 @@ private: protected: unsigned id; - static unsigned next_class_id; - PacketDefBase(unsigned); public: virtual ~PacketDefBase() { } @@ -166,21 +178,18 @@ private: private: CompoundTypeDef

*compound; - static unsigned class_id; - public: PacketTypeDef(unsigned); ~PacketTypeDef(); - static unsigned get_static_class_id() { return class_id; } - virtual unsigned get_class_id() const { return class_id; } + virtual unsigned get_class_id() const { return get_packet_class_id

(); } template void set_serializer(const S &); const CompoundTypeDef

&get_compound() const { return *compound; } - virtual std::string describe() const; + virtual std::string describe() const { return compound->describe(); } char *serialize(const P &, char *, char *) const; const char *deserialize(P &, const char *, const char *) const; virtual const char *dispatch(ReceiverBase &, const char *, const char *) const; @@ -203,11 +212,11 @@ private: struct PacketHeader { - UInt16 type; - UInt16 length; + std::uint16_t type; + std::uint16_t length; PacketHeader(); - PacketHeader(UInt16, UInt16); + PacketHeader(std::uint16_t, std::uint16_t); }; typedef std::map PacketMap; @@ -223,9 +232,17 @@ public: ~Protocol(); private: + static unsigned get_next_packet_class_id(); + + template + static unsigned get_packet_class_id(); + void add_packet(PacketDefBase *); protected: + template + PacketDefBuilder > add(unsigned); + template PacketDefBuilder > add(); @@ -237,12 +254,12 @@ protected: public: template - unsigned serialize(const P &, char *, unsigned) const; + std::size_t serialize(const P &, char *, std::size_t) const; - unsigned get_packet_size(const char *, unsigned) const; - unsigned dispatch(ReceiverBase &, const char *, unsigned) const; + std::size_t get_packet_size(const char *, std::size_t) const; + std::size_t dispatch(ReceiverBase &, const char *, std::size_t) const; - UInt64 get_hash() const; + std::uint64_t get_hash() const; private: template @@ -251,24 +268,39 @@ private: template -Protocol::PacketDefBuilder > Protocol::add() +unsigned Protocol::get_packet_class_id() +{ + static unsigned id = get_next_packet_class_id(); + return id; +} + +template +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 { - const PacketDefBase &pdef = get_packet_by_class_id(PacketTypeDef

::get_static_class_id()); + const PacketDefBase &pdef = get_packet_by_class_id(get_packet_class_id

()); return static_cast &>(pdef); } template -unsigned Protocol::serialize(const P &pkt, char *buf, unsigned size) const +std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const { const PacketTypeDef

&pdef = get_packet_by_class

(); + if(!pdef.get_id()) + throw std::invalid_argument("no packet id"); char *ptr = pdef.serialize(pkt, buf+4, buf+size); size = ptr-buf; header_def.serialize(PacketHeader(pdef.get_id(), size), buf, buf+4); @@ -278,7 +310,7 @@ unsigned Protocol::serialize(const P &pkt, char *buf, unsigned size) const template std::string Protocol::get_type_signature() { - const UInt16 sig = Traits::signature; + const std::uint16_t sig = Traits::signature; std::string result; result += sig&0xFF; if(sig>=0x100) @@ -287,34 +319,42 @@ std::string Protocol::get_type_signature() } -template +template struct Protocol::BasicTraits { - static const UInt16 signature = K | (sizeof(T)<<8); + static const std::uint16_t signature = K | (sizeof(T)<<8); typedef BasicSerializer Serializer; }; -template<> struct Protocol::Traits: BasicTraits { }; -template<> struct Protocol::Traits: BasicTraits { }; -template<> struct Protocol::Traits: BasicTraits { }; -template<> struct Protocol::Traits: BasicTraits { }; -template<> struct Protocol::Traits: BasicTraits { }; -template<> struct Protocol::Traits: BasicTraits { }; -template<> struct Protocol::Traits: BasicTraits { }; -template<> struct Protocol::Traits: BasicTraits { }; +template +struct Protocol::Traits +{ + static const std::uint16_t signature = 'C'; + typedef CompoundSerializer Serializer; +}; + +template<> struct Protocol::Traits: BasicTraits { }; +template<> struct Protocol::Traits: BasicTraits { }; +template<> struct Protocol::Traits: BasicTraits { }; +template<> struct Protocol::Traits: BasicTraits { }; +template<> struct Protocol::Traits: BasicTraits { }; +template<> struct Protocol::Traits: BasicTraits { }; +template<> struct Protocol::Traits: BasicTraits { }; +template<> struct Protocol::Traits: BasicTraits { }; +template<> struct Protocol::Traits: BasicTraits { }; template<> struct Protocol::Traits: BasicTraits { }; template<> struct Protocol::Traits: BasicTraits { }; template<> struct Protocol::Traits { - static const UInt16 signature = 'S'; + static const std::uint16_t signature = 'S'; typedef StringSerializer Serializer; }; template struct Protocol::Traits > { - static const UInt16 signature = 'A'; + static const std::uint16_t signature = 'A'; typedef ArraySerializer > Serializer; }; @@ -322,31 +362,25 @@ struct Protocol::Traits > template Protocol::CompoundDef::CompoundDef(const S &s): - serializer(new S(s)) + serializer(s) { } -template -Protocol::CompoundDef::~CompoundDef() -{ - delete serializer; -} - template std::string Protocol::CompoundDef::describe() const { - return "{"+serializer->describe()+"}"; + return "{"+serializer.describe()+"}"; } template char *Protocol::CompoundDef::serialize(const C &com, char *buf, char *end) const { - return serializer->serialize(com, buf, end); + return serializer.serialize(com, buf, end); } template const char *Protocol::CompoundDef::deserialize(C &com, const char *buf, const char *end) const { - return serializer->deserialize(com, buf, end); + return serializer.deserialize(com, buf, end); } @@ -374,7 +408,7 @@ char *Protocol::ArraySerializer::serialize(const A &array, char *buf, char *e template const char *Protocol::ArraySerializer::deserialize(A &array, const char *buf, const char *end) const { - UInt16 length; + std::uint16_t length; buf = length_serializer.deserialize(length, buf, end); array.resize(length); for(unsigned i=0; i::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), @@ -397,31 +449,25 @@ std::string Protocol::Serializer::describe() const } template -char * Protocol::Serializer::serialize(const P &pkt, char *buf, char *end) const +char *Protocol::Serializer::serialize(const P &pkt, char *buf, char *end) const { buf = Head::serialize(pkt, buf, end); return ser.serialize(pkt.*ptr, buf, end); } template -const char * Protocol::Serializer::deserialize(P &pkt, const char *buf, const char *end) const +const char *Protocol::Serializer::deserialize(P &pkt, const char *buf, const char *end) const { buf = Head::deserialize(pkt, buf, end); return ser.deserialize(pkt.*ptr, buf, end); } -template -unsigned Protocol::PacketTypeDef

::class_id = 0; - template Protocol::PacketTypeDef

::PacketTypeDef(unsigned i): PacketDefBase(i), compound(new CompoundDef >(NullSerializer

())) -{ - if(!class_id) - class_id = next_class_id++; -} +{ } template Protocol::PacketTypeDef

::~PacketTypeDef() @@ -437,12 +483,6 @@ void Protocol::PacketTypeDef

::set_serializer(const S &ser) compound = new CompoundDef(ser); } -template -std::string Protocol::PacketTypeDef

::describe() const -{ - return compound->describe(); -} - template char *Protocol::PacketTypeDef

::serialize(const P &pkt, char *buf, char *end) const {