acl/lib_acl_cpp/include/acl_cpp/mqtt/mqtt_publish.hpp

116 lines
2.2 KiB
C++
Raw Normal View History

2021-03-02 15:43:49 +08:00
#pragma once
#include "mqtt_message.hpp"
namespace acl {
/**
* mqtt message object for MQTT_PUBLISH type.
*/
2021-03-07 12:45:22 +08:00
class ACL_CPP_API mqtt_publish : public mqtt_message {
2021-03-02 15:43:49 +08:00
public:
/**
* constructor for creating MQTT_PUBLISH mqtt message object.
* @see mqtt_message
*/
2021-03-08 15:03:32 +08:00
mqtt_publish(void);
2021-03-02 15:43:49 +08:00
/**
* constructor for creating MQTT_PUBLISH mqtt message object.
* @see mqtt_message
*/
mqtt_publish(const mqtt_header& header);
2021-03-02 15:43:49 +08:00
~mqtt_publish(void);
2021-03-02 17:40:14 +08:00
/**
* set the message topic.
* @param topic {const char*}
2021-06-22 16:02:21 +08:00
* @return {mqtt_publish&}
*/
mqtt_publish& set_topic(const char* topic);
/**
* set the message id.
* @param id {unsigned short} the value must > 0 && <= 65535.
* @return {mqtt_publish&}
*/
mqtt_publish& set_pkt_id(unsigned short id);
/**
* set the message payload.
* @param len {unsigned} the length of the payload.
* @param data {const char*} the payload data.
* @return {mqtt_publish&}
*/
mqtt_publish& set_payload(unsigned len, const char* data = NULL);
/**
* get the message's topic.
* @return {const char*}
*/
2021-03-02 15:43:49 +08:00
const char* get_topic(void) const {
return topic_.c_str();
}
/**
* get the message's id.
* @return {unsigned short} the message will be invalid if return 0.
*/
2021-03-02 15:43:49 +08:00
unsigned short get_pkt_id(void) const {
return pkt_id_;
}
/**
* get the length of the payload.
* @return {unsigned}
*/
2021-03-02 15:43:49 +08:00
unsigned get_payload_len(void) const {
return payload_len_;
}
/**
* get the palyload.
* @return {const string&}
*/
const string& get_payload(void) const {
return payload_;
}
protected:
// @override
2021-03-02 15:43:49 +08:00
bool to_string(string& out);
// @override
int update(const char* data, int dlen);
2021-03-02 15:43:49 +08:00
// @override
bool finished(void) const {
2021-03-02 15:43:49 +08:00
return finished_;
}
public:
// the below methods were used internal to parse mqtt message
// in streaming mode.
int update_header_var(const char* data, int dlen);
int update_topic_len(const char* data, int dlen);
int update_topic_val(const char* data, int dlen);
int update_pktid(const char* data, int dlen);
int update_payload(const char* data, int dlen);
2021-03-02 15:43:49 +08:00
private:
2021-03-08 15:03:32 +08:00
unsigned status_;
2021-03-02 15:43:49 +08:00
bool finished_;
char buff_[2];
int dlen_;
unsigned hlen_var_;
2021-03-02 15:43:49 +08:00
string topic_;
unsigned short pkt_id_;
unsigned payload_len_;
string payload_;
};
} // namespace acl