X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnet%2Fprotocol.cpp;h=c6112923c9692db7f4b646f11d68e0a8e6e753a0;hb=c2e9e03b191a6ffe44a83be32aadf2a325491c02;hp=bcad032b4cb4fcc86a9de9702312806bf640b3b3;hpb=6625cf0e574406425c35d33110f8d99617e902df;p=libs%2Fnet.git diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index bcad032..c611292 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -1,9 +1,9 @@ +#include "protocol.h" #include #include #include #include #include -#include "protocol.h" using namespace std; @@ -14,14 +14,8 @@ Protocol::Protocol(unsigned npi): header_def(0), next_packet_id(npi) { - PacketDefBuilder >(*this, header_def, NullSerializer()) - (&PacketHeader::type)(&PacketHeader::length); -} - -Protocol::~Protocol() -{ - for(auto &kvp: packet_class_defs) - delete kvp.second; + PacketDefBuilder>(*this, header_def, Serializer()) + .fields(&PacketHeader::type, &PacketHeader::length); } unsigned Protocol::get_next_packet_class_id() @@ -30,17 +24,14 @@ unsigned Protocol::get_next_packet_class_id() 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 @@ -53,14 +44,14 @@ 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 +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; }