add mqtt_pubrel

This commit is contained in:
shuxin   zheng 2021-03-02 17:40:14 +08:00
parent 51ce9f18a3
commit 12fdda9da3
8 changed files with 86 additions and 45 deletions

View File

@ -63,14 +63,6 @@ public:
mqtt_message(mqtt_type_t type);
virtual ~mqtt_message(void);
/* for MQTT_PUBLISH */
void set_dup(bool yes);
void set_qos(mqtt_qos_t qos);
void set_retain(bool yes);
/* end */
int header_update(const char* data, unsigned dlen);
bool header_finish(void) const {
@ -81,18 +73,6 @@ public:
return type_;
}
bool is_dup(void) const {
return qos_ != MQTT_QOS0 && dup_;
}
mqtt_qos_t get_qos(void) const {
return qos_;
}
bool is_retain(void) const {
return retain_;
}
unsigned get_data_length(void) const {
return dlen_;
}
@ -100,10 +80,11 @@ public:
protected:
unsigned status_;
mqtt_type_t type_; // All
bool dup_; // MQTT_PUBLISH
mqtt_qos_t qos_; // MQTT_PUBLISH
bool retain_; // MQTT_PUBLISH
mqtt_type_t type_;
virtual unsigned char get_header_flags(void) const {
return 0x00;
}
void set_data_length(unsigned len);

View File

@ -10,6 +10,7 @@ public:
virtual ~mqtt_puback(void);
void set_pkt_id(unsigned short id);
unsigned short get_pkt_id(void) const {
return pkt_id_;
}

View File

@ -9,10 +9,26 @@ public:
mqtt_publish(bool parse_payload = true);
~mqtt_publish(void);
void set_dup(bool yes);
void set_qos(mqtt_qos_t qos);
void set_retain(bool yes);
void set_topic(const char* topic);
void set_pkt_id(unsigned short id);
void set_payload(unsigned len, const char* data = NULL);
bool is_dup(void) const {
return qos_ != MQTT_QOS0 && dup_;
}
mqtt_qos_t get_qos(void) const {
return qos_;
}
bool is_retain(void) const {
return retain_;
}
const char* get_topic(void) const {
return topic_.c_str();
}
@ -38,12 +54,19 @@ public:
int unpack_header_pktid(const char* data, unsigned dlen);
int unpack_done(const char* data, unsigned dlen);
protected:
// @override
unsigned char get_header_flags(void) const;
private:
bool finished_;
bool parse_payload_;
char hbuf_[2];
unsigned hlen_;
bool dup_;
mqtt_qos_t qos_;
bool retain_;
string topic_;
unsigned short pkt_id_;
unsigned payload_len_;

View File

@ -8,6 +8,12 @@ class mqtt_pubrec : public mqtt_puback {
public:
mqtt_pubrec(void);
~mqtt_pubrec(void);
protected:
// @override from mqtt_message
unsigned char get_header_flags(void) const {
return 0x02;
}
};
} // namespace acl

View File

@ -0,0 +1,13 @@
#pragma once
#include "mqtt_puback.hpp"
namespace acl {
class mqtt_pubrel : public mqtt_puback {
public:
mqtt_pubrel(void);
~mqtt_pubrel(void);
};
} // namespace acl

View File

@ -37,9 +37,6 @@ enum {
mqtt_message::mqtt_message(mqtt_type_t type)
: status_(MQTT_STAT_HDR_TYPE)
, type_(type)
, dup_(false)
, qos_(MQTT_QOS0)
, retain_(false)
, header_finished_(false)
, hflags_(0)
, dlen_(0)
@ -54,18 +51,6 @@ mqtt_message::mqtt_message(mqtt_type_t type)
mqtt_message::~mqtt_message(void) {}
void mqtt_message::set_dup(bool yes) {
dup_ = yes;
}
void mqtt_message::set_qos(mqtt_qos_t qos) {
qos_ = qos;
}
void mqtt_message::set_retain(bool yes) {
retain_ = yes;
}
void mqtt_message::set_data_length(unsigned len) {
if (len == 0) {
return;
@ -84,12 +69,8 @@ bool mqtt_message::pack_header(string& out) {
memset(header, 0, sizeof(header));
hflags_ |= qos_;
if (qos_ == MQTT_QOS0) {
dup_ = false;
} else if (dup_) {
hflags_ |= 0x8;
}
unsigned char flags = this->get_header_flags();
hflags_ |= (flags & 0x0f);
header[len] = ((((char) type_) << 4) & 0xff) | hflags_;
len++;

View File

@ -8,6 +8,9 @@ mqtt_publish::mqtt_publish(bool parse_payload /* true */)
, finished_(false)
, parse_payload_(parse_payload)
, hlen_(0)
, dup_(false)
, qos_(MQTT_QOS0)
, retain_(false)
, pkt_id_(0)
, payload_len_(0)
{
@ -16,6 +19,18 @@ mqtt_publish::mqtt_publish(bool parse_payload /* true */)
mqtt_publish::~mqtt_publish(void) {}
void mqtt_publish::set_dup(bool yes) {
dup_ = yes;
}
void mqtt_publish::set_qos(mqtt_qos_t qos) {
qos_ = qos;
}
void mqtt_publish::set_retain(bool yes) {
retain_ = yes;
}
void mqtt_publish::set_topic(const char* topic) {
topic_ = topic;
}
@ -31,6 +46,14 @@ void mqtt_publish::set_payload(unsigned len, const char* data /* NULL */) {
}
}
unsigned char mqtt_publish::get_header_flags(void) const {
unsigned char flags = qos_;
if (qos_ != MQTT_QOS0 && dup_) {
flags |= 0x8;
}
return flags;
}
bool mqtt_publish::to_string(string& out) {
bool old_mode = out.get_bin();
out.set_bin(true);

View File

@ -0,0 +1,13 @@
#include "acl_stdafx.hpp"
#include "acl_cpp/mqtt/mqtt_pubrel.hpp"
namespace acl {
mqtt_pubrel::mqtt_pubrel(void)
: mqtt_puback(MQTT_PUBREL)
{
}
mqtt_pubrel::~mqtt_pubrel(void) {}
} // namespace acl