Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef MESSAGERAWHEADER_H
00027 #define MESSAGERAWHEADER_H
00028
00029 #include "GpCoreToolsDLLExport.h"
00030 #include "FletcherChecksum.h"
00031 #include "ByteOrder.h"
00032
00033 namespace GpCoreTools {
00034
00035 #pragma pack(1)
00036
00037 class GPCORETOOLS_EXPORT MessageRawHeader
00038 {
00039 public:
00040 MessageRawHeader() {}
00041 inline MessageRawHeader(unsigned int length);
00042 inline MessageRawHeader(const MessageRawHeader& o);
00043
00044 enum {SynchronizationSize=2, HeaderSize=2, CheckSumSize=2};
00045
00046 bool isValid() const {return _sync[0]==0xB5 && _sync[1]==0x62;}
00047 inline bool isValid(const FletcherChecksum& cksum, const char * payload=0,
00048 unsigned short payloadLength=0) const;
00049
00050 inline void littleEndianValues();
00051 inline void bigEndianValues();
00052
00053 const char * header() const {return _bytes;}
00054
00055 unsigned int lengthLittleEndian() const {return ByteOrder::littleEndianToNative(_values.length);}
00056 unsigned int lengthBigEndian() const {return ByteOrder::bigEndianToNative(_values.length);}
00057 protected:
00058 unsigned char _sync[2];
00059 union {
00060 struct {
00061 unsigned short length;
00062 } _values;
00063 char _bytes[HeaderSize];
00064 };
00065 };
00066
00067 inline MessageRawHeader::MessageRawHeader(unsigned int length)
00068 {
00069 _sync[0]=0xB5;
00070 _sync[1]=0x62;
00071 _values.length=length;
00072 }
00073
00074 inline MessageRawHeader::MessageRawHeader(const MessageRawHeader& o)
00075 {
00076 _sync[0]=o._sync[0];
00077 _sync[1]=o._sync[1];
00078 _values.length=o._values.length;
00079 }
00080
00081 inline void MessageRawHeader::bigEndianValues()
00082 {
00083 #if BYTE_ORDER==LITTLE_ENDIAN
00084 ByteOrder::swap(_bytes[0], _bytes[1]);
00085 #endif
00086 }
00087
00088 inline void MessageRawHeader::littleEndianValues()
00089 {
00090 #if BYTE_ORDER==BIG_ENDIAN
00091 ByteOrder::swap(_bytes[0], _bytes[1]);
00092 #endif
00093 }
00094
00095 inline bool MessageRawHeader::isValid(const FletcherChecksum& cksum, const char * payload,
00096 unsigned short payloadLength) const
00097 {
00098 FletcherChecksum mycs;
00099 mycs.add(reinterpret_cast<const char *>(header()), HeaderSize);
00100 if(payload) {
00101 mycs.add(payload, payloadLength);
00102 }
00103 return cksum==mycs;
00104 }
00105
00106 #pragma pack() // restore original alignment
00107
00108 }
00109
00110 #endif // MESSAGERAWHEADER_H