MQTTAsyncUtils.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2024 IBM Corp. and others
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial implementation and documentation
  15. *******************************************************************************/
  16. #if !defined(MQTTASYNCUTILS_H_)
  17. #define MQTTASYNCUTILS_H_
  18. #include "MQTTPacket.h"
  19. #include "Thread.h"
  20. #define URI_TCP "tcp://"
  21. #define URI_MQTT "mqtt://"
  22. #define URI_WS "ws://"
  23. #define URI_WSS "wss://"
  24. #define URI_UNIX "unix://"
  25. enum MQTTAsync_threadStates
  26. {
  27. STOPPED, STARTING, RUNNING, STOPPING
  28. };
  29. typedef struct
  30. {
  31. MQTTAsync_message* msg;
  32. char* topicName;
  33. int topicLen;
  34. unsigned int seqno; /* only used on restore */
  35. } qEntry;
  36. typedef struct
  37. {
  38. int type;
  39. MQTTAsync_onSuccess* onSuccess;
  40. MQTTAsync_onFailure* onFailure;
  41. MQTTAsync_onSuccess5* onSuccess5;
  42. MQTTAsync_onFailure5* onFailure5;
  43. MQTTAsync_token token;
  44. void* context;
  45. START_TIME_TYPE start_time;
  46. MQTTProperties properties;
  47. union
  48. {
  49. struct
  50. {
  51. int count;
  52. char** topics;
  53. int* qoss;
  54. MQTTSubscribe_options opts;
  55. MQTTSubscribe_options* optlist;
  56. } sub;
  57. struct
  58. {
  59. int count;
  60. char** topics;
  61. } unsub;
  62. struct
  63. {
  64. char* destinationName;
  65. int payloadlen;
  66. void* payload;
  67. int qos;
  68. int retained;
  69. } pub;
  70. struct
  71. {
  72. int internal;
  73. int timeout;
  74. enum MQTTReasonCodes reasonCode;
  75. } dis;
  76. struct
  77. {
  78. int currentURI;
  79. int MQTTVersion; /**< current MQTT version being used to connect */
  80. } conn;
  81. } details;
  82. } MQTTAsync_command;
  83. typedef struct MQTTAsync_struct
  84. {
  85. char* serverURI;
  86. int unixsock;
  87. int ssl;
  88. int websocket;
  89. Clients* c;
  90. /* "Global", to the client, callback definitions */
  91. MQTTAsync_connectionLost* cl;
  92. MQTTAsync_messageArrived* ma;
  93. MQTTAsync_deliveryComplete* dc;
  94. void* clContext; /* the context to be associated with the conn lost callback*/
  95. void* maContext; /* the context to be associated with the msg arrived callback*/
  96. void* dcContext; /* the context to be associated with the deliv complete callback*/
  97. MQTTAsync_connected* connected;
  98. void* connected_context; /* the context to be associated with the connected callback*/
  99. MQTTAsync_disconnected* disconnected;
  100. void* disconnected_context; /* the context to be associated with the disconnected callback*/
  101. MQTTAsync_updateConnectOptions* updateConnectOptions;
  102. void* updateConnectOptions_context;
  103. /* Each time connect is called, we store the options that were used. These are reused in
  104. any call to reconnect, or an automatic reconnect attempt */
  105. MQTTAsync_command connect; /* Connect operation properties */
  106. MQTTAsync_command disconnect; /* Disconnect operation properties */
  107. MQTTAsync_command* pending_write; /* Is there a socket write pending? */
  108. List* responses;
  109. unsigned int command_seqno;
  110. MQTTPacket* pack;
  111. /* added for offline buffering */
  112. MQTTAsync_createOptions* createOptions;
  113. int shouldBeConnected;
  114. int noBufferedMessages; /* the current number of buffered (publish) messages for this client */
  115. /* added for automatic reconnect */
  116. int automaticReconnect;
  117. int minRetryInterval;
  118. int maxRetryInterval;
  119. int serverURIcount;
  120. char** serverURIs;
  121. int connectTimeout;
  122. int currentInterval;
  123. int currentIntervalBase;
  124. START_TIME_TYPE lastConnectionFailedTime;
  125. int retrying;
  126. int reconnectNow;
  127. /* MQTT V5 properties */
  128. MQTTProperties* connectProps;
  129. MQTTProperties* willProps;
  130. } MQTTAsyncs;
  131. typedef struct
  132. {
  133. MQTTAsync_command command;
  134. MQTTAsyncs* client;
  135. unsigned int seqno; /* only used on restore */
  136. int not_restored;
  137. char* key; /* if not_restored, this holds the key */
  138. } MQTTAsync_queuedCommand;
  139. void MQTTAsync_lock_mutex(mutex_type amutex);
  140. void MQTTAsync_unlock_mutex(mutex_type amutex);
  141. void MQTTAsync_terminate(void);
  142. #if !defined(NO_PERSISTENCE)
  143. int MQTTAsync_restoreCommands(MQTTAsyncs* client);
  144. #endif
  145. int MQTTAsync_addCommand(MQTTAsync_queuedCommand* command, int command_size);
  146. void MQTTAsync_emptyMessageQueue(Clients* client);
  147. void MQTTAsync_freeResponses(MQTTAsyncs* m);
  148. void MQTTAsync_freeCommands(MQTTAsyncs* m);
  149. int MQTTAsync_unpersistCommandsAndMessages(Clients* c);
  150. void MQTTAsync_closeSession(Clients* client, enum MQTTReasonCodes reasonCode, MQTTProperties* props);
  151. int MQTTAsync_disconnect1(MQTTAsync handle, const MQTTAsync_disconnectOptions* options, int internal);
  152. int MQTTAsync_assignMsgId(MQTTAsyncs* m);
  153. int MQTTAsync_getNoBufferedMessages(MQTTAsyncs* m);
  154. void MQTTAsync_writeContinue(SOCKET socket);
  155. void MQTTAsync_writeComplete(SOCKET socket, int rc);
  156. void setRetryLoopInterval(int keepalive);
  157. void MQTTAsync_NULLPublishResponses(MQTTAsyncs* m);
  158. #if defined(_WIN32) || defined(_WIN64)
  159. #else
  160. #define WINAPI
  161. #endif
  162. thread_return_type WINAPI MQTTAsync_sendThread(void* n);
  163. thread_return_type WINAPI MQTTAsync_receiveThread(void* n);
  164. #endif /* MQTTASYNCUTILS_H_ */