X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprotocol.h;h=75b3514bb43b2729c915902f7a33374e8b1b608c;hb=21c596567e3b6fd794ed6af73d304ce2bc70e58f;hp=8ae5cdd149170c0bfdc0e5dcf3342e226ff27946;hpb=2aab4004e71a2e5c773289e0be5e58aec6a8d339;p=libs%2Fnet.git diff --git a/source/protocol.h b/source/protocol.h index 8ae5cdd..75b3514 100644 --- a/source/protocol.h +++ b/source/protocol.h @@ -2,13 +2,29 @@ #define MSP_NET_PROTOCOL_H_ #include +#include #include -#include #include "receiver.h" namespace Msp { namespace Net { +class bad_packet: public std::runtime_error +{ +public: + bad_packet(const std::string &w): std::runtime_error(w) { } + virtual ~bad_packet() throw() { } +}; + + +class buffer_error: public std::runtime_error +{ +public: + buffer_error(const std::string &w): std::runtime_error(w) { } + virtual ~buffer_error() throw() { } +}; + + class Protocol { private: @@ -20,9 +36,9 @@ private: PacketDefBase(unsigned i): id(i) { } public: virtual ~PacketDefBase() { } - virtual unsigned get_class_id() const =0; + virtual unsigned get_class_id() const = 0; unsigned get_id() const { return id; } - virtual const char *disassemble(ReceiverBase &, const char *, const char *) const =0; + virtual const char *disassemble(ReceiverBase &, const char *, const char *) const = 0; static unsigned next_class_id; }; @@ -34,8 +50,8 @@ private: FieldBase() { } public: virtual ~FieldBase() { } - virtual char *assemble(const P &, char *, char *) const =0; - virtual const char *disassemble(P &, const char *, const char *) const =0; + virtual char *assemble(const P &, char *, char *) const = 0; + virtual const char *disassemble(P &, const char *, const char *) const = 0; }; template @@ -63,7 +79,7 @@ protected: public: PacketDef(unsigned i): PacketDefBase(i) - { if(!class_id) class_id=next_class_id++; } + { if(!class_id) class_id = next_class_id++; } ~PacketDef() { @@ -80,18 +96,18 @@ protected: char *assemble(const P &p, char *d, char *e) const { for(typename std::vector *>::const_iterator i=fields.begin(); i!=fields.end(); ++i) - d=(*i)->assemble(p, d, e); + d = (*i)->assemble(p, d, e); return d; } const char *disassemble(ReceiverBase &r, const char *d, const char *e) const { - PacketReceiver

*prcv=dynamic_cast *>(&r); + PacketReceiver

*prcv = dynamic_cast *>(&r); if(!prcv) - throw Exception("Packet type not supported by receiver"); + throw bad_packet("unsupported"); P pkt; for(typename std::vector *>::const_iterator i=fields.begin(); i!=fields.end(); ++i) - d=(*i)->disassemble(pkt, d, e); + d = (*i)->disassemble(pkt, d, e); prcv->receive(pkt); return d; } @@ -105,7 +121,7 @@ protected: PacketMap packet_class_defs; PacketMap packet_id_defs; - Protocol(unsigned =1); + Protocol(unsigned = 1); public: ~Protocol(); @@ -116,7 +132,7 @@ protected: template PacketDef

&add() { - PacketDef

*pdef=new PacketDef

(next_packet_id++); + PacketDef

*pdef = new PacketDef

(next_packet_id++); add_packet(*pdef); return *pdef; } @@ -128,10 +144,10 @@ public: template unsigned assemble(const P &pkt, char *buf, unsigned size) const { - unsigned id=PacketDef

::class_id; - const PacketDef

&pdef=static_cast &>(get_packet_by_class(id)); - char *ptr=pdef.assemble(pkt, buf+4, buf+size); - assemble_header(buf, pdef.get_id(), (size=ptr-buf)); + unsigned id = PacketDef

::class_id; + const PacketDef

&pdef = static_cast &>(get_packet_by_class(id)); + char *ptr = pdef.assemble(pkt, buf+4, buf+size); + assemble_header(buf, pdef.get_id(), (size = ptr-buf)); return size; } @@ -152,7 +168,7 @@ private: }; template -unsigned Protocol::PacketDef

::class_id=0; +unsigned Protocol::PacketDef

::class_id = 0; } // namespace Net } // namespace Msp