DDS  ver. 1.6
ProtocolMessage.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 #ifndef __DDS__ProtocolMessage__
6 #define __DDS__ProtocolMessage__
7 // DDS
8 #include "def.h"
9 // STD
10 #include <cstring>
11 #include <memory>
12 // BOOST
13 #include <boost/crc.hpp>
14 
15 namespace dds
16 {
17  namespace protocol_api
18  {
19  // a very simple protocol
20  // |>>>>>>>>>>>>>>>>>>>>>>>>>>>> HEADER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|>>>>>>>>>>>>>>>> DATA
21  // <<<<<<<<<<<<<<<<<<|
22  // | HEADER CHECKSUM(2) uint16_t | CMD (2) uint16_t | LEN (4) uint32_t | DATA BLOCK (size of LEN) unsigned char
23  // |
24  //----------------------------------------------------------------------
26  {
28  : m_cmd(0)
29  , m_len(0)
30  {
31  }
32 
33  bool isValid() const
34  {
35  return (getChecksum() == m_crc);
36  }
37  uint16_t getChecksum() const
38  {
39  // Calculate header's checksum.
40  // In order to do that, we of course should exclude m_crc from the calculation.
41  unsigned char const* buf = reinterpret_cast<unsigned char const*>(this);
42  boost::crc_16_type crc16;
43  crc16.process_bytes(buf + sizeof(uint16_t), sizeof(SMessageHeader) - sizeof(uint16_t));
44 
45  return crc16.checksum();
46  }
47 
48  void clear()
49  {
50  m_cmd = 0;
51  m_len = 0;
52  }
53 
54  // !!! IMPORTANT. The checksum member should be always on the top of the member's list !!!
55  // we exclude it from checksum calculations.
56  uint16_t m_crc;
58  uint16_t m_cmd;
59  uint32_t m_len;
60  };
61 
62  //----------------------------------------------------------------------
63 
65  {
66  public:
68  typedef dataContainer_t::value_type data_t;
69  typedef std::shared_ptr<CProtocolMessage> protocolMessagePtr_t;
70 
71  enum
72  {
73  cmd_sign_length = 2
74  };
75  enum
76  {
77  header_length = sizeof(SMessageHeader)
78  };
79  /* enum
80  {
81  max_body_length = 512
82  };*/
83 
84  public:
86 
87  CProtocolMessage(uint16_t _cmd, const MiscCommon::BYTEVector_t& _data);
88 
89  public:
90  void encode(uint16_t _cmd, const MiscCommon::BYTEVector_t& _data)
91  {
92  _encode_message(_cmd, _data);
93  }
94 
95  void clear();
96  void resize(size_t _size); // FIXME: Used in tests to allocate memory for m_data.
97  const data_t* data() const;
98  data_t* data();
99  size_t length() const;
100  const data_t* body() const;
101  data_t* body();
102  size_t body_length() const;
103  bool decode_header();
104  const SMessageHeader& header() const;
105  std::string toString() const;
106  dataContainer_t bodyToContainer() const
107  {
108  dataContainer_t buf(body(), body() + body_length());
109  return buf;
110  }
111 
112  private:
113  void _encode_message(uint16_t _cmd, const dataContainer_t& _data);
114 
115  private:
116  dataContainer_t m_data;
117  SMessageHeader m_header;
118  };
119  }
120 }
121 
122 #endif /* defined(__DDS__ProtocolMessage__) */
void clear()
Definition: ProtocolMessage.h:48
uint32_t m_len
Definition: ProtocolMessage.h:59
uint16_t getChecksum() const
Definition: ProtocolMessage.h:37
Definition: ProtocolMessage.h:64
uint16_t m_crc
Definition: ProtocolMessage.h:56
dataContainer_t bodyToContainer() const
Definition: ProtocolMessage.h:106
uint16_t m_cmd
Definition: ProtocolMessage.h:58
bool isValid() const
Definition: ProtocolMessage.h:33
Definition: dds-agent/src/AgentConnectionManager.h:16
void encode(uint16_t _cmd, const MiscCommon::BYTEVector_t &_data)
Definition: ProtocolMessage.h:90
std::vector< unsigned char > BYTEVector_t
An STL vector of bytes.
Definition: def.h:127
MiscCommon::BYTEVector_t dataContainer_t
Definition: ProtocolMessage.h:67
SMessageHeader()
Definition: ProtocolMessage.h:27
dataContainer_t::value_type data_t
Definition: ProtocolMessage.h:68
Definition: ProtocolMessage.h:25
std::shared_ptr< CProtocolMessage > protocolMessagePtr_t
Definition: ProtocolMessage.h:69