return *get_item(packet_id_defs, id);
}
+unsigned Protocol::get_max_packet_id() const
+{
+ if(packet_id_defs.empty())
+ return 0;
+ return prev(packet_id_defs.end())->first;
+}
+
size_t Protocol::dispatch(ReceiverBase &rcv, const char *buf, size_t size, unsigned base_id) const
{
PacketHeader header;
return ptr-buf;
}
-size_t Protocol::get_packet_size(const char *buf, size_t size) const
+bool Protocol::get_packet_header(PacketHeader &header, const char *buf, size_t size) const
{
if(size<4)
- return 0;
- PacketHeader header;
+ return false;
header_def.deserialize(header, buf, buf+size);
- return header.length;
+ return true;
+}
+
+size_t Protocol::get_packet_size(const char *buf, size_t size) const
+{
+ PacketHeader header;
+ return (get_packet_header(header, buf, size) ? header.length : 0);
}
uint64_t Protocol::get_hash() const
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<typename T, std::uint8_t K>
struct BasicTraits;
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<PacketHeader> header_def;
unsigned next_packet_id;
std::map<unsigned, std::unique_ptr<PacketDefBase>> packet_class_defs;
const PacketTypeDef<P> &get_packet_by_class() const;
public:
+ template<typename P>
+ bool has_packet() const { return packet_class_defs.count(get_packet_class_id<P>()); }
+
+ template<typename P>
+ unsigned get_packet_id() const { return get_item(packet_class_defs, get_packet_class_id<P>())->get_id(); }
+
+ unsigned get_max_packet_id() const;
+
template<typename P>
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;