Massive overhaul of the Protocol class
authorMikko Rasa <tdb@tdb.fi>
Sun, 9 Oct 2016 13:53:23 +0000 (16:53 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 9 Oct 2016 13:59:29 +0000 (16:59 +0300)
Makes the serialization machinery more flexible and reduces the number of
virtual function calls when de/serializing packets.

source/net/communicator.cpp
source/net/communicator.h
source/net/protocol.cpp
source/net/protocol.h

index 618e8b45e9e295f01fc4948ef19daecd56f51242..b30a8e965c388c8457b26ae454ce6c399fe5f6fc 100644 (file)
@@ -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);
 }
 
index cd465a52d525c25cf837db2cca614a5a6e8cfc98..5921767e6a37ada58ba91876f5a2b80a30c3c59a 100644 (file)
@@ -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);
index d56ac77a09f70eff7137da98beef2eb7112d8978..d862ed82f72a37d7416160307dac7b7069d749bf 100644 (file)
 
 using namespace std;
 
-namespace {
-
-using Msp::Net::buffer_error;
-
-template<typename T>
-class Assembler
-{
-public:
-       static char *assemble(const T &v, char *, char *);
-       static const char *disassemble(T &, const char *, const char *);
-};
-
-template<typename T>
-class Assembler<vector<T> >
-{
-public:
-       static char *assemble(const vector<T> &v, char *, char *);
-       static const char *disassemble(vector<T> &, const char *, const char *);
-};
-
-template<typename T>
-char *Assembler<T>::assemble(const T &v, char *data, char *end)
-{
-       // XXX Assumes little-endian
-       const char *ptr = reinterpret_cast<const char *>(&v)+sizeof(T);
-       for(unsigned i=0; i<sizeof(T); ++i)
-       {
-               if(data==end)
-                       throw buffer_error("overflow");
-               *data++ = *--ptr;
-       }
-       return data;
-}
-
-template<>
-char *Assembler<string>::assemble(const string &v, char *data, char *end)
-{
-       data = Assembler<unsigned short>::assemble(v.size(), data, end);
-       if(end-data<static_cast<int>(v.size()))
-               throw buffer_error("overflow");
-       memcpy(data, v.data(), v.size());
-       return data+v.size();
-}
-
-template<typename T>
-char *Assembler<vector<T> >::assemble(const vector<T> &v, char *data, char *end)
-{
-       data = Assembler<unsigned short>::assemble(v.size(), data, end);
-       for(typename vector<T>::const_iterator i=v.begin(); i!=v.end(); ++i)
-               data = Assembler<T>::assemble(*i, data, end);
-       return data;
-}
-
-template<typename T>
-const char *Assembler<T>::disassemble(T &v, const char *data, const char *end)
-{
-       char *ptr = reinterpret_cast<char *>(&v)+sizeof(T);
-       for(unsigned i=0; i<sizeof(T); ++i)
-       {
-               if(data==end)
-                       throw buffer_error("underflow");
-               *--ptr = *data++;
-       }
-       return data;
-}
-
-template<>
-const char *Assembler<string>::disassemble(string &v, const char *data, const char *end)
-{
-       unsigned short size;
-       data = Assembler<unsigned short>::disassemble(size, data, end);
-       if(end-data<size)
-               throw buffer_error("underflow");
-       v.assign(data, data+size);
-       return data+size;
-}
-
-template<typename T>
-const char *Assembler<vector<T> >::disassemble(vector<T> &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<P>::disassemble, which creates
-       a new packet. */
-       unsigned short size;
-       data = Assembler<unsigned short>::disassemble(size, data, end);
-       for(unsigned i=0; i<size; ++i)
-       {
-               T u;
-               data = Assembler<T>::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<PacketHeader, NullSerializer<PacketHeader> >(*this, header_def, NullSerializer<PacketHeader>())
+               (&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<const unsigned char *>(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<const unsigned char *>(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<typename T>
+char *Protocol::BasicSerializer<T>::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<static_cast<int>(sizeof(T)))
+               throw buffer_error("overflow");
+
+       const char *ptr = reinterpret_cast<const char *>(&value)+sizeof(T);
+       for(unsigned i=0; i<sizeof(T); ++i)
+               *buf++ = *--ptr;
+
+       return buf;
 }
 
 template<typename T>
-char *Protocol::assemble_field(const T &v, char *d, char *e)
-{ return Assembler<T>::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<char> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<signed char> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<unsigned char> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<short> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<unsigned short> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<int> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<unsigned> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<long> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<unsigned long> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<float> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<double> &v, char *d, char *e);
-template char *Protocol::assemble_field<>(const vector<string> &v, char *d, char *e);
+const char *Protocol::BasicSerializer<T>::deserialize(T &value, const char *buf, const char *end) const
+{
+       if(end-buf<static_cast<int>(sizeof(T)))
+               throw buffer_error("underflow");
+
+       char *ptr = reinterpret_cast<char *>(&value)+sizeof(T);
+       for(unsigned i=0; i<sizeof(T); ++i)
+               *--ptr = *buf++;
+
+       return buf;
+}
+
+template char *Protocol::BasicSerializer<Int8>::serialize(const Int8 &, char *, char *) const;
+template char *Protocol::BasicSerializer<Int16>::serialize(const Int16 &, char *, char *) const;
+template char *Protocol::BasicSerializer<Int32>::serialize(const Int32 &, char *, char *) const;
+template char *Protocol::BasicSerializer<Int64>::serialize(const Int64 &, char *, char *) const;
+template char *Protocol::BasicSerializer<UInt8>::serialize(const UInt8 &, char *, char *) const;
+template char *Protocol::BasicSerializer<UInt16>::serialize(const UInt16 &, char *, char *) const;
+template char *Protocol::BasicSerializer<UInt32>::serialize(const UInt32 &, char *, char *) const;
+template char *Protocol::BasicSerializer<UInt64>::serialize(const UInt64 &, char *, char *) const;
+template char *Protocol::BasicSerializer<float>::serialize(const float &, char *, char *) const;
+template char *Protocol::BasicSerializer<double>::serialize(const double &, char *, char *) const;
+template const char *Protocol::BasicSerializer<Int8>::deserialize(Int8 &, const char *, const char *) const;
+template const char *Protocol::BasicSerializer<Int16>::deserialize(Int16 &, const char *, const char *) const;
+template const char *Protocol::BasicSerializer<Int32>::deserialize(Int32 &, const char *, const char *) const;
+template const char *Protocol::BasicSerializer<Int64>::deserialize(Int64 &, const char *, const char *) const;
+template const char *Protocol::BasicSerializer<UInt8>::deserialize(UInt8 &, const char *, const char *) const;
+template const char *Protocol::BasicSerializer<UInt16>::deserialize(UInt16 &, const char *, const char *) const;
+template const char *Protocol::BasicSerializer<UInt32>::deserialize(UInt32 &, const char *, const char *) const;
+template const char *Protocol::BasicSerializer<UInt64>::deserialize(UInt64 &, const char *, const char *) const;
+template const char *Protocol::BasicSerializer<float>::deserialize(float &, const char *, const char *) const;
+template const char *Protocol::BasicSerializer<double>::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<static_cast<int>(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<static_cast<int>(length))
+               throw buffer_error("underflow");
+       str.assign(buf, buf+length);
+       return buf+length;
+}
 
-template<typename T>
-const char *Protocol::disassemble_field(T &v, const char *d, const char *e)
-{ return Assembler<T>::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<char> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<signed char> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<unsigned char> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<short> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<unsigned short> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<int> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<unsigned> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<long> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<unsigned long> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<float> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<double> &v, const char *d, const char *e);
-template const char *Protocol::disassemble_field<>(vector<string> &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
index 75b3514bb43b2729c915902f7a33374e8b1b608c..f6c382c9f1ca003e2dbb982e0d7f59867c3a5ae3 100644 (file)
@@ -4,6 +4,7 @@
 #include <map>
 #include <stdexcept>
 #include <vector>
+#include <msp/core/inttypes.h>
 #include "receiver.h"
 
 namespace Msp {
@@ -28,147 +29,409 @@ public:
 class Protocol
 {
 private:
-       class PacketDefBase
+       template<typename T, UInt8 K>
+       struct BasicTraits;
+
+       template<typename T>
+       struct Traits;
+
+       template<typename C>
+       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<typename C, typename S>
+       struct CompoundDef: public CompoundTypeDef<C>
+       {
+               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<typename P>
-       class FieldBase
+       template<typename T>
+       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<typename P, typename T>
-       class Field: public FieldBase<P>
+       class StringSerializer
        {
+       public:
+               typedef std::string ValueType;
+
        private:
-               T P::*ptr;
+               BasicSerializer<UInt16> 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<typename P>
-       class PacketDef: public PacketDefBase
+       template<typename A>
+       class ArraySerializer
        {
+       public:
+               typedef A ValueType;
+
        private:
-               std::vector<FieldBase<P> *> fields;
+               BasicSerializer<UInt16> length_serializer;
+               typename Traits<typename A::value_type>::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<typename P, typename Head, typename S>
+       class Serializer: public Head
+       {
+       public:
+               template<typename N>
+               struct Next
                {
-                       for(typename std::vector<FieldBase<P> *>::const_iterator i=fields.begin(); i!=fields.end(); ++i)
-                               delete *i;
-               }
+                       typedef Serializer<P, Serializer<P, Head, S>, typename Traits<N>::Serializer> Type;
+               };
 
-               virtual unsigned get_class_id() const { return class_id; }
+       private:
+               typedef typename S::ValueType P::*Pointer;
 
-               template<typename T>
-               PacketDef &operator()(T P::*p)
-               { fields.push_back(new Field<P, T>(p)); return *this; }
+               Pointer ptr;
+               S ser;
 
-               char *assemble(const P &p, char *d, char *e) const
-               {
-                       for(typename std::vector<FieldBase<P> *>::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<typename P>
+       class NullSerializer
+       {
+       public:
+               template<typename N>
+               struct Next
                {
-                       PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&r);
-                       if(!prcv)
-                               throw bad_packet("unsupported");
-                       P pkt;
-                       for(typename std::vector<FieldBase<P> *>::const_iterator i=fields.begin(); i!=fields.end(); ++i)
-                               d = (*i)->disassemble(pkt, d, e);
-                       prcv->receive(pkt);
-                       return d;
-               }
+                       typedef Serializer<P, NullSerializer, typename Traits<N>::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<typename P>
+       class PacketTypeDef: public PacketDefBase
+       {
+       private:
+               CompoundTypeDef<P> *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<typename S>
+               void set_serializer(const S &);
+
+               const CompoundTypeDef<P> &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<typename P, typename S>
+       class PacketDefBuilder
+       {
+       private:
+               const Protocol &protocol;
+               PacketTypeDef<P> &pktdef;
+               S serializer;
+
+       public:
+               PacketDefBuilder(const Protocol &, PacketTypeDef<P> &, const S &);
+               
+               template<typename T>
+               PacketDefBuilder<P, typename S::template Next<T>::Type> operator()(T P::*);
+       };
+
+       struct PacketHeader
+       {
+               UInt16 type;
+               UInt16 length;
+
+               PacketHeader();
+               PacketHeader(UInt16, UInt16);
        };
 
        typedef std::map<unsigned, PacketDefBase *> PacketMap;
 
+       PacketTypeDef<PacketHeader> 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<typename P>
-       PacketDef<P> &add()
-       {
-               PacketDef<P> *pdef = new PacketDef<P>(next_packet_id++);
-               add_packet(*pdef);
-               return *pdef;
-       }
+       PacketDefBuilder<P, NullSerializer<P> > 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<typename P>
-       unsigned assemble(const P &pkt, char *buf, unsigned size) const
-       {
-               unsigned id = PacketDef<P>::class_id;
-               const PacketDef<P> &pdef = static_cast<const PacketDef<P> &>(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<P> &get_packet_by_class() const;
 
-       unsigned disassemble(ReceiverBase &, const char *, unsigned) const;
+public:
+       template<typename P>
+       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<typename T>
-       static char *assemble_field(const T &, char *, char *);
+template<typename P>
+Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
+{
+       PacketTypeDef<P> *pdef = new PacketTypeDef<P>(next_packet_id++);
+       add_packet(pdef);
+       return PacketDefBuilder<P, NullSerializer<P> >(*this, *pdef, NullSerializer<P>());
+}
 
-       template<typename T>
-       static const char *disassemble_field(T &, const char *, const char *);
+template<typename P>
+const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
+{
+       const PacketDefBase &pdef = get_packet_by_class_id(PacketTypeDef<P>::get_static_class_id());
+       return static_cast<const PacketTypeDef<P> &>(pdef);
+}
+
+template<typename P>
+unsigned Protocol::serialize(const P &pkt, char *buf, unsigned size) const
+{
+       const PacketTypeDef<P> &pdef = get_packet_by_class<P>();
+       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<typename T, UInt8 K>
+struct Protocol::BasicTraits
+{
+       typedef BasicSerializer<T> Serializer;
 };
 
+template<> struct Protocol::Traits<Int8>: BasicTraits<Int8> { };
+template<> struct Protocol::Traits<UInt8>: BasicTraits<UInt8> { };
+template<> struct Protocol::Traits<Int16>: BasicTraits<Int16> { };
+template<> struct Protocol::Traits<UInt16>: BasicTraits<UInt16> { };
+template<> struct Protocol::Traits<Int32>: BasicTraits<Int32> { };
+template<> struct Protocol::Traits<UInt32>: BasicTraits<UInt32> { };
+template<> struct Protocol::Traits<Int64>: BasicTraits<Int64> { };
+template<> struct Protocol::Traits<UInt64>: BasicTraits<UInt64> { };
+template<> struct Protocol::Traits<float>: BasicTraits<float> { };
+template<> struct Protocol::Traits<double>: BasicTraits<double> { };
+
+template<> struct Protocol::Traits<std::string>
+{
+       typedef StringSerializer Serializer;
+};
+
+template<typename T>
+struct Protocol::Traits<std::vector<T> >
+{
+       typedef ArraySerializer<std::vector<T> > Serializer;
+};
+
+
+
+template<typename C, typename S>
+Protocol::CompoundDef<C, S>::CompoundDef(const S &s):
+       serializer(new S(s))
+{ }
+
+template<typename C, typename S>
+Protocol::CompoundDef<C, S>::~CompoundDef()
+{
+       delete serializer;
+}
+
+template<typename C, typename S>
+char *Protocol::CompoundDef<C, S>::serialize(const C &com, char *buf, char *end) const
+{
+       return serializer->serialize(com, buf, end);
+}
+
+template<typename C, typename S>
+const char *Protocol::CompoundDef<C, S>::deserialize(C &com, const char *buf, const char *end) const
+{
+       return serializer->deserialize(com, buf, end);
+}
+
+
+template<typename A>
+Protocol::ArraySerializer<A>::ArraySerializer(const Protocol &proto):
+       length_serializer(proto),
+       element_serializer(proto)
+{ }
+
+template<typename A>
+char *Protocol::ArraySerializer<A>::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<typename A>
+const char *Protocol::ArraySerializer<A>::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<length; ++i)
+               buf = element_serializer.deserialize(array[i], buf, end);
+       return buf;
+}
+
+
+template<typename P, typename Head, typename S>
+Protocol::Serializer<P, Head, S>::Serializer(const Head &h, Pointer p, const Protocol &proto):
+       Head(h),
+       ptr(p),
+       ser(proto)
+{ }
+
+template<typename P, typename Head, typename S>
+char * Protocol::Serializer<P, Head, S>::serialize(const P &pkt, char *buf, char *end) const
+{
+       buf = Head::serialize(pkt, buf, end);
+       return ser.serialize(pkt.*ptr, buf, end);
+}
+
+template<typename P, typename Head, typename S>
+const char * Protocol::Serializer<P, Head, S>::deserialize(P &pkt, const char *buf, const char *end) const
+{
+       buf = Head::deserialize(pkt, buf, end);
+       return ser.deserialize(pkt.*ptr, buf, end);
+}
+
+
+template<typename P>
+unsigned Protocol::PacketTypeDef<P>::class_id = 0;
+
 template<typename P>
-unsigned Protocol::PacketDef<P>::class_id = 0;
+Protocol::PacketTypeDef<P>::PacketTypeDef(unsigned i):
+       PacketDefBase(i),
+       compound(new CompoundDef<P, NullSerializer<P> >(NullSerializer<P>()))
+{
+       if(!class_id)
+               class_id = next_class_id++;
+}
+
+template<typename P>
+Protocol::PacketTypeDef<P>::~PacketTypeDef()
+{
+       delete compound;
+}
+
+template<typename P>
+template<typename S>
+void Protocol::PacketTypeDef<P>::set_serializer(const S &ser)
+{
+       delete compound;
+       compound = new CompoundDef<P, S>(ser);
+}
+
+template<typename P>
+char *Protocol::PacketTypeDef<P>::serialize(const P &pkt, char *buf, char *end) const
+{
+       return compound->serialize(pkt, buf, end);
+}
+
+template<typename P>
+const char *Protocol::PacketTypeDef<P>::deserialize(P &pkt, const char *buf, const char *end) const
+{
+       return compound->deserialize(pkt, buf, end);
+}
+
+template<typename P>
+const char *Protocol::PacketTypeDef<P>::dispatch(ReceiverBase &rcv, const char *buf, const char *end) const
+{
+       PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&rcv);
+       if(!prcv)
+               throw bad_packet("unsupported");
+       P pkt;
+       buf = deserialize(pkt, buf, end);
+       prcv->receive(pkt);
+       return buf;
+}
+
+
+template<typename P, typename S>
+Protocol::PacketDefBuilder<P, S>::PacketDefBuilder(const Protocol &p, PacketTypeDef<P> &k, const S &s):
+       protocol(p),
+       pktdef(k),
+       serializer(s)
+{ }
+
+template<typename P, typename S>
+template<typename T>
+Protocol::PacketDefBuilder<P, typename S::template Next<T>::Type> Protocol::PacketDefBuilder<P, S>::operator()(T P::*ptr)
+{
+       typename S::template Next<T>::Type next_ser(serializer, ptr, protocol);
+       pktdef.set_serializer(next_ser);
+       return PacketDefBuilder<P, typename S::template Next<T>::Type>(protocol, pktdef, next_ser);
+}
 
 } // namespace Net
 } // namespace Msp