UniSet  1.4.0
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     static const size_t MaxACount = 200;
00045     static const size_t MaxDCount = 400;
00046     static const size_t MaxDDataCount = MaxDCount / sizeof(unsigned char);
00047 
00048     struct UDPPacket
00049     {
00050         UDPPacket():len(0){}
00051 
00052         int len;
00053         unsigned char data[ sizeof(UDPHeader) + MaxDCount*sizeof(long) + MaxDDataCount + MaxACount*sizeof(UDPAData) ];
00054     }__attribute__((packed));
00055 
00056     static const int MaxDataLen = sizeof(UDPPacket);
00057 
00058     struct UDPMessage:
00059         public UDPHeader
00060     {
00061         UDPMessage();
00062 
00063         UDPMessage( UDPPacket& p );
00064         size_t transport_msg( UDPPacket& p );
00065         static size_t getMessage( UDPMessage& m, UDPPacket& p );
00066 
00067         size_t addDData( long id, bool val );
00068         bool setDData( size_t index, bool val );
00069         long dID( size_t index );
00070         bool dValue( size_t index );
00071         
00072         size_t addAData( const UDPAData& dat );
00073         size_t addAData( long id, long val );
00074         bool setAData( size_t index, long val );
00075 
00076         inline bool isFull(){ return ((dcount<MaxDCount) && (acount<MaxACount)); }
00077         inline int dsize(){ return dcount; }
00078         inline int asize(){ return acount; }
00079 //      inline int byte_size(){ return (dcount*sizeof(long)*UDPDData) + acount*sizeof(UDPAData)); }
00080         
00081         // количество байт в пакете с булевыми переменными...
00082         int d_byte(){ return dcount*sizeof(long) + dcount; }
00083         
00084         UDPAData a_dat[MaxACount]; 
00085         long d_id[MaxDCount];      
00086         unsigned char d_dat[MaxDDataCount];  
00088         friend std::ostream& operator<<( std::ostream& os, UDPMessage& p );
00089     };
00090 }
00091 // -----------------------------------------------------------------------------
00092 #endif // UDPPacket_H_
00093 // -----------------------------------------------------------------------------