X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=blobdiff_plain;f=source%2Fnet%2Fprotocol.h;h=0a8110c9518182f91ada3cfe48f0f09ecd579b98;hp=75b3514bb43b2729c915902f7a33374e8b1b608c;hb=c2d202f9b482be7b439436970d6366c369837107;hpb=50e9f9ea7f7385a2c5931fca2b8fb1103078e67c diff --git a/source/net/protocol.h b/source/net/protocol.h index 75b3514..0a8110c 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,511 @@ 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 std::string describe() 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 std::string describe() const; + 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 &) { } + + std::string describe() const { return get_type_signature(); } + 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) { } + StringSerializer(const Protocol &); - virtual char *assemble(const P &p, char *d, char *e) const - { return assemble_field(p.*ptr, d, e); } + std::string describe() const { return get_type_signature(); } + char *serialize(const std::string &, char *, char *) const; + const char *deserialize(std::string &, const char *, const char *) const; + }; + + template + class ArraySerializer + { + public: + typedef A ValueType; + + private: + BasicSerializer length_serializer; + typename Traits::Serializer element_serializer; - virtual const char *disassemble(P &p, const char *d, const char *e) const - { return disassemble_field(p.*ptr, d, e); } + public: + ArraySerializer(const Protocol &); + + std::string describe() const; + char *serialize(const A &, char *, char *) const; + const char *deserialize(A &, const char *, const char *) const; }; -protected: - template - class PacketDef: public PacketDefBase + template + class CompoundSerializer { + public: + typedef C ValueType; + private: - std::vector *> fields; + const CompoundTypeDef &def; public: - PacketDef(unsigned i): PacketDefBase(i) - { if(!class_id) class_id = next_class_id++; } + 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; + }; - ~PacketDef() + 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 + std::string describe() 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; + }; + + std::string describe() const { return std::string(); } + 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 std::string describe() const = 0; + 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; } + + virtual std::string describe() const; + 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(unsigned); - const PacketDefBase &get_packet_by_class(unsigned) const; + template + PacketDefBuilder > add(); + + 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; + UInt64 get_hash() const; private: - static void assemble_header(char *, unsigned, unsigned); - template - static char *assemble_field(const T &, char *, char *); + static std::string get_type_signature(); +}; - template - static const char *disassemble_field(T &, const char *, const char *); + +template +Protocol::PacketDefBuilder > Protocol::add(unsigned 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()); + 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 +std::string Protocol::get_type_signature() +{ + const UInt16 sig = Traits::signature; + std::string result; + result += sig&0xFF; + if(sig>=0x100) + result += '0'+(sig>>8); + return result; +} + + +template +struct Protocol::BasicTraits +{ + static const UInt16 signature = K | (sizeof(T)<<8); + 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 { }; +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'; + typedef StringSerializer Serializer; +}; + +template +struct Protocol::Traits > +{ + static const UInt16 signature = 'A'; + typedef ArraySerializer > Serializer; +}; + + + +template +Protocol::CompoundDef::CompoundDef(const S &s): + serializer(new S(s)) +{ } + +template +Protocol::CompoundDef::~CompoundDef() +{ + delete serializer; +} + +template +std::string Protocol::CompoundDef::describe() const +{ + return "{"+serializer->describe()+"}"; +} + +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 +std::string Protocol::ArraySerializer::describe() const +{ + return "["+element_serializer.describe()+"]"; +} + +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::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), + ptr(p), + ser(proto) +{ } + +template +std::string Protocol::Serializer::describe() const +{ + return Head::describe()+ser.describe(); +} + +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::PacketDef

::class_id = 0; +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() +{ + delete compound; +} + +template +template +void Protocol::PacketTypeDef

::set_serializer(const S &ser) +{ + delete compound; + 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 +{ + 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