X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnet%2Fprotocol.h;h=248b97ea5a1d14cb4fde4589ccaf355685370019;hb=f17a55dc7fc44d1516db445550f55ed31e7534fa;hp=8068d3275ca292eea2317e9847725f90a0be7524;hpb=394c9a732192fce9b3b453dfdb9e92400af2a4f8;p=libs%2Fnet.git diff --git a/source/net/protocol.h b/source/net/protocol.h index 8068d32..248b97e 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,15 +190,6 @@ 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; std::map> packet_class_defs; @@ -226,10 +227,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; }; @@ -265,14 +275,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; }