03-publish-c2b-qos2-len.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <mosquitto.h>
  6. static int run = -1;
  7. void on_connect(struct mosquitto *mosq, void *obj, int rc)
  8. {
  9. if(rc){
  10. exit(1);
  11. }else{
  12. mosquitto_publish(mosq, NULL, "pub/qos2/test", strlen("message"), "message", 2, false);
  13. }
  14. }
  15. void on_publish(struct mosquitto *mosq, void *obj, int mid, int reason_code, const mosquitto_property *properties)
  16. {
  17. mosquitto_disconnect(mosq);
  18. }
  19. void on_disconnect(struct mosquitto *mosq, void *obj, int rc)
  20. {
  21. run = 0;
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. int rc;
  26. struct mosquitto *mosq;
  27. int port = atoi(argv[1]);
  28. mosquitto_lib_init();
  29. mosq = mosquitto_new("publish-qos2-test", true, NULL);
  30. if(mosq == NULL){
  31. return 1;
  32. }
  33. mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  34. mosquitto_connect_callback_set(mosq, on_connect);
  35. mosquitto_disconnect_callback_set(mosq, on_disconnect);
  36. mosquitto_publish_v5_callback_set(mosq, on_publish);
  37. mosquitto_message_retry_set(mosq, 3);
  38. rc = mosquitto_connect(mosq, "localhost", port, 60);
  39. while(run == -1){
  40. mosquitto_loop(mosq, 300, 1);
  41. }
  42. mosquitto_destroy(mosq);
  43. mosquitto_lib_cleanup();
  44. return run;
  45. }