]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/protocol.h
Add a base id parameter to Protocol's serialization functions
[libs/net.git] / source / net / protocol.h
index 35d3cb2cbfe177de4214816d70643f9ff97e32bd..0721ec5b5403adebb4013e54a07358700beebecc 100644 (file)
@@ -3,29 +3,31 @@
 
 #include <cstdint>
 #include <map>
+#include <memory>
 #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
 {
 private:
        template<typename T, std::uint8_t K>
@@ -142,11 +144,10 @@ private:
        class PacketTypeDef: public PacketDefBase
        {
        private:
-               Serializer<P> *serializer;
+               std::unique_ptr<Serializer<P>> serializer;
 
        public:
                PacketTypeDef(unsigned);
-               ~PacketTypeDef();
 
                unsigned get_class_id() const override { return get_packet_class_id<P>(); }
 
@@ -188,17 +189,13 @@ private:
                PacketHeader(std::uint16_t, std::uint16_t);
        };
 
-       typedef std::map<unsigned, PacketDefBase *> PacketMap;
-
        PacketTypeDef<PacketHeader> header_def;
        unsigned next_packet_id;
-       PacketMap packet_class_defs;
-       PacketMap packet_id_defs;
+       std::map<unsigned, std::unique_ptr<PacketDefBase>> packet_class_defs;
+       std::map<unsigned, PacketDefBase *> packet_id_defs;
 
 protected:
        Protocol(unsigned = 1);
-public:
-       ~Protocol();
 
 private:
        static unsigned get_next_packet_class_id();
@@ -206,7 +203,7 @@ private:
        template<typename P>
        static unsigned get_packet_class_id();
 
-       void add_packet(PacketDefBase *);
+       void add_packet(std::unique_ptr<PacketDefBase>);
 
 protected:
        template<typename P>
@@ -229,10 +226,10 @@ protected:
 
 public:
        template<typename P>
-       std::size_t serialize(const P &, char *, std::size_t) const;
+       std::size_t serialize(const P &, char *, std::size_t, unsigned = 0) 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;
 };
@@ -248,9 +245,10 @@ unsigned Protocol::get_packet_class_id()
 template<typename P>
 Protocol::PacketDefBuilder<P, Protocol::Serializer<P>> Protocol::add(unsigned id)
 {
-       PacketTypeDef<P> *pdef = new PacketTypeDef<P>(id);
-       add_packet(pdef);
-       return PacketDefBuilder<P, Serializer<P>>(*this, *pdef, Serializer<P>());
+       std::unique_ptr<PacketTypeDef<P>> pdef = std::make_unique<PacketTypeDef<P>>(id);
+       PacketDefBuilder<P, Serializer<P>> next(*this, *pdef, Serializer<P>());
+       add_packet(move(pdef));
+       return next;
 }
 
 template<typename P>
@@ -267,14 +265,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;
 }
 
@@ -413,21 +411,14 @@ const char *Protocol::FieldSerializer<C, Head, T>::deserialize(C &com, const cha
 template<typename P>
 Protocol::PacketTypeDef<P>::PacketTypeDef(unsigned i):
        PacketDefBase(i),
-       serializer(new Serializer<P>)
+       serializer(std::make_unique<Serializer<P>>())
 { }
 
-template<typename P>
-Protocol::PacketTypeDef<P>::~PacketTypeDef()
-{
-       delete serializer;
-}
-
 template<typename P>
 template<typename S>
 void Protocol::PacketTypeDef<P>::set_serializer(const S &ser)
 {
-       delete serializer;
-       serializer = new S(ser);
+       serializer = std::make_unique<S>(ser);
 }
 
 template<typename P>