03-publish-c2b-qos2-receive-maximum-1.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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, int flags, const mosquitto_property *properties)
  8. {
  9. int i;
  10. if(rc){
  11. exit(1);
  12. }
  13. for(i=0; i<5; i++){
  14. mosquitto_publish_v5(mosq, NULL, "topic", 5, "12345", 2, false, NULL);
  15. }
  16. }
  17. void on_publish(struct mosquitto *mosq, void *obj, int mid, int reason_code, const mosquitto_property *properties)
  18. {
  19. if(mid == 5){
  20. mosquitto_disconnect(mosq);
  21. run = 0;
  22. }
  23. }
  24. int main(int argc, char *argv[])
  25. {
  26. int rc;
  27. struct mosquitto *mosq;
  28. mosquitto_property *props = NULL;
  29. int port = atoi(argv[1]);
  30. mosquitto_lib_init();
  31. mosq = mosquitto_new("publish-qos2-test", true, &run);
  32. if(mosq == NULL){
  33. return 1;
  34. }
  35. mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  36. mosquitto_connect_v5_callback_set(mosq, on_connect);
  37. mosquitto_publish_v5_callback_set(mosq, on_publish);
  38. rc = mosquitto_connect_bind_v5(mosq, "localhost", port, 60, NULL, NULL);
  39. while(run == -1){
  40. mosquitto_loop(mosq, 300, 1);
  41. }
  42. mosquitto_destroy(mosq);
  43. mosquitto_lib_cleanup();
  44. return run;
  45. }