MQTTAsyncUtils.h 5.2 KB

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