]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/protocol.h
Provide access to packet IDs in Protcool
[libs/net.git] / source / net / protocol.h
index f040a69ef1293665b3f38c79387706c04bf75e05..248b97ea5a1d14cb4fde4589ccaf355685370019 100644 (file)
@@ -7,27 +7,38 @@
 #include <stdexcept>
 #include <vector>
 #include <msp/core/hash.h>
+#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<typename T, std::uint8_t K>
        struct BasicTraits;
@@ -179,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<PacketHeader> header_def;
        unsigned next_packet_id;
        std::map<unsigned, std::unique_ptr<PacketDefBase>> packet_class_defs;
@@ -225,10 +227,19 @@ protected:
 
 public:
        template<typename P>
-       std::size_t serialize(const P &, char *, std::size_t) const;
+       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) const;
+       std::size_t dispatch(ReceiverBase &, const char *, std::size_t, unsigned = 0) const;
 
        std::uint64_t get_hash() const;
 };
@@ -264,14 +275,14 @@ const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
 }
 
 template<typename P>
-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<P> &pdef = get_packet_by_class<P>();
        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;
 }