Clients.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2017 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  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 API and implementation and/or initial documentation
  15. * Ian Craggs - add SSL support
  16. * Ian Craggs - fix for bug 413429 - connectionLost not called
  17. * Ian Craggs - change will payload to binary
  18. * Ian Craggs - password to binary
  19. *******************************************************************************/
  20. #if !defined(CLIENTS_H)
  21. #define CLIENTS_H
  22. #include <time.h>
  23. #if defined(OPENSSL)
  24. #if defined(WIN32) || defined(WIN64)
  25. #include <winsock2.h>
  26. #endif
  27. #include <openssl/ssl.h>
  28. #endif
  29. #include "MQTTClient.h"
  30. #include "LinkedList.h"
  31. #include "MQTTClientPersistence.h"
  32. /*BE
  33. include "LinkedList"
  34. BE*/
  35. /*BE
  36. def PUBLICATIONS
  37. {
  38. n32 ptr STRING open "topic"
  39. n32 ptr DATA "payload"
  40. n32 dec "payloadlen"
  41. n32 dec "refcount"
  42. }
  43. BE*/
  44. /**
  45. * Stored publication data to minimize copying
  46. */
  47. typedef struct
  48. {
  49. char *topic;
  50. int topiclen;
  51. char* payload;
  52. int payloadlen;
  53. int refcount;
  54. } Publications;
  55. /*BE
  56. // This should get moved to MQTTProtocol, but the includes don't quite work yet
  57. map MESSAGE_TYPES
  58. {
  59. "PUBREC" 5
  60. "PUBREL" .
  61. "PUBCOMP" .
  62. }
  63. def MESSAGES
  64. {
  65. n32 dec "qos"
  66. n32 map bool "retain"
  67. n32 dec "msgid"
  68. n32 ptr PUBLICATIONS "publish"
  69. n32 time "lastTouch"
  70. n8 map MESSAGE_TYPES "nextMessageType"
  71. n32 dec "len"
  72. }
  73. defList(MESSAGES)
  74. BE*/
  75. /**
  76. * Client publication message data
  77. */
  78. typedef struct
  79. {
  80. int qos;
  81. int retain;
  82. int msgid;
  83. Publications *publish;
  84. time_t lastTouch; /**> used for retry and expiry */
  85. char nextMessageType; /**> PUBREC, PUBREL, PUBCOMP */
  86. int len; /**> length of the whole structure+data */
  87. } Messages;
  88. /*BE
  89. def WILLMESSAGES
  90. {
  91. n32 ptr STRING open "topic"
  92. n32 ptr DATA open "msg"
  93. n32 dec "retained"
  94. n32 dec "qos"
  95. }
  96. BE*/
  97. /**
  98. * Client will message data
  99. */
  100. typedef struct
  101. {
  102. char *topic;
  103. int payloadlen;
  104. void *payload;
  105. int retained;
  106. int qos;
  107. } willMessages;
  108. /*BE
  109. map CLIENT_BITS
  110. {
  111. "cleansession" 1 : .
  112. "connected" 2 : .
  113. "good" 4 : .
  114. "ping_outstanding" 8 : .
  115. }
  116. def CLIENTS
  117. {
  118. n32 ptr STRING open "clientID"
  119. n32 ptr STRING open "username"
  120. n32 ptr STRING open "password"
  121. n32 map CLIENT_BITS "bits"
  122. at 4 n8 bits 7:6 dec "connect_state"
  123. at 8
  124. n32 dec "socket"
  125. n32 ptr "SSL"
  126. n32 dec "msgID"
  127. n32 dec "keepAliveInterval"
  128. n32 dec "maxInflightMessages"
  129. n32 ptr BRIDGECONNECTIONS "bridge_context"
  130. n32 time "lastContact"
  131. n32 ptr WILLMESSAGES "will"
  132. n32 ptr MESSAGESList open "inboundMsgs"
  133. n32 ptr MESSAGESList open "outboundMsgs"
  134. n32 ptr MESSAGESList open "messageQueue"
  135. n32 dec "discardedMsgs"
  136. }
  137. defList(CLIENTS)
  138. BE*/
  139. typedef struct
  140. {
  141. int socket;
  142. time_t lastSent;
  143. time_t lastReceived;
  144. #if defined(OPENSSL)
  145. SSL* ssl;
  146. SSL_CTX* ctx;
  147. #endif
  148. } networkHandles;
  149. /**
  150. * Data related to one client
  151. */
  152. typedef struct
  153. {
  154. char* clientID; /**< the string id of the client */
  155. const char* username; /**< MQTT v3.1 user name */
  156. int passwordlen; /**< MQTT password length */
  157. const void* password; /**< MQTT v3.1 binary password */
  158. unsigned int cleansession : 1; /**< MQTT clean session flag */
  159. unsigned int connected : 1; /**< whether it is currently connected */
  160. unsigned int good : 1; /**< if we have an error on the socket we turn this off */
  161. unsigned int ping_outstanding : 1;
  162. int connect_state : 4;
  163. networkHandles net;
  164. int msgID;
  165. int keepAliveInterval;
  166. int retryInterval;
  167. int maxInflightMessages;
  168. willMessages* will;
  169. List* inboundMsgs;
  170. List* outboundMsgs; /**< in flight */
  171. List* messageQueue;
  172. unsigned int qentry_seqno;
  173. void* phandle; /* the persistence handle */
  174. MQTTClient_persistence* persistence; /* a persistence implementation */
  175. void* context; /* calling context - used when calling disconnect_internal */
  176. int MQTTVersion;
  177. #if defined(OPENSSL)
  178. MQTTClient_SSLOptions *sslopts;
  179. SSL_SESSION* session; /***< SSL session pointer for fast handhake */
  180. #endif
  181. } Clients;
  182. int clientIDCompare(void* a, void* b);
  183. int clientSocketCompare(void* a, void* b);
  184. /**
  185. * Configuration data related to all clients
  186. */
  187. typedef struct
  188. {
  189. const char* version;
  190. List* clients;
  191. } ClientStates;
  192. #endif