From 6c17451e0b5f84a230ce868fe95a9c571d79a65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?shuxin=20=E3=80=80=E3=80=80zheng?= Date: Tue, 2 Mar 2021 18:34:34 +0800 Subject: [PATCH] adding mqtt_subscribe --- .../include/acl_cpp/mqtt/mqtt_subscribe.hpp | 36 ++++++++++++ lib_acl_cpp/src/mqtt/mqtt_subscribe.cpp | 57 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 lib_acl_cpp/include/acl_cpp/mqtt/mqtt_subscribe.hpp create mode 100644 lib_acl_cpp/src/mqtt/mqtt_subscribe.cpp diff --git a/lib_acl_cpp/include/acl_cpp/mqtt/mqtt_subscribe.hpp b/lib_acl_cpp/include/acl_cpp/mqtt/mqtt_subscribe.hpp new file mode 100644 index 000000000..53596fbfe --- /dev/null +++ b/lib_acl_cpp/include/acl_cpp/mqtt/mqtt_subscribe.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "mqtt_message.hpp" + +namespace acl { + +class mqtt_subscribe : public mqtt_message { +public: + mqtt_subscribe(void); + ~mqtt_subscribe(void); + + void set_pkt_id(unsigned short id); + void add_topic(const char* topic, mqtt_qos_t qos); + + unsigned short get_pkt_id(void) const { + return pkt_id_; + } + + bool to_string(string& out); + + int update(const char* data, unsigned dlen); + +protected: + // @override + unsigned char get_header_flags(void) const { + return 0x02; + } + +private: + unsigned short pkt_id_; + std::vector topics_; + std::vector qoses_; + unsigned payload_len_; +}; + +} // namespace acl diff --git a/lib_acl_cpp/src/mqtt/mqtt_subscribe.cpp b/lib_acl_cpp/src/mqtt/mqtt_subscribe.cpp new file mode 100644 index 000000000..5fa774953 --- /dev/null +++ b/lib_acl_cpp/src/mqtt/mqtt_subscribe.cpp @@ -0,0 +1,57 @@ +#include "acl_stdafx.hpp" +#include "acl_cpp/mqtt/mqtt_subscribe.hpp" + +namespace acl { + +mqtt_subscribe::mqtt_subscribe(void) +: mqtt_message(MQTT_SUBSCRIBE) +, pkt_id_(0) +, payload_len_(0) +{ +} + +mqtt_subscribe::~mqtt_subscribe(void) {} + +void mqtt_subscribe::set_pkt_id(unsigned short id) { + pkt_id_ = id; +} + +void mqtt_subscribe::add_topic(const char* topic, mqtt_qos_t qos) { + topics_.push_back(topic); + qoses_.push_back(qos); + payload_len_ += strlen(topic) + 1; +} + +bool mqtt_subscribe::to_string(string& out) { + if (topics_.empty()) { + logger_error("no topic available!"); + return false; + } + + bool old_mode = out.get_bin(); + this->set_data_length(payload_len_); + + if (!this->pack_header(out)) { + out.set_bin(old_mode); + return false; + } + + size_t n = topics_.size(); + for (size_t i = 0; i < n; i++) { + this->pack_add(topics_[i], out); + this->pack_add((unsigned char) qoses_[i], out); + } + + out.set_bin(old_mode); + return true; +} + +int mqtt_subscribe::update(const char* data, unsigned dlen) { + if (data == NULL || dlen == 0) { + return 0; + } + + return dlen; +} + +} // namespace acl