acl  3.5.3.0
mqtt_message.hpp
浏览该文件的文档.
1 #pragma once
2 
3 #include "../stdlib/string.hpp"
4 #include "mqtt_header.hpp"
5 
6 namespace acl {
7 
8 /**
9  * the pure virtual class for creating one mqtt message object from or to mqtt
10  * message data, all subclass of it should implement the pure virtual method.
11  * Any subclass of it can be used to parse mqtt data to create mqtt message,
12  * or serialize to string data from mqtt message.
13  */
15 public:
16  /**
17  * create message object to be used for sending message to peer.
18  * @param type {mqtt_type_t}
19  */
21 
22  /**
23  * create message object after receiving message data from peer,
24  * because the mqtt message includes header and body, so we parse
25  * the protocol data in two steps: frist parsing the mqtt header,
26  * and then parsing the mqtt body.
27  * @param header {const mqtt_header&} created from the mqtt data and
28  * passed to the message object to be created.
29  */
30  mqtt_message(const mqtt_header& header);
31 
32  /**
33  * virtual destructor.
34  */
35  virtual ~mqtt_message(void);
36 
37 public:
38  /**
39  * the subclass should implement this method to build mqtt message data,
40  * this is used as the mqtt message serialize.
41  * @param out {string&} used to store the mqtt data.
42  * @return {bool} return true if serialize sucessfully.
43  */
44  virtual bool to_string(string& out) = 0;
45 
46  /**
47  * the subclass should implement this method to parse mqtt data,
48  * this is used as the mqtt message deserialize.
49  * @param data {char*} mqtt message data received from socket.
50  * @param dlen {int} the length of data.
51  * @return {int} return the length of left data not consumed:
52  * > 0: the current mqtt message object has been finished, the left
53  * data is for the next message object;
54  * -1: some error happened when parsing the mqtt message data;
55  * 0: all the data passed in has been consumed by the current mqtt
56  * message object, the object maybe complete or not, you should
57  * call finished() to check if the object has been completed.
58  */
59  virtual int update(const char* data, int dlen) = 0;
60 
61  /**
62  * check if the current mqtt object been parsing from mqtt data has
63  * been completed.
64  * @return {bool}
65  */
66  virtual bool finished(void) const {
67  return false;
68  }
69 
70  /**
71  * get the current mqtt message header in variable mode in order to
72  * change some information of the header.
73  * @return {mqtt_header&}
74  */
76  return header_;
77  }
78 
79  /**
80  * get the current mqtt message header in invariable mode, just for
81  * getting some information from it.
82  * @return {const mqtt_header&}
83  */
84  const mqtt_header& get_header(void) const {
85  return header_;
86  }
87 
88 public:
89  /**
90  * create mqtt message object with the specified mqtt header after
91  * parsing the mqtt data.
92  * @param header {const mqtt_header&}
93  * @return {mqtt_message*} return no-NULL if successful, or NULL if
94  * the mqtt object type of header is invalid.
95  */
96  static mqtt_message* create_message(const mqtt_header& header);
97 
98 protected:
100 
101  // add one byte to string buffer when building mqtt message.
102  void pack_add(unsigned char ch, string& out);
103 
104  // add two bytes to string buffer when building mqtt message.
105  void pack_add(unsigned short n, string& out);
106 
107  // add string data to string buffer when building mqtt message.
108  void pack_add(const string& s, string& out);
109 
110  // parse one short from mqtt data.
111  bool unpack_short(const char* data, size_t len, unsigned short& out);
112 };
113 
114 } // namespace acl
mqtt_header header_
virtual bool finished(void) const
mqtt_header & get_header(void)
const mqtt_header & get_header(void) const
mqtt_type_t
Definition: mqtt_header.hpp:10
#define ACL_CPP_API