acl  3.5.3.0
mqtt_header.hpp
浏览该文件的文档.
1 #pragma once
2 
3 #include "../acl_cpp_define.hpp"
4 
5 namespace acl {
6 
7 /**
8  * all the mqtt types were defined below.
9  */
10 typedef enum {
27 } mqtt_type_t;
28 
29 typedef enum {
34 
35 /**
36  * all the qos type of mqtt
37  */
38 typedef enum {
39  MQTT_QOS0 = 0x0,
40  MQTT_QOS1 = 0x1,
41  MQTT_QOS2 = 0x2,
42 } mqtt_qos_t;
43 
46  unsigned char flags:4;
49  const char* desc;
50 };
51 
52 /**
53  * get description of the specified mqtt type.
54  * @param type {mqtt_type_t}
55  * @return {const char*}
56  */
57 const char* mqtt_type_desc(mqtt_type_t type);
58 
59 /**
60  * get description of the specified mqtt qos.
61  * @param qos {mqtt_qos_t}
62  * @return {const char*}
63  */
64 const char* mqtt_qos_desc(mqtt_qos_t qos);
65 
66 class string;
67 
68 /**
69  * mqtt message header class, used for building or parsing mqtt header data.
70  */
72 public:
73  /**
74  * mqtt header constructor, usually for building mqtt message.
75  * @param type {mqtt_type_t}
76  */
78 
79  /**
80  * mqtt header constructor, usually for parsing mqtt message.
81  * @param header {const mqtt_header&} will be copied internal.
82  */
83  mqtt_header(const mqtt_header& header);
84 
85  virtual ~mqtt_header(void);
86 
87 public:
88  /**
89  * build mqtt header data after initializing the header object
90  * by calling the setting methods below like set_xxx.
91  * @param out {string&} store mqtt header data.
92  * @return {bool} return true if build header successfully.
93  */
94  bool build_header(string& out);
95 
96  /**
97  * parsing mqtt header data in streaming mode.
98  * @param data {const char*} the mqtt header data, not NULL.
99  * @param dlen {int} the length of data, must > 0
100  * @return {int} return the length of the left data not consumed:
101  * > 0: the header has completed and the length of left data can
102  * be used as mqtt body or next mqtt message;
103  * 0: the data input has been consumed, you can call finished() to
104  * check if the mqtt header has completed.
105  * -1: some error happened when parsing the input data.
106  */
107  int update(const char* data, int dlen);
108 
109  /**
110  * check if the mqtt header has completed.
111  * @return {bool}
112  */
113  bool finished(void) const {
114  return finished_;
115  }
116 
117  /**
118  * reset the status of the mqtt header object for reusing the object.
119  */
120  void reset(void);
121 
122 public:
123  /**
124  * set the mqtt message type in mqtt header
125  * @param type {mqtt_type_t}
126  * @return {mqtt_header&}
127  */
128  mqtt_header& set_type(mqtt_type_t type);
129 
130  /**
131  * set the mqtt header flags.
132  * @param flags {char}
133  * @return {mqtt_header&}
134  */
135  mqtt_header& set_header_flags(char flags);
136 
137  /**
138  * set the length of the mqtt message body.
139  * @param len {unsigned}
140  * @return {mqtt_header&}
141  */
142  mqtt_header& set_remaing_length(unsigned len);
143 
144  /**
145  * set the qos of the mqtt message.
146  * @param qos {mqtt_qos_t}
147  * @return {mqtt_header&}
148  */
149  mqtt_header& set_qos(mqtt_qos_t qos);
150 
151  /**
152  * set if the mqtt message be sent duplicated.
153  * @param yes {bool}
154  * @return {mqtt_header&}
155  */
156  mqtt_header& set_dup(bool yes);
157 
158  /**
159  * set the remain flag in mqtt header.
160  * @param yes {bool}
161  * @return {mqtt_header&}
162  */
163  mqtt_header& set_remain(bool yes);
164 
165  /**
166  * get the mqtt message type.
167  * @return {mqtt_type_t}
168  */
169  mqtt_type_t get_type(void) const {
170  return type_;
171  }
172 
173  /**
174  * get the mqtt header flags.
175  * @return {unsigned char}
176  */
177  unsigned char get_header_flags(void) const {
178  return hflags_;
179  }
180 
181  /**
182  * get the length of the mqtt message body.
183  * @return {unsigned}
184  */
185  unsigned get_remaining_length(void) const {
186  return dlen_;
187  }
188 
189  /**
190  * get the mqtt message's qos.
191  * @return {mqtt_qos_t}
192  */
193  mqtt_qos_t get_qos(void) const;
194 
195  /**
196  * check if the duplicated flag has been set in header.
197  * @return {bool}
198  */
199  bool is_dup(void) const;
200 
201  /**
202  * check if the remain flag has been set in header.
203  * @return {bool}
204  */
205  bool is_remain(void) const;
206 
207 private:
208  unsigned status_;
209  bool finished_;
210 
211  mqtt_type_t type_;
212  unsigned char hflags_:4;
213  unsigned dlen_;
214 
215  char hbuf_[4];
216  unsigned hlen_;
217 
218 public:
219  // parsing mqtt header in streaming mode, return the length of left
220  // data that not consumed.
221 
222  int update_header_type(const char* data, int dlen);
223  int update_header_len(const char* data, int dlen);
224 };
225 
226 } // namespace acl
mqtt_type_t get_type(void) const
mqtt_option_t
Definition: mqtt_header.hpp:29
mqtt_option_t payload
Definition: mqtt_header.hpp:48
unsigned get_remaining_length(void) const
mqtt_qos_t
Definition: mqtt_header.hpp:38
const char * desc
Definition: mqtt_header.hpp:49
const char * mqtt_type_desc(mqtt_type_t type)
mqtt_type_t
Definition: mqtt_header.hpp:10
bool finished(void) const
unsigned char flags
Definition: mqtt_header.hpp:46
unsigned char get_header_flags(void) const
mqtt_option_t id
Definition: mqtt_header.hpp:47
#define ACL_CPP_API
const char * mqtt_qos_desc(mqtt_qos_t qos)