X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnet%2Fprotocol.h;h=e766733cb6a6373839ecefd32c8e95dd9eb24d52;hb=3f46fef7032d97b0dd82971ffece1062fd6b05b8;hp=0721ec5b5403adebb4013e54a07358700beebecc;hpb=c2e9e03b191a6ffe44a83be32aadf2a325491c02;p=libs%2Fnet.git diff --git a/source/net/protocol.h b/source/net/protocol.h index 0721ec5..e766733 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -29,6 +29,16 @@ public: 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; @@ -180,22 +190,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(); @@ -206,12 +207,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,9 +220,18 @@ protected: const PacketTypeDef

&get_packet_by_class() const; public: + template + 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, unsigned = 0) const; @@ -243,20 +247,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 {