#include <iostream>
enum {
MQTT_STAT_HDR_VAR,
MQTT_STAT_TOPIC_LEN,
MQTT_STAT_TOPIC_VAL,
MQTT_STAT_PKTID,
MQTT_STAT_PAYLOAD,
};
class mqtt_publish {
public:
/**
* constructor for creating MQTT_PUBLISH mqtt message object.
* @see mqtt_message
*/
mqtt_publish(void){
}
int update(const char* data, int dlen);
protected:
public:
// the below methods were used internal to parse mqtt message
// in streaming mode.
int update_header_var(const char* data, int state){
printf("-------状态机-----------\n");
return 0;
}
int update_topic_len(const char* data, int state){
return 0;
}
int update_topic_val(const char* data, int state){
return 0;
}
int update_pktid(const char* data, int state){
return 0;
}
int update_payload(const char* data, int state){
return 0;
}
private:
};
static struct {
int status;
int (mqtt_publish::*handler)(const char*, int);
} handlers[] = {
{ MQTT_STAT_HDR_VAR, &mqtt_publish::update_header_var },
{ MQTT_STAT_TOPIC_LEN, &mqtt_publish::update_topic_len },
{ MQTT_STAT_TOPIC_VAL, &mqtt_publish::update_topic_val },
{ MQTT_STAT_PKTID, &mqtt_publish::update_pktid },
{ MQTT_STAT_PAYLOAD, &mqtt_publish::update_payload },
};
int mqtt_publish::update(const char* data, int state) {
int status_ = state;
int ret = (this->*handlers[status_].handler)(data, state);
return ret;
}
int main() {
mqtt_publish publish;
publish.update(NULL, 0);
std::cout << "Hello, World!" << std::endl;
return 0;
}
C/C++编程:状态机的驱动表
于 2021-01-06 16:46:30 首次发布