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