03-publish-b2c-qos2-unexpected-pubrel.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. }
  12. }
  13. void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
  14. {
  15. if(!strcmp(msg->topic, "quit")){
  16. run = 0;
  17. return;
  18. }
  19. if(msg->mid != 13423){
  20. printf("Invalid mid (%d)\n", msg->mid);
  21. exit(1);
  22. }
  23. if(msg->qos != 2){
  24. printf("Invalid qos (%d)\n", msg->qos);
  25. exit(1);
  26. }
  27. if(strcmp(msg->topic, "pub/qos2/receive")){
  28. printf("Invalid topic (%s)\n", msg->topic);
  29. exit(1);
  30. }
  31. if(strcmp(msg->payload, "message")){
  32. printf("Invalid payload (%s)\n", (char *)msg->payload);
  33. exit(1);
  34. }
  35. if(msg->payloadlen != 7){
  36. printf("Invalid payloadlen (%d)\n", msg->payloadlen);
  37. exit(1);
  38. }
  39. if(msg->retain != false){
  40. printf("Invalid retain (%d)\n", msg->retain);
  41. exit(1);
  42. }
  43. }
  44. int main(int argc, char *argv[])
  45. {
  46. int rc;
  47. struct mosquitto *mosq;
  48. int port = atoi(argv[1]);
  49. mosquitto_lib_init();
  50. mosq = mosquitto_new("publish-qos2-test", true, &run);
  51. if(mosq == NULL){
  52. return 1;
  53. }
  54. mosquitto_connect_callback_set(mosq, on_connect);
  55. mosquitto_message_callback_set(mosq, on_message);
  56. mosquitto_message_retry_set(mosq, 5);
  57. rc = mosquitto_connect(mosq, "localhost", port, 60);
  58. while(run == -1){
  59. rc = mosquitto_loop(mosq, 300, 1);
  60. if(rc){
  61. printf("%d:%s\n", rc, mosquitto_strerror(rc));
  62. exit(1);
  63. }
  64. }
  65. mosquitto_destroy(mosq);
  66. mosquitto_lib_cleanup();
  67. return run;
  68. }