acl  3.5.3.0
mqtt_publish.hpp
浏览该文件的文档.
1 #pragma once
2 
3 #include "mqtt_message.hpp"
4 
5 namespace acl {
6 
7 /**
8  * mqtt message object for MQTT_PUBLISH type.
9  */
11 public:
12  /**
13  * constructor for creating MQTT_PUBLISH mqtt message object.
14  * @see mqtt_message
15  */
16  mqtt_publish(void);
17 
18  /**
19  * constructor for creating MQTT_PUBLISH mqtt message object.
20  * @see mqtt_message
21  */
22  mqtt_publish(const mqtt_header& header);
23 
24  ~mqtt_publish(void);
25 
26  /**
27  * set the message topic.
28  * @param topic {const char*}
29  * @return {mqtt_publish&}
30  */
31  mqtt_publish& set_topic(const char* topic);
32 
33  /**
34  * set the message id.
35  * @param id {unsigned short} the value must > 0 && <= 65535.
36  * @return {mqtt_publish&}
37  */
38  mqtt_publish& set_pkt_id(unsigned short id);
39 
40  /**
41  * set the message payload.
42  * @param len {unsigned} the length of the payload.
43  * @param data {const char*} the payload data.
44  * @return {mqtt_publish&}
45  */
46  mqtt_publish& set_payload(unsigned len, const char* data = NULL);
47 
48  /**
49  * get the message's topic.
50  * @return {const char*}
51  */
52  const char* get_topic(void) const {
53  return topic_.c_str();
54  }
55 
56  /**
57  * get the message's id.
58  * @return {unsigned short} the message will be invalid if return 0.
59  */
60  unsigned short get_pkt_id(void) const {
61  return pkt_id_;
62  }
63 
64  /**
65  * get the length of the payload.
66  * @return {unsigned}
67  */
68  unsigned get_payload_len(void) const {
69  return payload_len_;
70  }
71 
72  /**
73  * get the palyload.
74  * @return {const string&}
75  */
76  const string& get_payload(void) const {
77  return payload_;
78  }
79 
80 protected:
81  // @override
82  bool to_string(string& out);
83 
84  // @override
85  int update(const char* data, int dlen);
86 
87  // @override
88  bool finished(void) const {
89  return finished_;
90  }
91 
92 public:
93  // the below methods were used internal to parse mqtt message
94  // in streaming mode.
95 
96  int update_header_var(const char* data, int dlen);
97  int update_topic_len(const char* data, int dlen);
98  int update_topic_val(const char* data, int dlen);
99  int update_pktid(const char* data, int dlen);
100  int update_payload(const char* data, int dlen);
101 
102 private:
103  unsigned status_;
104  bool finished_;
105  char buff_[2];
106  int dlen_;
107  unsigned hlen_var_;
108 
109  string topic_;
110  unsigned short pkt_id_;
111  unsigned payload_len_;
112  string payload_;
113 };
114 
115 } // namespace acl
const char * get_topic(void) const
const string & get_payload(void) const
unsigned short get_pkt_id(void) const
bool finished(void) const
unsigned get_payload_len(void) const
#define ACL_CPP_API