X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnet%2Fprotocol.h;h=0721ec5b5403adebb4013e54a07358700beebecc;hb=c2e9e03b191a6ffe44a83be32aadf2a325491c02;hp=ae1c3c010c0671a8c88968b359220f9142449a92;hpb=1c300cbe77a7084951a080020361fc45bd190253;p=libs%2Fnet.git diff --git a/source/net/protocol.h b/source/net/protocol.h index ae1c3c0..0721ec5 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -3,29 +3,31 @@ #include #include +#include #include #include #include +#include "mspnet_api.h" #include "receiver.h" namespace Msp { namespace Net { -class bad_packet: public std::runtime_error +class MSPNET_API bad_packet: public std::runtime_error { public: bad_packet(const std::string &w): std::runtime_error(w) { } }; -class buffer_error: public std::runtime_error +class MSPNET_API buffer_error: public std::runtime_error { public: buffer_error(const std::string &w): std::runtime_error(w) { } }; -class Protocol +class MSPNET_API Protocol { private: template @@ -142,11 +144,10 @@ private: class PacketTypeDef: public PacketDefBase { private: - Serializer

*serializer; + std::unique_ptr> serializer; public: PacketTypeDef(unsigned); - ~PacketTypeDef(); unsigned get_class_id() const override { return get_packet_class_id

(); } @@ -171,9 +172,12 @@ private: public: PacketDefBuilder(const Protocol &, PacketTypeDef

&, const S &); - + template - PacketDefBuilder> operator()(T P::*); + PacketDefBuilder> fields(T P::*); + + template + auto fields(T1 P::*first, T2 P::*second, Rest P::*...rest) { return fields(first).fields(second, rest...); } }; struct PacketHeader @@ -185,17 +189,13 @@ private: PacketHeader(std::uint16_t, std::uint16_t); }; - typedef std::map PacketMap; - PacketTypeDef header_def; unsigned next_packet_id; - PacketMap packet_class_defs; - PacketMap packet_id_defs; + std::map> packet_class_defs; + std::map packet_id_defs; protected: Protocol(unsigned = 1); -public: - ~Protocol(); private: static unsigned get_next_packet_class_id(); @@ -203,15 +203,21 @@ private: template static unsigned get_packet_class_id(); - void add_packet(PacketDefBase *); + void add_packet(std::unique_ptr); protected: template PacketDefBuilder> add(unsigned); + template + auto add(unsigned id, T P::*field, Rest P::*...rest) { return add

(id).fields(field, rest...); } + template PacketDefBuilder> add(); + template + auto add(T P::*field, Rest P::*...rest) { return add

().fields(field, rest...); } + const PacketDefBase &get_packet_by_class_id(unsigned) const; const PacketDefBase &get_packet_by_id(unsigned) const; @@ -220,10 +226,10 @@ protected: public: template - std::size_t serialize(const P &, char *, std::size_t) const; + std::size_t serialize(const P &, char *, std::size_t, unsigned = 0) const; std::size_t get_packet_size(const char *, std::size_t) const; - std::size_t dispatch(ReceiverBase &, const char *, std::size_t) const; + std::size_t dispatch(ReceiverBase &, const char *, std::size_t, unsigned = 0) const; std::uint64_t get_hash() const; }; @@ -239,9 +245,10 @@ unsigned Protocol::get_packet_class_id() template Protocol::PacketDefBuilder> Protocol::add(unsigned id) { - PacketTypeDef

*pdef = new PacketTypeDef

(id); - add_packet(pdef); - return PacketDefBuilder>(*this, *pdef, Serializer

()); + std::unique_ptr> pdef = std::make_unique>(id); + PacketDefBuilder> next(*this, *pdef, Serializer

()); + add_packet(move(pdef)); + return next; } template @@ -258,14 +265,14 @@ const Protocol::PacketTypeDef

&Protocol::get_packet_by_class() const } template -std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const +std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size, unsigned base_id) 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); + header_def.serialize(PacketHeader(base_id+pdef.get_id(), size), buf, buf+4); return size; } @@ -404,21 +411,14 @@ const char *Protocol::FieldSerializer::deserialize(C &com, const cha template Protocol::PacketTypeDef

::PacketTypeDef(unsigned i): PacketDefBase(i), - serializer(new Serializer

) + serializer(std::make_unique>()) { } -template -Protocol::PacketTypeDef

::~PacketTypeDef() -{ - delete serializer; -} - template template void Protocol::PacketTypeDef

::set_serializer(const S &ser) { - delete serializer; - serializer = new S(ser); + serializer = std::make_unique(ser); } template @@ -455,7 +455,7 @@ Protocol::PacketDefBuilder::PacketDefBuilder(const Protocol &p, PacketType template template -Protocol::PacketDefBuilder> Protocol::PacketDefBuilder::operator()(T P::*ptr) +Protocol::PacketDefBuilder> Protocol::PacketDefBuilder::fields(T P::*ptr) { typename S::template Next next_ser(serializer, ptr, protocol); pktdef.set_serializer(next_ser);