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

120 lines
2.1 KiB
C++
Raw Normal View History

2021-03-02 11:31:24 +08:00
#pragma once
#include "../stdlib/string.hpp"
namespace acl {
typedef enum {
MQTT_RESERVED_MIN = 0,
MQTT_CONNECT = 1,
MQTT_CONNACK = 2,
MQTT_PUBLISH = 3,
MQTT_PUBACK = 4,
MQTT_PUBREC = 5,
MQTT_PUBREL = 6,
MQTT_PUBCOMP = 7,
MQTT_SUBSCRIBE = 8,
MQTT_SUBACK = 9,
MQTT_UNSUBSCRIBE = 10,
MQTT_UNSUBACK = 11,
MQTT_PINGREQ = 12,
MQTT_PINGRESP = 13,
MQTT_DISCONNECT = 14,
MQTT_RESERVED_MAX = 15,
} mqtt_type_t;
enum {
MQTT_STAT_STR_LEN = 0,
MQTT_STAT_STR_VAL = 1,
MQTT_STAT_HDR_VAR = 2,
MQTT_STAT_HDR_END = 3,
};
typedef enum {
MQTT_NONE,
MQTT_NEED,
MQTT_MAYBE,
} mqtt_option_t;
2021-03-02 15:43:49 +08:00
typedef enum {
MQTT_QOS0 = 0x0,
MQTT_QOS1 = 0x1,
MQTT_QOS2 = 0x2,
} mqtt_qos_t;
2021-03-02 11:31:24 +08:00
struct mqtt_constrain {
mqtt_type_t type;
unsigned char flags:4;
mqtt_option_t id;
mqtt_option_t payload;
const char* desc;
};
struct mqtt_header {
mqtt_type_t type;
unsigned char flags:4;
unsigned int length;
};
const char* mqtt_type_desc(mqtt_type_t type);
class mqtt_message {
public:
mqtt_message(mqtt_type_t type);
virtual ~mqtt_message(void);
int header_update(const char* data, unsigned dlen);
bool header_finish(void) const {
return header_finished_;
}
mqtt_type_t get_type(void) const {
return type_;
}
unsigned get_data_length(void) const {
return dlen_;
}
protected:
unsigned status_;
2021-03-02 15:43:49 +08:00
2021-03-02 17:40:14 +08:00
mqtt_type_t type_;
virtual unsigned char get_header_flags(void) const {
return 0x00;
}
2021-03-02 11:31:24 +08:00
void set_data_length(unsigned len);
bool pack_header(string& out);
void pack_add(unsigned char ch, string& out);
void pack_add(unsigned short n, string& out);
void pack_add(const string& s, string& out);
bool unpack_short(const char* data, size_t len, unsigned short& out);
void unpack_string_await(string& buf, int next);
private:
bool header_finished_;
unsigned char hflags_:4;
char hbuf_[4];
unsigned hlen_;
unsigned dlen_;
int status_next_;
string* buff_;
unsigned short vlen_;
public:
int unpack_string_len(const char* data, unsigned dlen);
int unpack_string_val(const char* data, unsigned dlen);
int unpack_header_type(const char* data, unsigned dlen);
int unpack_header_len(const char* data, unsigned dlen);
};
} // namespace acl