X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnet%2Fprotocol.cpp;h=358966f643152804ebc0e1b68a3c193179e2a4de;hb=449a2f3417748761f94f3002b1c15819c4d83365;hp=17e4310f5196c2f5c599cc103c8d8d5c6cf36430;hpb=2fbfce0c327c852d33c6713af646abf07b241108;p=libs%2Fnet.git diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index 17e4310..358966f 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -1,11 +1,9 @@ +#include "protocol.h" #include #include -#include #include #include #include -#include "protocol.h" -#include "protocol_impl.h" using namespace std; @@ -16,27 +14,24 @@ Protocol::Protocol(unsigned npi): header_def(0), next_packet_id(npi) { - PacketDefBuilder >(*this, header_def, NullSerializer()) - (&PacketHeader::type)(&PacketHeader::length); + PacketDefBuilder>(*this, header_def, Serializer()) + .fields(&PacketHeader::type, &PacketHeader::length); } -Protocol::~Protocol() +unsigned Protocol::get_next_packet_class_id() { - for(map::iterator i=packet_class_defs.begin(); i!=packet_class_defs.end(); ++i) - delete i->second; + static unsigned next_id = 1; + return next_id++; } -void Protocol::add_packet(PacketDefBase *pdef) +void Protocol::add_packet(unique_ptr pdef) { - PacketDefBase *&ptr = packet_class_defs[pdef->get_class_id()]; + unique_ptr &ptr = packet_class_defs[pdef->get_class_id()]; if(ptr) - { packet_id_defs.erase(ptr->get_id()); - delete ptr; - } - ptr = pdef; - if(unsigned id = pdef->get_id()) - packet_id_defs[id] = pdef; + ptr = move(pdef); + if(unsigned id = ptr->get_id()) + packet_id_defs[id] = ptr.get(); } const Protocol::PacketDefBase &Protocol::get_packet_by_class_id(unsigned id) const @@ -49,32 +44,47 @@ const Protocol::PacketDefBase &Protocol::get_packet_by_id(unsigned id) const return *get_item(packet_id_defs, id); } -size_t Protocol::dispatch(ReceiverBase &rcv, const char *buf, size_t size) const +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; - 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); + const PacketDefBase &pdef = get_packet_by_id(header.type-base_id); + ptr = pdef.dispatch(rcv, ptr, ptr+header.length); 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 { - 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 +164,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)