From: Mikko Rasa Date: Sun, 9 Oct 2016 13:53:23 +0000 (+0300) Subject: Massive overhaul of the Protocol class X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3c2a877580e234df5fcbe06bf2850cd29f875e28;p=libs%2Fnet.git Massive overhaul of the Protocol class Makes the serialization machinery more flexible and reduces the number of virtual function calls when de/serializing packets. --- diff --git a/source/net/communicator.cpp b/source/net/communicator.cpp index 618e8b4..b30a8e9 100644 --- a/source/net/communicator.cpp +++ b/source/net/communicator.cpp @@ -133,7 +133,7 @@ bool Communicator::receive_packet(const Protocol &proto, ReceiverBase &recv) { char *pkt = in_begin; in_begin += psz; - proto.disassemble(recv, pkt, psz); + proto.dispatch(recv, pkt, psz); return true; } else @@ -155,7 +155,7 @@ void Communicator::send_handshake() shake.hash = protocol.get_hash(); HandshakeProtocol hsproto; - unsigned size = hsproto.assemble(shake, out_buf, buf_size); + unsigned size = hsproto.serialize(shake, out_buf, buf_size); socket.write(out_buf, size); } diff --git a/source/net/communicator.h b/source/net/communicator.h index cd465a5..5921767 100644 --- a/source/net/communicator.h +++ b/source/net/communicator.h @@ -47,7 +47,7 @@ public: throw sequence_error("connection aborted"); if(handshake_status!=2) throw sequence_error("handshaking not done"); - unsigned size = protocol.assemble(pkt, out_buf, buf_size); + unsigned size = protocol.serialize(pkt, out_buf, buf_size); try { socket.write(out_buf, size); diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index d56ac77..d862ed8 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -6,108 +6,17 @@ using namespace std; -namespace { - -using Msp::Net::buffer_error; - -template -class Assembler -{ -public: - static char *assemble(const T &v, char *, char *); - static const char *disassemble(T &, const char *, const char *); -}; - -template -class Assembler > -{ -public: - static char *assemble(const vector &v, char *, char *); - static const char *disassemble(vector &, const char *, const char *); -}; - -template -char *Assembler::assemble(const T &v, char *data, char *end) -{ - // XXX Assumes little-endian - const char *ptr = reinterpret_cast(&v)+sizeof(T); - for(unsigned i=0; i -char *Assembler::assemble(const string &v, char *data, char *end) -{ - data = Assembler::assemble(v.size(), data, end); - if(end-data(v.size())) - throw buffer_error("overflow"); - memcpy(data, v.data(), v.size()); - return data+v.size(); -} - -template -char *Assembler >::assemble(const vector &v, char *data, char *end) -{ - data = Assembler::assemble(v.size(), data, end); - for(typename vector::const_iterator i=v.begin(); i!=v.end(); ++i) - data = Assembler::assemble(*i, data, end); - return data; -} - -template -const char *Assembler::disassemble(T &v, const char *data, const char *end) -{ - char *ptr = reinterpret_cast(&v)+sizeof(T); - for(unsigned i=0; i -const char *Assembler::disassemble(string &v, const char *data, const char *end) -{ - unsigned short size; - data = Assembler::disassemble(size, data, end); - if(end-data -const char *Assembler >::disassemble(vector &v, const char *data, const char *end) -{ - /* We assume that the vector is in pristine state - this holds because the - only code path leading here is from PacketDef

::disassemble, which creates - a new packet. */ - unsigned short size; - data = Assembler::disassemble(size, data, end); - for(unsigned i=0; i::disassemble(u, data, end); - v.push_back(u); - } - return data; -} - -} namespace Msp { namespace Net { Protocol::Protocol(unsigned npi): + header_def(0), next_packet_id(npi) -{ } +{ + PacketDefBuilder >(*this, header_def, NullSerializer()) + (&PacketHeader::type)(&PacketHeader::length); +} Protocol::~Protocol() { @@ -115,16 +24,16 @@ Protocol::~Protocol() delete i->second; } -void Protocol::add_packet(PacketDefBase &pdef) +void Protocol::add_packet(PacketDefBase *pdef) { - PacketDefBase *&ptr = packet_class_defs[pdef.get_class_id()]; + PacketDefBase *&ptr = packet_class_defs[pdef->get_class_id()]; if(ptr) delete ptr; - ptr = &pdef; - packet_id_defs[pdef.get_id()] = &pdef; + ptr = pdef; + packet_id_defs[pdef->get_id()] = pdef; } -const Protocol::PacketDefBase &Protocol::get_packet_by_class(unsigned id) const +const Protocol::PacketDefBase &Protocol::get_packet_by_class_id(unsigned id) const { return *get_item(packet_class_defs, id); } @@ -134,24 +43,24 @@ const Protocol::PacketDefBase &Protocol::get_packet_by_id(unsigned id) const return *get_item(packet_id_defs, id); } -unsigned Protocol::disassemble(ReceiverBase &rcv, const char *data, unsigned size) const +unsigned Protocol::dispatch(ReceiverBase &rcv, const char *buf, unsigned size) const { - const unsigned char *udata = reinterpret_cast(data); - unsigned id = (udata[0]<<8)+udata[1]; - unsigned psz = (udata[2]<<8)+udata[3]; - if(psz>size) + PacketHeader header; + buf = header_def.deserialize(header, buf, buf+size); + if(header.length>size) throw bad_packet("truncated"); - const PacketDefBase &pdef = get_packet_by_id(id); - const char *ptr = pdef.disassemble(rcv, data+4, data+psz); - return ptr-data; + const PacketDefBase &pdef = get_packet_by_id(header.type); + const char *ptr = pdef.dispatch(rcv, buf, buf+header.length); + return ptr-buf; } -unsigned Protocol::get_packet_size(const char *data, unsigned size) const +unsigned Protocol::get_packet_size(const char *buf, unsigned size) const { if(size<4) return 0; - const unsigned char *udata = reinterpret_cast(data); - return (udata[2]<<8)+udata[3]; + PacketHeader header; + header_def.deserialize(header, buf, buf+size); + return header.length; } unsigned Protocol::get_hash() const @@ -160,73 +69,97 @@ unsigned Protocol::get_hash() const return 123; } -void Protocol::assemble_header(char *buf, unsigned id, unsigned size) + +/* TODO These assumes the machine is little-endian; are there any relevant +big-endian platforms these days? */ +template +char *Protocol::BasicSerializer::serialize(const T &value, char *buf, char *end) const { - buf[0] = (id>>8)&0xFF; - buf[1] = id&0xFF; - buf[2] = (size>>8)&0xFF; - buf[3] = size&0xFF; + if(end-buf(sizeof(T))) + throw buffer_error("overflow"); + + const char *ptr = reinterpret_cast(&value)+sizeof(T); + for(unsigned i=0; i -char *Protocol::assemble_field(const T &v, char *d, char *e) -{ return Assembler::assemble(v, d, e); } - -template char *Protocol::assemble_field<>(const char &v, char *d, char *e); -template char *Protocol::assemble_field<>(const signed char &v, char *d, char *e); -template char *Protocol::assemble_field<>(const unsigned char &v, char *d, char *e); -template char *Protocol::assemble_field<>(const short &v, char *d, char *e); -template char *Protocol::assemble_field<>(const unsigned short &v, char *d, char *e); -template char *Protocol::assemble_field<>(const int &v, char *d, char *e); -template char *Protocol::assemble_field<>(const unsigned &v, char *d, char *e); -template char *Protocol::assemble_field<>(const long &v, char *d, char *e); -template char *Protocol::assemble_field<>(const unsigned long &v, char *d, char *e); -template char *Protocol::assemble_field<>(const float &v, char *d, char *e); -template char *Protocol::assemble_field<>(const double &v, char *d, char *e); -template char *Protocol::assemble_field<>(const string &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); -template char *Protocol::assemble_field<>(const vector &v, char *d, char *e); +const char *Protocol::BasicSerializer::deserialize(T &value, const char *buf, const char *end) const +{ + if(end-buf(sizeof(T))) + throw buffer_error("underflow"); + + char *ptr = reinterpret_cast(&value)+sizeof(T); + for(unsigned i=0; i::serialize(const Int8 &, char *, char *) const; +template char *Protocol::BasicSerializer::serialize(const Int16 &, char *, char *) const; +template char *Protocol::BasicSerializer::serialize(const Int32 &, char *, char *) const; +template char *Protocol::BasicSerializer::serialize(const Int64 &, char *, char *) const; +template char *Protocol::BasicSerializer::serialize(const UInt8 &, char *, char *) const; +template char *Protocol::BasicSerializer::serialize(const UInt16 &, char *, char *) const; +template char *Protocol::BasicSerializer::serialize(const UInt32 &, char *, char *) const; +template char *Protocol::BasicSerializer::serialize(const UInt64 &, char *, char *) const; +template char *Protocol::BasicSerializer::serialize(const float &, char *, char *) const; +template char *Protocol::BasicSerializer::serialize(const double &, char *, char *) const; +template const char *Protocol::BasicSerializer::deserialize(Int8 &, const char *, const char *) const; +template const char *Protocol::BasicSerializer::deserialize(Int16 &, const char *, const char *) const; +template const char *Protocol::BasicSerializer::deserialize(Int32 &, const char *, const char *) const; +template const char *Protocol::BasicSerializer::deserialize(Int64 &, const char *, const char *) const; +template const char *Protocol::BasicSerializer::deserialize(UInt8 &, const char *, const char *) const; +template const char *Protocol::BasicSerializer::deserialize(UInt16 &, const char *, const char *) const; +template const char *Protocol::BasicSerializer::deserialize(UInt32 &, const char *, const char *) const; +template const char *Protocol::BasicSerializer::deserialize(UInt64 &, const char *, const char *) const; +template const char *Protocol::BasicSerializer::deserialize(float &, const char *, const char *) const; +template const char *Protocol::BasicSerializer::deserialize(double &, const char *, const char *) const; + + +Protocol::StringSerializer::StringSerializer(const Protocol &p): + length_serializer(p) +{ } + +char *Protocol::StringSerializer::serialize(const string &str, char *buf, char *end) const +{ + buf = length_serializer.serialize(str.size(), buf, end); + if(end-buf(str.size())) + throw buffer_error("overflow"); + copy(str.begin(), str.end(), buf); + return buf+str.size(); +} + +const char *Protocol::StringSerializer::deserialize(string &str, const char *buf, const char *end) const +{ + UInt16 length; + buf = length_serializer.deserialize(length, buf, end); + if(end-buf(length)) + throw buffer_error("underflow"); + str.assign(buf, buf+length); + return buf+length; +} -template -const char *Protocol::disassemble_field(T &v, const char *d, const char *e) -{ return Assembler::disassemble(v, d, e); } - -template const char *Protocol::disassemble_field<>(char &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(signed char &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(unsigned char &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(short &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(unsigned short &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(int &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(unsigned &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(long &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(unsigned long &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(float &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(double &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(string &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); -template const char *Protocol::disassemble_field<>(vector &v, const char *d, const char *e); unsigned Protocol::PacketDefBase::next_class_id = 1; +Protocol::PacketDefBase::PacketDefBase(unsigned i): + id(i) +{ } + + +Protocol::PacketHeader::PacketHeader(): + type(0), + length(0) +{ } + +Protocol::PacketHeader::PacketHeader(UInt16 t, UInt16 l): + type(t), + length(l) +{ } + } // namespace Net } // namespace Msp diff --git a/source/net/protocol.h b/source/net/protocol.h index 75b3514..f6c382c 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "receiver.h" namespace Msp { @@ -28,147 +29,409 @@ public: class Protocol { private: - class PacketDefBase + template + struct BasicTraits; + + template + struct Traits; + + template + struct CompoundTypeDef { - protected: - unsigned id; + virtual ~CompoundTypeDef() { } - PacketDefBase(unsigned i): id(i) { } - public: - virtual ~PacketDefBase() { } - virtual unsigned get_class_id() const = 0; - unsigned get_id() const { return id; } - virtual const char *disassemble(ReceiverBase &, const char *, const char *) const = 0; + virtual char *serialize(const C &, char *, char *) const = 0; + virtual const char *deserialize(C &, const char *, const char *) const = 0; + }; - static unsigned next_class_id; + template + struct CompoundDef: public CompoundTypeDef + { + S *serializer; + + CompoundDef(const S &); + virtual ~CompoundDef(); + + virtual char *serialize(const C &, char *, char *) const; + virtual const char *deserialize(C &, const char *, const char *) const; }; - template - class FieldBase + template + class BasicSerializer { - protected: - FieldBase() { } public: - virtual ~FieldBase() { } - virtual char *assemble(const P &, char *, char *) const = 0; - virtual const char *disassemble(P &, const char *, const char *) const = 0; + typedef T ValueType; + + BasicSerializer(const Protocol &) { } + + char *serialize(const T &, char *, char *) const; + const char *deserialize(T &, const char *, const char *) const; }; - template - class Field: public FieldBase

+ class StringSerializer { + public: + typedef std::string ValueType; + private: - T P::*ptr; + BasicSerializer length_serializer; public: - Field(T P::*p): ptr(p) { } - - virtual char *assemble(const P &p, char *d, char *e) const - { return assemble_field(p.*ptr, d, e); } + StringSerializer(const Protocol &); - virtual const char *disassemble(P &p, const char *d, const char *e) const - { return disassemble_field(p.*ptr, d, e); } + char *serialize(const std::string &, char *, char *) const; + const char *deserialize(std::string &, const char *, const char *) const; }; -protected: - template - class PacketDef: public PacketDefBase + template + class ArraySerializer { + public: + typedef A ValueType; + private: - std::vector *> fields; + BasicSerializer length_serializer; + typename Traits::Serializer element_serializer; public: - PacketDef(unsigned i): PacketDefBase(i) - { if(!class_id) class_id = next_class_id++; } + ArraySerializer(const Protocol &); - ~PacketDef() + char *serialize(const A &, char *, char *) const; + const char *deserialize(A &, const char *, const char *) const; + }; + + template + class Serializer: public Head + { + public: + template + struct Next { - for(typename std::vector *>::const_iterator i=fields.begin(); i!=fields.end(); ++i) - delete *i; - } + typedef Serializer, typename Traits::Serializer> Type; + }; - virtual unsigned get_class_id() const { return class_id; } + private: + typedef typename S::ValueType P::*Pointer; - template - PacketDef &operator()(T P::*p) - { fields.push_back(new Field(p)); return *this; } + Pointer ptr; + S ser; - char *assemble(const P &p, char *d, char *e) const - { - for(typename std::vector *>::const_iterator i=fields.begin(); i!=fields.end(); ++i) - d = (*i)->assemble(p, d, e); - return d; - } + public: + Serializer(const Head &, Pointer, const Protocol &); - const char *disassemble(ReceiverBase &r, const char *d, const char *e) const + char *serialize(const P &, char *, char *) const; + const char *deserialize(P &, const char *, const char *) const; + }; + + template + class NullSerializer + { + public: + template + struct Next { - PacketReceiver

*prcv = dynamic_cast *>(&r); - if(!prcv) - throw bad_packet("unsupported"); - P pkt; - for(typename std::vector *>::const_iterator i=fields.begin(); i!=fields.end(); ++i) - d = (*i)->disassemble(pkt, d, e); - prcv->receive(pkt); - return d; - } + typedef Serializer::Serializer> Type; + }; + + char *serialize(const P &, char *b, char *) const { return b; } + const char *deserialize(P &, const char *b, const char *) const { return b; } + }; + + class PacketDefBase + { + protected: + unsigned id; + + static unsigned next_class_id; + + PacketDefBase(unsigned); + public: + virtual ~PacketDefBase() { } + virtual unsigned get_class_id() const = 0; + unsigned get_id() const { return id; } + virtual const char *dispatch(ReceiverBase &, const char *, const char *) const = 0; + }; + + template + class PacketTypeDef: public PacketDefBase + { + 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; } + + template + void set_serializer(const S &); + + const CompoundTypeDef

&get_compound() const { return *compound; } + + 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; + }; + + template + class PacketDefBuilder + { + private: + const Protocol &protocol; + PacketTypeDef

&pktdef; + S serializer; + + public: + PacketDefBuilder(const Protocol &, PacketTypeDef

&, const S &); + + template + PacketDefBuilder::Type> operator()(T P::*); + }; + + struct PacketHeader + { + UInt16 type; + UInt16 length; + + PacketHeader(); + PacketHeader(UInt16, UInt16); }; typedef std::map PacketMap; + PacketTypeDef header_def; unsigned next_packet_id; PacketMap packet_class_defs; PacketMap packet_id_defs; +protected: Protocol(unsigned = 1); public: ~Protocol(); private: - void add_packet(PacketDefBase &); + void add_packet(PacketDefBase *); protected: template - PacketDef

&add() - { - PacketDef

*pdef = new PacketDef

(next_packet_id++); - add_packet(*pdef); - return *pdef; - } + PacketDefBuilder > add(); - const PacketDefBase &get_packet_by_class(unsigned) const; + const PacketDefBase &get_packet_by_class_id(unsigned) const; const PacketDefBase &get_packet_by_id(unsigned) const; -public: template - unsigned assemble(const P &pkt, char *buf, unsigned size) const - { - unsigned id = PacketDef

::class_id; - const PacketDef

&pdef = static_cast &>(get_packet_by_class(id)); - char *ptr = pdef.assemble(pkt, buf+4, buf+size); - assemble_header(buf, pdef.get_id(), (size = ptr-buf)); - return size; - } + const PacketTypeDef

&get_packet_by_class() const; - unsigned disassemble(ReceiverBase &, const char *, unsigned) const; +public: + template + unsigned serialize(const P &, char *, unsigned) const; unsigned get_packet_size(const char *, unsigned) const; + unsigned dispatch(ReceiverBase &, const char *, unsigned) const; unsigned get_hash() const; +}; -private: - static void assemble_header(char *, unsigned, unsigned); - template - static char *assemble_field(const T &, char *, char *); +template +Protocol::PacketDefBuilder > Protocol::add() +{ + PacketTypeDef

*pdef = new PacketTypeDef

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

()); +} - template - static const char *disassemble_field(T &, const char *, const char *); +template +const Protocol::PacketTypeDef

&Protocol::get_packet_by_class() const +{ + const PacketDefBase &pdef = get_packet_by_class_id(PacketTypeDef

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

&pdef = get_packet_by_class

(); + char *ptr = pdef.serialize(pkt, buf+4, buf+size); + size = ptr-buf; + header_def.serialize(PacketHeader(pdef.get_id(), size), buf, buf+4); + return size; +} + + +template +struct Protocol::BasicTraits +{ + 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: BasicTraits { }; +template<> struct Protocol::Traits: BasicTraits { }; + +template<> struct Protocol::Traits +{ + typedef StringSerializer Serializer; +}; + +template +struct Protocol::Traits > +{ + typedef ArraySerializer > Serializer; +}; + + + +template +Protocol::CompoundDef::CompoundDef(const S &s): + serializer(new S(s)) +{ } + +template +Protocol::CompoundDef::~CompoundDef() +{ + delete serializer; +} + +template +char *Protocol::CompoundDef::serialize(const C &com, char *buf, char *end) const +{ + 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); +} + + +template +Protocol::ArraySerializer::ArraySerializer(const Protocol &proto): + length_serializer(proto), + element_serializer(proto) +{ } + +template +char *Protocol::ArraySerializer::serialize(const A &array, char *buf, char *end) const +{ + buf = length_serializer.serialize(array.size(), buf, end); + for(typename A::const_iterator i=array.begin(); i!=array.end(); ++i) + buf = element_serializer.serialize(*i, buf, end); + return buf; +} + +template +const char *Protocol::ArraySerializer::deserialize(A &array, const char *buf, const char *end) const +{ + UInt16 length; + buf = length_serializer.deserialize(length, buf, end); + array.resize(length); + for(unsigned i=0; i +Protocol::Serializer::Serializer(const Head &h, Pointer p, const Protocol &proto): + Head(h), + ptr(p), + ser(proto) +{ } + +template +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 +{ + buf = Head::deserialize(pkt, buf, end); + return ser.deserialize(pkt.*ptr, buf, end); +} + + +template +unsigned Protocol::PacketTypeDef

::class_id = 0; + template -unsigned Protocol::PacketDef

::class_id = 0; +Protocol::PacketTypeDef

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

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

::~PacketTypeDef() +{ + delete compound; +} + +template +template +void Protocol::PacketTypeDef

::set_serializer(const S &ser) +{ + delete compound; + compound = new CompoundDef(ser); +} + +template +char *Protocol::PacketTypeDef

::serialize(const P &pkt, char *buf, char *end) const +{ + return compound->serialize(pkt, buf, end); +} + +template +const char *Protocol::PacketTypeDef

::deserialize(P &pkt, const char *buf, const char *end) const +{ + return compound->deserialize(pkt, buf, end); +} + +template +const char *Protocol::PacketTypeDef

::dispatch(ReceiverBase &rcv, const char *buf, const char *end) const +{ + PacketReceiver

*prcv = dynamic_cast *>(&rcv); + if(!prcv) + throw bad_packet("unsupported"); + P pkt; + buf = deserialize(pkt, buf, end); + prcv->receive(pkt); + return buf; +} + + +template +Protocol::PacketDefBuilder::PacketDefBuilder(const Protocol &p, PacketTypeDef

&k, const S &s): + protocol(p), + pktdef(k), + serializer(s) +{ } + +template +template +Protocol::PacketDefBuilder::Type> Protocol::PacketDefBuilder::operator()(T P::*ptr) +{ + typename S::template Next::Type next_ser(serializer, ptr, protocol); + pktdef.set_serializer(next_ser); + return PacketDefBuilder::Type>(protocol, pktdef, next_ser); +} } // namespace Net } // namespace Msp