X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=blobdiff_plain;f=source%2Fnet%2Fprotocol.h;h=5051338f3911558c8e2ee19fe2d0665c0e10eec3;hp=f040a69ef1293665b3f38c79387706c04bf75e05;hb=HEAD;hpb=ede42d5bb352841e2e425972e12b8ef31ddf2123 diff --git a/source/net/protocol.h b/source/net/protocol.h index f040a69..5051338 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -7,27 +7,38 @@ #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 { +public: + struct PacketHeader + { + std::uint16_t type = 0; + std::uint16_t length = 0; + + PacketHeader() = default; + PacketHeader(std::uint16_t, std::uint16_t); + }; + private: template struct BasicTraits; @@ -136,6 +147,7 @@ private: virtual unsigned get_class_id() const = 0; unsigned get_id() const { return id; } virtual std::uint64_t get_hash() const = 0; + virtual const char *deserialize(Variant &, const char *, const char *) const = 0; virtual const char *dispatch(ReceiverBase &, const char *, const char *) const = 0; }; @@ -158,6 +170,7 @@ private: std::uint64_t get_hash() const override { return serializer->get_hash(); } char *serialize(const P &, char *, char *) const; const char *deserialize(P &, const char *, const char *) const; + const char *deserialize(Variant &, const char *, const char *) const override; const char *dispatch(ReceiverBase &, const char *, const char *) const override; }; @@ -179,22 +192,13 @@ private: auto fields(T1 P::*first, T2 P::*second, Rest P::*...rest) { return fields(first).fields(second, rest...); } }; - struct PacketHeader - { - std::uint16_t type = 0; - std::uint16_t length = 0; - - PacketHeader() = default; - PacketHeader(std::uint16_t, std::uint16_t); - }; - PacketTypeDef header_def; - unsigned next_packet_id; + unsigned next_packet_id = 1; std::map> packet_class_defs; std::map packet_id_defs; protected: - Protocol(unsigned = 1); + Protocol(); private: static unsigned get_next_packet_class_id(); @@ -205,12 +209,6 @@ private: 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(); @@ -225,10 +223,19 @@ protected: public: template - std::size_t serialize(const P &, char *, std::size_t) const; + bool has_packet() const { return packet_class_defs.count(get_packet_class_id

()); } + + template + unsigned get_packet_id() const { return get_item(packet_class_defs, get_packet_class_id

())->get_id(); } + unsigned get_max_packet_id() const; + + template + std::size_t serialize(const P &, char *, std::size_t, unsigned = 0) const; + + bool get_packet_header(PacketHeader &, const char *, std::size_t) 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; }; @@ -242,20 +249,14 @@ unsigned Protocol::get_packet_class_id() } template -Protocol::PacketDefBuilder> Protocol::add(unsigned id) +Protocol::PacketDefBuilder> Protocol::add() { - std::unique_ptr> pdef = std::make_unique>(id); + std::unique_ptr> pdef = std::make_unique>(next_packet_id++); PacketDefBuilder> next(*this, *pdef, Serializer

()); add_packet(move(pdef)); return next; } -template -Protocol::PacketDefBuilder> Protocol::add() -{ - return add

(next_packet_id++); -} - template const Protocol::PacketTypeDef

&Protocol::get_packet_by_class() const { @@ -264,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; } @@ -432,6 +433,15 @@ const char *Protocol::PacketTypeDef

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

::deserialize(Variant &var_pkt, const char *buf, const char *end) const +{ + P pkt; + const char *ptr = serializer->deserialize(pkt, buf, end); + var_pkt = std::move(pkt); + return ptr; +} + template const char *Protocol::PacketTypeDef

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