include/UDPPacket.h
00001 #ifndef UDPPacket_H_
00002 #define UDPPacket_H_
00003
00004 #include <list>
00005 #include <limits>
00006 #include <ostream>
00007 #include "UniSetTypes.h"
00008
00009 namespace UniSetUDP
00010 {
00018 struct UDPHeader
00019 {
00020 UDPHeader():num(0),nodeID(0),procID(0),dcount(0),acount(0){}
00021 unsigned long num;
00022 long nodeID;
00023 long procID;
00024 size_t dcount;
00025 size_t acount;
00027 friend std::ostream& operator<<( std::ostream& os, UDPHeader& p );
00028 friend std::ostream& operator<<( std::ostream& os, UDPHeader* p );
00029 }__attribute__((packed));
00030
00031 static unsigned long MaxPacketNum = std::numeric_limits<unsigned long>::max();
00032
00033 struct UDPAData
00034 {
00035 UDPAData():id(UniSetTypes::DefaultObjectId),val(0){}
00036 UDPAData(long id, long val):id(id),val(val){}
00037
00038 long id;
00039 long val;
00040
00041 friend std::ostream& operator<<( std::ostream& os, UDPAData& p );
00042 }__attribute__((packed));
00043
00044
00045
00046 static const size_t MaxACount = 550;
00047 static const size_t MaxDCount = 900;
00048 static const size_t MaxDDataCount = 1 + MaxDCount / 8*sizeof(unsigned char);
00049
00050 struct UDPPacket
00051 {
00052 UDPPacket():len(0){}
00053
00054 int len;
00055 unsigned char data[ sizeof(UDPHeader) + MaxDCount*sizeof(long) + MaxDDataCount + MaxACount*sizeof(UDPAData) ];
00056 }__attribute__((packed));
00057
00058 static const int MaxDataLen = sizeof(UDPPacket);
00059
00060 struct UDPMessage:
00061 public UDPHeader
00062 {
00063 UDPMessage();
00064
00065 UDPMessage( UDPPacket& p );
00066 size_t transport_msg( UDPPacket& p );
00067 static size_t getMessage( UDPMessage& m, UDPPacket& p );
00068
00069 size_t addDData( long id, bool val );
00070 bool setDData( size_t index, bool val );
00071 long dID( size_t index );
00072 bool dValue( size_t index );
00073
00074 size_t addAData( const UDPAData& dat );
00075 size_t addAData( long id, long val );
00076 bool setAData( size_t index, long val );
00077
00078 inline bool isFull(){ return ((dcount<MaxDCount) && (acount<MaxACount)); }
00079 inline int dsize(){ return dcount; }
00080 inline int asize(){ return acount; }
00081
00082
00083
00084 int d_byte(){ return dcount*sizeof(long) + dcount; }
00085
00086 UDPAData a_dat[MaxACount];
00087 long d_id[MaxDCount];
00088 unsigned char d_dat[MaxDDataCount];
00090 friend std::ostream& operator<<( std::ostream& os, UDPMessage& p );
00091 };
00092 }
00093
00094 #endif // UDPPacket_H_
00095