paho_client.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // Created by zx on 23-2-22.
  3. //
  4. #ifndef PAHOC_SAMPLE_SAMPLES_PAHO_CLIENT_H_
  5. #define PAHOC_SAMPLE_SAMPLES_PAHO_CLIENT_H_
  6. #include "MQTTAsync.h"
  7. #include <string>
  8. #include "mqttmsg.h"
  9. typedef void (*ArrivedCallback)(std::string topic,int QOS,MqttMsg& msg,void* context);
  10. class Paho_client
  11. {
  12. public:
  13. Paho_client(std::string clientid);
  14. ~Paho_client();
  15. void set_maxbuf(int size);
  16. bool connect(std::string address,int port);
  17. bool disconnect();
  18. void subcribe(std::string topic,int QOS,ArrivedCallback callback,void* context);
  19. void publish(const std::string& topic,int QOS,const MqttMsg& msg);
  20. bool isconnected(){return connected_;}
  21. protected:
  22. static int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message);
  23. static void connlost(void *context, char *cause);
  24. static void onConnectFailure(void* context, MQTTAsync_failureData* response);
  25. static void onSubscribe(void* context, MQTTAsync_successData* response);
  26. static void onSubscribeFailure(void* context, MQTTAsync_failureData* response);
  27. static void onConnect(void* context, MQTTAsync_successData* response);
  28. static void onSendFailure(void* context, MQTTAsync_failureData* response);
  29. static void onSend(void* context, MQTTAsync_successData* response);
  30. static void onDisconnectFailure(void* context, MQTTAsync_failureData* response);
  31. static void onDisconnect(void* context, MQTTAsync_successData* response);
  32. protected:
  33. bool connected_;
  34. std::string clientid_;
  35. std::string address_;
  36. int port_;
  37. MQTTAsync client_;
  38. MQTTAsync_connectOptions conn_opts_ = MQTTAsync_connectOptions_initializer;
  39. MQTTAsync_message pubmsg_ = MQTTAsync_message_initializer;
  40. MQTTAsync_responseOptions pub_opts_ = MQTTAsync_responseOptions_initializer;
  41. MQTTAsync_createOptions create_opts_=MQTTAsync_createOptions_initializer;
  42. ArrivedCallback arrivedCallback_= nullptr;
  43. void* context_;
  44. };
  45. #endif //PAHOC_SAMPLE_SAMPLES_PAHO_CLIENT_H_