消息类
用于存储消息内容以及消息内容读写相关函数。
1 #ifndef __cooper_message_h_ 2 #define __cooper_message_h_ 3 4 #include "cooper_common.h" 5 6 //----------------------------------------------------------------------------- 7 8 class CooperMessage 9 { 10 public: 11 enum { header_length = 60 }; 12 enum { max_body_length = (2 * kSubvolUnitSize * kMaxMemberNumber) }; 13 14 CooperMessage() : data_(NULL), body_length_(header_length) 15 { 16 printf("msg(%llx) constructor 0\n", this); 17 } 18 19 CooperMessage(const string& dst_ip, uint type, const char* buf, uint buf_length) 20 { 21 if(InitMsgData(buf_length)) { 22 SetMsg(dst_ip, type, buf); 23 } else { 24 data_ = NULL; 25 body_length_ = 0; 26 } 27 28 printf("msg(%llx) constructor 1\n", this); 29 } 30 31 CooperMessage(const CooperMessage& msg) 32 { 33 body_length_ = msg.get_data_length() - header_length; 34 35 if (msg.get_data() != NULL) { 36 data_ = new char[msg.get_data_length()]; 37 memcpy(data_, msg.get_data(), msg.get_data_length()); 38 } 39 40 printf("deep copy msg\n"); 41 } 42 43 ~CooperMessage() 44 { 45 if (data_ != NULL) { 46 delete[] data_; 47 data_ = NULL; 48 } 49 50 printf("msg(%llx) destructor\n", this); 51 } 52 53 bool InitMsg(const char* header, uint buf_length) 54 { 55 if (!InitMsgData(buf_length)) { 56 return false; 57 } 58 59 memcpy(data_, header, header_length); 60 return true; 61 } 62 63 uint get_data_length() const { return header_length + body_length_; } 64 const char* get_src_ip() const { return data_; } 65 const char* get_dst_ip() const { return data_ + 16; } 66 uint get_type() const { return *(uint*)(data_ + 32); } 67 uint get_timestamp_second() const { return *(uint*)(data_ + 36); } 68 ull get_timestamp_microsec() const { return *(ull*)(data_ + 40); } 69 uint get_body_length() const { return *(uint*)(data_ + 48);} 70 uint get_body_length(const char* header) { return *(uint*)(header + 48);} 71 ull get_body_crc32() const { return *(ull*)(data_ + 52); } 72 const char* get_data() const { return data_; } 73 char* get_body() const { return data_ + header_length; } 74 75 private: 76 bool InitMsgData(uint buf_length) 77 { 78 if (buf_length > max_body_length) { 79 return false; 80 } 81 82 body_length_ = buf_length; 83 data_ = new char[header_length + buf_length]; 84 memset(data_, 0, header_length + buf_length); 85 return true; 86 } 87 88 void SetMsg(const string& dst_ip, uint type, const char* buf) 89 { 90 // |0- src_ip -|16- dst_ip -|32- type -|36- sec -|40- microsec -|48- body_length -|52- crc32 -| 91 uint seconds = CooperCommon::get_instance()->GetCurrentSecond(); 92 ull microsecs = CooperCommon::get_instance()->GetCurrentMicrosec(); 93 ull crc32 = CooperCommon::get_instance()->ChecksumCRC32(buf, body_length_); 94 printf("msg seconds=%u, microsecs=%llu, crc32=%llu\n", seconds, microsecs, crc32); 95 96 memcpy(data_, CooperCommon::get_instance()->local_ip_.c_str(), 16); 97 memcpy(data_ + 16, dst_ip.c_str(), 16); 98 memcpy(data_ + 32, (char*)&type, 4); 99 memcpy(data_ + 36, (char*)&seconds, 4); 100 memcpy(data_ + 40, (char*)µsecs, 8); 101 memcpy(data_ + 48, (char*)&body_length_, 4); 102 memcpy(data_ + 52, (char*)&crc32, 8); 103 104 if (buf != NULL) 105 memcpy(data_ + 60, buf, body_length_); 106 } 107 108 private: 109 uint body_length_; 110 char* data_; 111 }; 112 113 typedef boost::shared_ptr<CooperMessage> msg_ptr; 114 115 #endif // __cooper_message_h_