X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=blobdiff_plain;f=source%2Fnet%2Fprotocol.h;fp=source%2Fnet%2Fprotocol.h;h=f6c382c9f1ca003e2dbb982e0d7f59867c3a5ae3;hp=75b3514bb43b2729c915902f7a33374e8b1b608c;hb=3c2a877580e234df5fcbe06bf2850cd29f875e28;hpb=b069cfc670fb78b4b429d07d2aadb59628426a25 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