00001 #ifndef _BASEPACKET_H__
00002 #define _BASEPACKET_H__
00003
00004 #include <cassert>
00005
00006 #include <awds/NodeId.h>
00007 #include <gea/Handle.h>
00008 #include <gea/UdpHandle.h>
00009
00010 namespace awds {
00011
00016 enum PacketType {
00017 PacketTypeBeacon = 0,
00018 PacketTypeFlood = 1,
00019 PacketTypeUnicast = 2,
00020 PacketTypeForward = 3
00021 };
00022
00029 class BasePacket {
00030
00031 public:
00032
00033 static const int MaxSize = 0x1000;
00034 char buffer[MaxSize];
00035 size_t size;
00040 int refcount;
00041 typedef void (*SendCallback)(BasePacket &p, void* data, ssize_t size);
00042 SendCallback cb;
00043 void *cb_data;
00044
00045 int receive(gea::Handle* h) { return (size = h->read(buffer, MaxSize)) ; }
00046 NodeId dest;
00047
00048 BasePacket() : size(0), refcount(1), cb(NULL) {
00049 buffer[0] = 0;
00050 }
00051
00052 void setSendCallback(SendCallback cb, void *data) {
00053
00054 assert(this->cb == NULL);
00055 this->cb = cb;
00056 this->cb_data = data;
00057 }
00060 int ref() { return ++refcount; }
00061
00069 int unref() {
00070 int ret;
00071 assert(refcount > 0);
00072 --refcount;
00073 ret = refcount;
00074 if (refcount == 0) {
00075 if (cb)
00076 cb(*this, cb_data, -1);
00077 delete this;
00078 }
00079 return ret;
00080 }
00081
00082 PacketType getType() const {
00083 return static_cast<PacketType>(buffer[0] & 0x03);
00084 }
00085
00086 void setType(PacketType pt) {
00087 buffer[0] = (buffer[0] & ~0x03) | static_cast<char>(pt);
00088 }
00089
00090 void setDest(const NodeId& dest) {
00091 this->dest = dest;
00092 }
00093
00094 };
00095 }
00096
00097
00098
00099
00100 #endif //_BASEPACKET_H__
00101
00102
00103
00104
00105
00106