-/* $Id$
-
-This file is part of libmspnet
-Copyright © 2009 Mikkosoft Productions, Mikko Rasa
-Distributed under the LGPL
-*/
-
#ifndef MSP_NET_PROTOCOL_H_
#define MSP_NET_PROTOCOL_H_
#include <map>
+#include <stdexcept>
#include <vector>
-#include <msp/core/except.h>
#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:
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;
};
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<typename P, typename T>
public:
PacketDef(unsigned i): PacketDefBase(i)
- { if(!class_id) class_id=next_class_id++; }
+ { if(!class_id) class_id = next_class_id++; }
~PacketDef()
{
char *assemble(const P &p, char *d, char *e) const
{
for(typename std::vector<FieldBase<P> *>::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<P> *prcv=dynamic_cast<PacketReceiver<P> *>(&r);
+ PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&r);
if(!prcv)
- throw Exception("Packet type not supported by receiver");
+ throw bad_packet("unsupported");
P pkt;
for(typename std::vector<FieldBase<P> *>::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;
}
PacketMap packet_class_defs;
PacketMap packet_id_defs;
- Protocol(unsigned =1);
+ Protocol(unsigned = 1);
public:
~Protocol();
template<typename P>
PacketDef<P> &add()
{
- PacketDef<P> *pdef=new PacketDef<P>(next_packet_id++);
+ PacketDef<P> *pdef = new PacketDef<P>(next_packet_id++);
add_packet(*pdef);
return *pdef;
}
template<typename P>
unsigned assemble(const P &pkt, char *buf, unsigned size) const
{
- unsigned id=PacketDef<P>::class_id;
- const PacketDef<P> &pdef=static_cast<const PacketDef<P> &>(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<P>::class_id;
+ const PacketDef<P> &pdef = static_cast<const PacketDef<P> &>(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;
}
};
template<typename P>
-unsigned Protocol::PacketDef<P>::class_id=0;
+unsigned Protocol::PacketDef<P>::class_id = 0;
} // namespace Net
} // namespace Msp