-
Notifications
You must be signed in to change notification settings - Fork 938
/
mqtt_client.cpp
172 lines (144 loc) · 3.55 KB
/
mqtt_client.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/mqtt/mqtt_message.hpp"
#include "acl_cpp/mqtt/mqtt_client.hpp"
#endif
namespace acl {
mqtt_client::mqtt_client(const char* addr, int conn_timeout, int rw_timeout)
: addr_(addr)
, conn_timeout_(conn_timeout)
, rw_timeout_(rw_timeout)
{
conn_ = conn_internal_ = NEW socket_stream;
}
mqtt_client::mqtt_client(socket_stream& conn, int conn_timeout)
: conn_timeout_(conn_timeout)
, rw_timeout_(conn.get_rw_timeout())
, conn_(&conn)
, conn_internal_(NULL)
{
}
mqtt_client::~mqtt_client() {
delete conn_internal_;
}
bool mqtt_client::open() {
if (conn_->opened()) {
return true;
}
if (!conn_->open(addr_.c_str(), conn_timeout_, rw_timeout_)) {
logger_error("connect redis %s error: %s",
addr_.c_str(), last_serror());
return false;
}
return true;
}
bool mqtt_client::send(mqtt_message& message) {
// If the addr_ isn't empty, we should try to connect the server,
// else the stream_ is set in the mqtt_client::mqtt_client().
if (!addr_.empty() && !open()) {
logger_error("connect server error: %s", last_serror());
return false;
}
string buff;
if (!message.to_string(buff)) {
logger_error("build mqtt message error");
return false;
}
if (buff.empty()) {
logger_error("mqtt message empty");
return false;
}
if (conn_->write(buff) == -1) {
if (!addr_.empty()) {
conn_->close();
}
//logger_error("send message error=%s", last_serror());
return false;
}
return true;
}
mqtt_message* mqtt_client::get_message() {
mqtt_header header(MQTT_RESERVED_MIN);
if (!read_header(header)) {
// If the addr_ isn't empty, the conn_internal_ is in
// the client mode, so we can close it and try again.
if (!addr_.empty()) {
conn_->close();
}
//logger_error("get header error");
return NULL;
}
mqtt_message* message = mqtt_message::create_message(header);
if (message == NULL) {
logger_error("create_message error");
return NULL;
}
if (!read_message(header, *message)) {
delete message;
return NULL;
}
return message;
}
bool mqtt_client::read_header(mqtt_header& header) {
char ch;
if (!conn_->read(ch)) {
//logger_error("read header type error: %s", last_serror());
return false;
}
// update the first char for mqtt_type_t
if (header.update(&ch, 1) != 0) {
logger_debug(DEBUG_MQTT, 1, "invalid header type=%d", (int) ch);
return false;
}
for (int i = 0; i < 4; i++) {
if (!conn_->read(ch)) {
logger_debug(DEBUG_MQTT, 1, "read char error: %s, i=%d",
last_serror(), i);
return false;
}
if (header.update(&ch, 1) != 0) {
logger_debug(DEBUG_MQTT, 1, "header_update error, ch=%d",
(int) ch);
return false;
}
if (header.finished()) {
break;
}
}
if (!header.finished()) {
logger_debug(DEBUG_MQTT, 1, "get mqtt header error");
return false;
}
return true;
}
bool mqtt_client::read_message(const mqtt_header& header, mqtt_message& body) {
unsigned len = header.get_remaining_length();
if (len == 0) {
return true;
}
char buf[8192];
while (len > 0) {
size_t size = sizeof(buf) > len ? len : sizeof(buf);
int n = conn_->read(buf, size);
if (n == -1) {
logger_debug(DEBUG_MQTT, 1, "read body error: %s",
last_serror());
return false;
}
len -= n;
n = body.update(buf, (int) size);
if (n == -1) {
logger_debug(DEBUG_MQTT, 1, "update body error");
return false;
} else if (n != 0) {
logger_debug(DEBUG_MQTT, 1, "invalid body data");
return false;
}
}
if (!body.finished()) {
logger_debug(DEBUG_MQTT, 1, "body not finished!");
return false;
}
return true;
}
} // namespace acl