-
Notifications
You must be signed in to change notification settings - Fork 939
/
Copy pathmqtt_ack.cpp
112 lines (92 loc) · 1.93 KB
/
mqtt_ack.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
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/mqtt/mqtt_ack.hpp"
#endif
namespace acl {
enum {
MQTT_STAT_HDR_VAR,
};
mqtt_ack::mqtt_ack(mqtt_type_t type)
: mqtt_message(type)
, pkt_id_(0)
, hlen_(0)
, finished_(false)
{
status_ = MQTT_STAT_HDR_VAR; // just for update()
}
mqtt_ack::mqtt_ack(const mqtt_header& header)
: mqtt_message(header)
, pkt_id_(0)
, hlen_(0)
, finished_(false)
{
status_ = MQTT_STAT_HDR_VAR; // just for update()
}
mqtt_ack::~mqtt_ack() {}
void mqtt_ack::set_pkt_id(unsigned short id) {
if (id > 0) {
pkt_id_ = id;
} else {
logger_error("invalid pkt_id=%u", id);
}
}
bool mqtt_ack::to_string(string& out) {
/*
if (pkt_id_ == 0) {
logger_error("pkt_id=0 is invalid");
return false;
}
*/
mqtt_header& header = this->get_header();
header.set_remaing_length(2);
if (!header.build_header(out)) {
return false;
}
this->pack_add((unsigned short) pkt_id_, out);
return true;
}
static struct {
int status;
int (mqtt_ack::*handler)(const char*, int);
} handlers[] = {
{ MQTT_STAT_HDR_VAR, &mqtt_ack::update_header_var },
};
int mqtt_ack::update(const char* data, int dlen) {
if (data == NULL || dlen <= 0) {
logger_error("invalid input");
return -1;
}
while (dlen > 0 && !finished_) {
int ret = (this->*handlers[status_].handler)(data, dlen);
if (ret < 0) {
return -1;
}
data += dlen - ret;
dlen = ret;
}
return dlen;
}
#define HDR_VAR_LEN 2
int mqtt_ack::update_header_var(const char* data, int dlen) {
assert(data && dlen > 0);
assert(sizeof(hbuf_) >= HDR_VAR_LEN);
if (hlen_ >= HDR_VAR_LEN) {
logger_error("invalid header var");
return -1;
}
for (; hlen_ < HDR_VAR_LEN && dlen > 0;) {
hbuf_[hlen_++] = *data++;
dlen --;
}
if (hlen_ < HDR_VAR_LEN) {
assert(dlen == 0);
return dlen;
}
if (!this->unpack_short(&hbuf_[0], 2, pkt_id_)) {
logger_error("unpack pkt id error");
return -1;
}
finished_ = true;
return dlen;
}
} // namespace acl