]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/protocol.cpp
Refactor the API for defining packets in Protocol
[libs/net.git] / source / net / protocol.cpp
index 17e4310f5196c2f5c599cc103c8d8d5c6cf36430..ce200a2c0ea49afed1f320b0914c252a094669bd 100644 (file)
@@ -1,11 +1,9 @@
 #include <cstring>
 #include <string>
-#include <msp/core/hash.h>
 #include <msp/core/maputils.h>
 #include <msp/strings/format.h>
 #include <msp/strings/lexicalcast.h>
 #include "protocol.h"
-#include "protocol_impl.h"
 
 using namespace std;
 
@@ -16,14 +14,20 @@ Protocol::Protocol(unsigned npi):
        header_def(0),
        next_packet_id(npi)
 {
-       PacketDefBuilder<PacketHeader, NullSerializer<PacketHeader> >(*this, header_def, NullSerializer<PacketHeader>())
-               (&PacketHeader::type)(&PacketHeader::length);
+       PacketDefBuilder<PacketHeader, Serializer<PacketHeader>>(*this, header_def, Serializer<PacketHeader>())
+               .fields(&PacketHeader::type, &PacketHeader::length);
 }
 
 Protocol::~Protocol()
 {
-       for(map<unsigned, PacketDefBase *>::iterator i=packet_class_defs.begin(); i!=packet_class_defs.end(); ++i)
-               delete i->second;
+       for(auto &kvp: packet_class_defs)
+               delete kvp.second;
+}
+
+unsigned Protocol::get_next_packet_class_id()
+{
+       static unsigned next_id = 1;
+       return next_id++;
 }
 
 void Protocol::add_packet(PacketDefBase *pdef)
@@ -52,11 +56,11 @@ const Protocol::PacketDefBase &Protocol::get_packet_by_id(unsigned id) const
 size_t Protocol::dispatch(ReceiverBase &rcv, const char *buf, size_t size) const
 {
        PacketHeader header;
-       buf = header_def.deserialize(header, buf, buf+size);
+       const char *ptr = header_def.deserialize(header, buf, buf+size);
        if(header.length>size)
                throw bad_packet("truncated");
        const PacketDefBase &pdef = get_packet_by_id(header.type);
-       const char *ptr = pdef.dispatch(rcv, buf, buf+header.length);
+       ptr = pdef.dispatch(rcv, ptr, ptr+header.length);
        return ptr-buf;
 }
 
@@ -71,10 +75,13 @@ size_t Protocol::get_packet_size(const char *buf, size_t size) const
 
 uint64_t Protocol::get_hash() const
 {
-       string description;
-       for(PacketMap::const_iterator i=packet_id_defs.begin(); i!=packet_id_defs.end(); ++i)
-               description += format("%d:%s\n", i->first, i->second->describe());
-       return hash<64>(description);
+       uint64_t result = hash<64>(packet_id_defs.size());
+       for(auto &kvp: packet_id_defs)
+       {
+               hash_update<64>(result, kvp.first);
+               hash_update<64>(result, kvp.second->get_hash());
+       }
+       return result;
 }
 
 
@@ -154,18 +161,11 @@ const char *Protocol::StringSerializer::deserialize(string &str, const char *buf
 }
 
 
-unsigned Protocol::PacketDefBase::next_class_id = 1;
-
 Protocol::PacketDefBase::PacketDefBase(unsigned i):
        id(i)
 { }
 
 
-Protocol::PacketHeader::PacketHeader():
-       type(0),
-       length(0)
-{ }
-
 Protocol::PacketHeader::PacketHeader(uint16_t t, uint16_t l):
        type(t),
        length(l)