-
Notifications
You must be signed in to change notification settings - Fork 938
/
mqtt_aclient.cpp
346 lines (293 loc) · 7.16 KB
/
mqtt_aclient.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/stdlib/log.hpp"
#include "acl_cpp/stream/sslbase_conf.hpp"
#include "acl_cpp/stream/sslbase_io.hpp"
#include "acl_cpp/mqtt/mqtt_message.hpp"
#include "acl_cpp/mqtt/mqtt_aclient.hpp"
#endif
namespace acl {
mqtt_aclient::mqtt_aclient(aio_handle& handle, sslbase_conf* ssl_conf)
: handle_(handle)
, ssl_conf_(ssl_conf)
, conn_(NULL)
, body_(NULL)
{
header_ = NEW mqtt_header(MQTT_RESERVED_MIN);
}
mqtt_aclient::~mqtt_aclient() {
delete header_;
delete body_;
}
mqtt_aclient& mqtt_aclient::set_host(const char* host) {
if (host && *host) {
host_ = host;
} else {
host_.clear();
}
return *this;
}
mqtt_aclient& mqtt_aclient::set_sni_prefix(const char *prefix) {
if (prefix && *prefix) {
sni_prefix_ = prefix;
} else {
sni_prefix_.clear();
}
return *this;
}
mqtt_aclient& mqtt_aclient::set_sni_suffix(const char *suffix) {
if (suffix && *suffix) {
sni_suffix_ = suffix;
} else {
sni_suffix_.clear();
}
return *this;
}
bool mqtt_aclient::open(const char* addr, int conn_timeout, int rw_timeout) {
ACL_AIO* aio = handle_.get_handle();
if (acl_aio_connect_addr(aio, addr, conn_timeout,
connect_callback, this) == -1) {
logger_error("connect %s error %s", addr, last_serror());
return false;
}
rw_timeout_ = rw_timeout;
return true;
}
bool mqtt_aclient::open(aio_socket_stream* conn) {
conn_ = conn;
conn_->add_close_callback(this);
conn_->add_timeout_callback(this);
if (!ssl_conf_) {
return open_done();
}
sslbase_io* ssl_io = ssl_conf_->create(true);
if (conn_->setup_hook(ssl_io) == ssl_io || !ssl_io->handshake()) {
logger_error("open ssl failed");
conn_->remove_hook();
ssl_io->destroy();
return false;
}
// begin SSL handshake, read_wakeup will be called if some data arrived.
conn_->add_read_callback(this);
conn_->readable_await(rw_timeout_);
return true;
}
void mqtt_aclient::close() {
if (conn_) {
conn_->close();
}
}
bool mqtt_aclient::get_ns_addr(string& out) const {
char buf[256];
const struct sockaddr* sa = (const struct sockaddr*) &ns_addr_;
size_t ret = acl_inet_ntop(sa, buf, sizeof(buf));
if (ret == 0) {
return false;
}
out = buf;
return true;
}
bool mqtt_aclient::get_server_addr(string& out) const {
char buf[256];
const struct sockaddr* sa = (const struct sockaddr*) &serv_addr_;
size_t ret = acl_inet_ntop(sa, buf, sizeof(buf));
if (ret == 0) {
return false;
}
out = buf;
return true;
}
bool mqtt_aclient::handle_connect(const ACL_ASTREAM_CTX *ctx)
{
const ACL_SOCKADDR *ns_addr = acl_astream_get_ns_addr(ctx);
if (ns_addr) {
memcpy(&ns_addr_, &ns_addr->ss, sizeof(ns_addr_));
}
const ACL_SOCKADDR* serv_addr = acl_astream_get_serv_addr(ctx);
if (serv_addr) {
memcpy(&serv_addr_, &serv_addr->ss, sizeof(serv_addr_));
}
ACL_ASTREAM* astream = acl_astream_get_conn(ctx);
if (astream == NULL) {
int status = acl_astream_get_status(ctx);
switch (status) {
case ACL_ASTREAM_STATUS_NS_ERROR:
this->on_ns_failed();
this->destroy();
break;
case ACL_ASTREAM_STATUS_CONNECT_TIMEOUT:
this->on_connect_timeout();
this->destroy();
break;
default:
this->on_connect_failed();
this->destroy();
break;
}
return false;
}
// create one c++ aio with the connection
conn_ = NEW aio_socket_stream(&handle_, astream, true);
// add closing callback when the connection disconnected
conn_->add_close_callback(this);
// add reading timeout callback when reading timeout
conn_->add_timeout_callback(this);
if (!ssl_conf_) {
return open_done();
}
// create one SSL IO for SSL communication mode, and begin to SSL
// handshake async.
sslbase_io* ssl_io = ssl_conf_->create(true);
if (!host_.empty()) {
ssl_io->set_sni_host(host_.c_str(),
sni_prefix_.empty() ? NULL : sni_prefix_.c_str(),
sni_suffix_.empty() ? NULL : sni_suffix_.c_str());
}
if (conn_->setup_hook(ssl_io) == ssl_io || !ssl_io->handshake()) {
logger_error("open ssl failed");
conn_->remove_hook();
ssl_io->destroy();
this->on_connect_failed();
return false;
}
// begin SSL handshake, read_wakeup will be called if some data arrived.
conn_->add_read_callback(this);
conn_->readable_await(rw_timeout_);
return true;
}
int mqtt_aclient::connect_callback(const ACL_ASTREAM_CTX* ctx) {
assert(ctx);
mqtt_aclient* me = (mqtt_aclient*) acl_astream_get_ctx(ctx);
assert(me);
return me->handle_connect(ctx) ? 0 : -1;
}
bool mqtt_aclient::open_done() {
if (!this->on_open()) {
return false;
}
// begin to wait for mqtt message from peer
return message_await();
}
bool mqtt_aclient::timeout_callback() {
return this->on_read_timeout();
}
void mqtt_aclient::close_callback() {
this->on_disconnect();
this->destroy();
}
bool mqtt_aclient::read_wakeup() {
return handle_ssl_handshake();
}
bool mqtt_aclient::handle_ssl_handshake() {
sslbase_io* ssl_io = (sslbase_io*) conn_->get_hook();
if (ssl_io == NULL) {
logger_error("no ssl_io hooked!");
return false;
}
if (!ssl_io->handshake()) {
logger_error("ssl handshake error!");
return false;
}
// if ssl handshake successful, notify the subclass object
if (ssl_io->handshake_ok()) {
conn_->del_read_callback(this);
conn_->disable_read();
return open_done();
}
// else continue to wait for the completion of ssl handshake
return true;
}
bool mqtt_aclient::send(mqtt_message& message) {
mqtt_type_t type = message.get_header().get_type();
if (conn_ == NULL) {
logger_error("connection not opened yet, type=%s",
mqtt_type_desc(type));
return false;
}
string buff;
if (!message.to_string(buff)) {
logger_error("build mqtt message error, type=%s",
mqtt_type_desc(type));
return false;
}
if (buff.empty()) {
logger_error("message empty, type=%s",
mqtt_type_desc(type));
return false;
}
conn_->write(buff.c_str(), (int) buff.size());
return true;
}
bool mqtt_aclient::message_await() {
if (conn_) {
conn_->keep_read(true);
conn_->add_read_callback(this);
conn_->read();
return true;
} else {
logger_error("not connected yet");
return false;
}
}
bool mqtt_aclient::read_callback(char* data, int len) {
if (data == NULL || len <= 0) {
logger_error("invalid data=%p, len=%d", data, len);
return false;
}
while (true) {
int left = handle_data(data, len);
if (left < 0) {
header_->reset();
return false;
} else if (left > 0) {
data += len - left;
len = left;
} else {
return true;
}
}
}
int mqtt_aclient::handle_data(char* data, int len) {
int left;
if (!header_->finished()) {
left = header_->update(data, len);
} else {
left = len;
}
if (left < 0) {
logger_error("header update failed");
return -1;
}
if (!header_->finished()) {
assert(left == 0);
return 0;
}
data += len - left;
len = left;
if (body_ == NULL) {
body_ = mqtt_message::create_message(*header_);
if (body_ == NULL) {
logger_error("create mqtt_message failed");
return -1;
}
}
if (len > 0) {
left = body_->update(data, len);
if (left < 0) {
logger_error("message update failed");
return -1;
}
}
if (body_->finished()) {
bool ret = this->on_body(*body_);
header_->reset();
delete body_;
body_ = NULL;
if (!ret) {
logger_error("subclass return false");
return -1;
}
}
return left;
}
} // namespace acl