03-publish-qos0-no-payload.c 976 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. static int sent_mid = -1;
  8. void on_connect(struct mosquitto *mosq, void *obj, int rc)
  9. {
  10. if(rc){
  11. exit(1);
  12. }else{
  13. mosquitto_publish(mosq, &sent_mid, "pub/qos0/no-payload/test", 0, NULL, 0, false);
  14. }
  15. }
  16. void on_publish(struct mosquitto *mosq, void *obj, int mid)
  17. {
  18. if(mid == sent_mid){
  19. mosquitto_disconnect(mosq);
  20. run = 0;
  21. }else{
  22. exit(1);
  23. }
  24. }
  25. int main(int argc, char *argv[])
  26. {
  27. int rc;
  28. struct mosquitto *mosq;
  29. int port = atoi(argv[1]);
  30. mosquitto_lib_init();
  31. mosq = mosquitto_new("publish-qos0-test-np", true, NULL);
  32. if(mosq == NULL){
  33. return 1;
  34. }
  35. mosquitto_connect_callback_set(mosq, on_connect);
  36. mosquitto_publish_callback_set(mosq, on_publish);
  37. rc = mosquitto_connect(mosq, "localhost", port, 60);
  38. while(run == -1){
  39. mosquitto_loop(mosq, -1, 1);
  40. }
  41. mosquitto_destroy(mosq);
  42. mosquitto_lib_cleanup();
  43. return run;
  44. }