Clients.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2018 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. * Ian Craggs - MQTT 5 support
  20. *******************************************************************************/
  21. #if !defined(CLIENTS_H)
  22. #define CLIENTS_H
  23. #include <time.h>
  24. #if defined(OPENSSL)
  25. #if defined(WIN32) || defined(WIN64)
  26. #include <winsock2.h>
  27. #endif
  28. #include <openssl/ssl.h>
  29. #endif
  30. #include "MQTTClient.h"
  31. #include "LinkedList.h"
  32. #include "MQTTClientPersistence.h"
  33. /**
  34. * Stored publication data to minimize copying
  35. */
  36. typedef struct
  37. {
  38. char *topic;
  39. int topiclen;
  40. char* payload;
  41. int payloadlen;
  42. int refcount;
  43. } Publications;
  44. /**
  45. * Client publication message data
  46. */
  47. typedef struct
  48. {
  49. int qos;
  50. int retain;
  51. int msgid;
  52. int MQTTVersion;
  53. MQTTProperties properties;
  54. Publications *publish;
  55. time_t lastTouch; /**> used for retry and expiry */
  56. char nextMessageType; /**> PUBREC, PUBREL, PUBCOMP */
  57. int len; /**> length of the whole structure+data */
  58. } Messages;
  59. /**
  60. * Client will message data
  61. */
  62. typedef struct
  63. {
  64. char *topic;
  65. int payloadlen;
  66. void *payload;
  67. int retained;
  68. int qos;
  69. } willMessages;
  70. typedef struct
  71. {
  72. int socket;
  73. time_t lastSent;
  74. time_t lastReceived;
  75. time_t lastPing;
  76. #if defined(OPENSSL)
  77. SSL* ssl;
  78. SSL_CTX* ctx;
  79. #endif
  80. int websocket; /**< socket has been upgraded to use web sockets */
  81. char *websocket_key;
  82. } networkHandles;
  83. /* connection states */
  84. /** no connection in progress, see connected value */
  85. #define NOT_IN_PROGRESS 0x0
  86. /** TCP connection in progress */
  87. #define TCP_IN_PROGRESS 0x1
  88. /** SSL connection in progress */
  89. #define SSL_IN_PROGRESS 0x2
  90. /** Websocket connection in progress */
  91. #define WEBSOCKET_IN_PROGRESS 0x3
  92. /** TCP completed, waiting for MQTT ACK */
  93. #define WAIT_FOR_CONNACK 0x4
  94. /** Disconnecting */
  95. #define DISCONNECTING -2
  96. /**
  97. * Data related to one client
  98. */
  99. typedef struct
  100. {
  101. char* clientID; /**< the string id of the client */
  102. const char* username; /**< MQTT v3.1 user name */
  103. int passwordlen; /**< MQTT password length */
  104. const void* password; /**< MQTT v3.1 binary password */
  105. unsigned int cleansession : 1; /**< MQTT V3 clean session flag */
  106. unsigned int cleanstart : 1; /**< MQTT V5 clean start flag */
  107. unsigned int connected : 1; /**< whether it is currently connected */
  108. unsigned int good : 1; /**< if we have an error on the socket we turn this off */
  109. unsigned int ping_outstanding : 1;
  110. signed int connect_state : 4;
  111. networkHandles net;
  112. int msgID;
  113. int keepAliveInterval;
  114. int retryInterval;
  115. int maxInflightMessages;
  116. willMessages* will;
  117. List* inboundMsgs;
  118. List* outboundMsgs; /**< in flight */
  119. List* messageQueue;
  120. unsigned int qentry_seqno;
  121. void* phandle; /* the persistence handle */
  122. MQTTClient_persistence* persistence; /* a persistence implementation */
  123. void* context; /* calling context - used when calling disconnect_internal */
  124. int MQTTVersion;
  125. int sessionExpiry; /**< MQTT 5 session expiry */
  126. #if defined(OPENSSL)
  127. MQTTClient_SSLOptions *sslopts;
  128. SSL_SESSION* session; /***< SSL session pointer for fast handhake */
  129. #endif
  130. } Clients;
  131. int clientIDCompare(void* a, void* b);
  132. int clientSocketCompare(void* a, void* b);
  133. /**
  134. * Configuration data related to all clients
  135. */
  136. typedef struct
  137. {
  138. const char* version;
  139. List* clients;
  140. } ClientStates;
  141. #endif