connect_options.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file connect_options.h
  3. /// Declaration of MQTT connect_options class
  4. /// @date May 1, 2013
  5. /// @author Frank Pagliughi
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2013-2024 Frank Pagliughi <fpagliughi@mindspring.com>
  9. *
  10. * All rights reserved. This program and the accompanying materials
  11. * are made available under the terms of the Eclipse Public License v2.0
  12. * and Eclipse Distribution License v1.0 which accompany this distribution.
  13. *
  14. * The Eclipse Public License is available at
  15. * http://www.eclipse.org/legal/epl-v20.html
  16. * and the Eclipse Distribution License is available at
  17. * http://www.eclipse.org/org/documents/edl-v10.php.
  18. *
  19. * Contributors:
  20. * Frank Pagliughi - initial implementation and documentation
  21. *******************************************************************************/
  22. #ifndef __mqtt_connect_options_h
  23. #define __mqtt_connect_options_h
  24. #include <chrono>
  25. #include <map>
  26. #include <vector>
  27. #include "MQTTAsync.h"
  28. #include "mqtt/message.h"
  29. #include "mqtt/platform.h"
  30. #include "mqtt/ssl_options.h"
  31. #include "mqtt/string_collection.h"
  32. #include "mqtt/token.h"
  33. #include "mqtt/topic.h"
  34. #include "mqtt/types.h"
  35. #include "mqtt/will_options.h"
  36. namespace mqtt {
  37. /////////////////////////////////////////////////////////////////////////////
  38. /**
  39. * Holds the set of options that control how the client connects to a
  40. * server.
  41. */
  42. class connect_options
  43. {
  44. /** The default C struct for non-WebSocket connections */
  45. static constexpr MQTTAsync_connectOptions DFLT_C_STRUCT
  46. MQTTAsync_connectOptions_initializer;
  47. /** The default C struct for non-Websocket MQTT v5 connections */
  48. static constexpr MQTTAsync_connectOptions DFLT_C_STRUCT5
  49. MQTTAsync_connectOptions_initializer5;
  50. /** The default C struct for WebSocket connections */
  51. static constexpr MQTTAsync_connectOptions DFLT_C_STRUCT_WS
  52. MQTTAsync_connectOptions_initializer_ws;
  53. /** The default C struct for Websocket MQTT v5 connections */
  54. static constexpr MQTTAsync_connectOptions DFLT_C_STRUCT5_WS
  55. MQTTAsync_connectOptions_initializer5_ws;
  56. /** The underlying C connection options */
  57. MQTTAsync_connectOptions opts_;
  58. /** The LWT options */
  59. will_options will_;
  60. /** The SSL options */
  61. ssl_options ssl_;
  62. /** The user name to use for the connection. */
  63. string_ref userName_;
  64. /** The password to use for the connection. */
  65. binary_ref password_;
  66. /** Shared token pointer for context, if any */
  67. token_ptr tok_;
  68. /** Collection of server URIs, if any */
  69. const_string_collection_ptr serverURIs_;
  70. /** The connect properties */
  71. properties props_;
  72. /** HTTP Headers */
  73. name_value_collection httpHeaders_;
  74. /** HTTP proxy for websockets */
  75. string httpProxy_;
  76. /** Secure HTTPS proxy for websockets */
  77. string httpsProxy_;
  78. /** The client has special access */
  79. friend class async_client;
  80. /**
  81. * Gets a pointer to the C-language NUL-terminated strings for the
  82. * struct.
  83. * @note In the connect options, by default, the Paho C treats
  84. * nullptr char arrays as unset values, so we keep that semantic and
  85. * only set those char arrays if the string is non-empty.
  86. * @param sr The C++ string object.
  87. * @return Pointer to a NUL terminated string. This is only valid until
  88. * the next time the string is updated.
  89. */
  90. const char* c_str(const string_ref& sr) { return sr.empty() ? nullptr : sr.c_str(); }
  91. const char* c_str(const string& s) { return s.empty() ? nullptr : s.c_str(); }
  92. /**
  93. * Updates the underlying C structure to match our strings.
  94. */
  95. void update_c_struct();
  96. /**
  97. * Creates the options from a C option struct.
  98. * @param copts The C options struct.
  99. */
  100. connect_options(const MQTTAsync_connectOptions& copts) : opts_(copts) {}
  101. public:
  102. /** Smart/shared pointer to an object of this class. */
  103. using ptr_t = std::shared_ptr<connect_options>;
  104. /** Smart/shared pointer to a const object of this class. */
  105. using const_ptr_t = std::shared_ptr<const connect_options>;
  106. /**
  107. * Constructs a new object using the default values.
  108. *
  109. * @param ver The MQTT protocol version.
  110. */
  111. explicit connect_options(int ver = MQTTVERSION_DEFAULT);
  112. /**
  113. * Constructs a new object using the specified user name and password.
  114. * @param userName The name of the user for connecting to the server
  115. * @param password The password for connecting to the server
  116. * @param ver The MQTT protocol version.
  117. */
  118. connect_options(string_ref userName, binary_ref password, int ver = MQTTVERSION_DEFAULT);
  119. /**
  120. * Copy constructor.
  121. * @param opt Another object to copy.
  122. */
  123. connect_options(const connect_options& opt);
  124. /**
  125. * Move constructor.
  126. * @param opt Another object to move into this new one.
  127. */
  128. connect_options(connect_options&& opt);
  129. /**
  130. * Creates default options for an MQTT v3.x connection.
  131. * @return Default options for an MQTT v3.x connection.
  132. */
  133. static connect_options v3() { return connect_options(DFLT_C_STRUCT); }
  134. /**
  135. * Creates default options for an MQTT v5 connection.
  136. * @return Default options for an MQTT v5 connection.
  137. */
  138. static connect_options v5() { return connect_options(DFLT_C_STRUCT5); }
  139. /**
  140. * Creates default options for an MQTT v3.x connection using WebSockets.
  141. *
  142. * The keepalive interval is set to 45 seconds to avoid webserver 60
  143. * second inactivity timeouts.
  144. *
  145. * @return Default options for an MQTT v3.x connection using websockets.
  146. */
  147. static connect_options ws() { return connect_options(DFLT_C_STRUCT_WS); }
  148. /**
  149. * Creates default options for an MQTT v5 connection using WebSockets.
  150. *
  151. * The keepalive interval is set to 45 seconds to avoid webserver 60
  152. * second inactivity timeouts.
  153. *
  154. * @return Default options for an MQTT v5 connection using websockets.
  155. */
  156. static connect_options v5_ws() { return connect_options(DFLT_C_STRUCT5_WS); }
  157. /**
  158. * Copy assignment.
  159. * @param opt Another object to copy.
  160. */
  161. connect_options& operator=(const connect_options& opt);
  162. /**
  163. * Move assignment.
  164. * @param opt Another object to move into this new one.
  165. */
  166. connect_options& operator=(connect_options&& opt);
  167. /**
  168. * Expose the underlying C struct for the unit tests.
  169. */
  170. #if defined(UNIT_TESTS)
  171. const MQTTAsync_connectOptions& c_struct() const { return opts_; }
  172. #endif
  173. /**
  174. * Gets the "keep alive" interval.
  175. * @return The keep alive interval in seconds.
  176. */
  177. std::chrono::seconds get_keep_alive_interval() const {
  178. return std::chrono::seconds(opts_.keepAliveInterval);
  179. }
  180. /**
  181. * Gets the connection timeout.
  182. * This is the amount of time the underlying library will wait for a
  183. * timeout before failing.
  184. * @return The connect timeout in seconds.
  185. */
  186. std::chrono::seconds get_connect_timeout() const {
  187. return std::chrono::seconds(opts_.connectTimeout);
  188. }
  189. /**
  190. * Gets the user name to use for the connection.
  191. * @return The user name to use for the connection.
  192. */
  193. string get_user_name() const { return userName_ ? userName_.to_string() : string(); }
  194. /**
  195. * Gets the password to use for the connection.
  196. * @return The password to use for the connection.
  197. */
  198. binary_ref get_password() const { return password_; }
  199. /**
  200. * Gets the password to use for the connection.
  201. * @return The password to use for the connection.
  202. */
  203. string get_password_str() const { return password_ ? password_.to_string() : string(); }
  204. /**
  205. * Gets the maximum number of messages that can be in-flight
  206. * simultaneously.
  207. * @return The maximum number of inflight messages.
  208. */
  209. int get_max_inflight() const { return opts_.maxInflight; }
  210. /**
  211. * Gets the topic to be used for last will and testament (LWT).
  212. * @return The topic to be used for last will and testament (LWT).
  213. */
  214. string get_will_topic() const { return will_.get_topic(); }
  215. /**
  216. * Gets the message to be sent as last will and testament (LWT).
  217. * @return The message to be sent as last will and testament (LWT).
  218. */
  219. const_message_ptr get_will_message() const { return will_.get_message(); }
  220. /**
  221. * Get the LWT options to use for the connection.
  222. * @return The LWT options to use for the connection.
  223. */
  224. const will_options& get_will_options() const { return will_; }
  225. /**
  226. * Get the SSL options to use for the connection.
  227. * @return The SSL options to use for the connection.
  228. */
  229. const ssl_options& get_ssl_options() const { return ssl_; }
  230. /**
  231. * Sets the SSL for the connection.
  232. * These will only have an effect if compiled against the SSL version of
  233. * the Paho C library, and using a secure connection, "ssl://" or
  234. * "wss://".
  235. * @param ssl The SSL options.
  236. */
  237. void set_ssl(const ssl_options& ssl);
  238. /**
  239. * Sets the SSL for the connection.
  240. * These will only have an effect if compiled against the SSL version of
  241. * the Paho C library, and using a secure connection, "ssl://" or
  242. * "wss://".
  243. * @param ssl The SSL options.
  244. */
  245. void set_ssl(ssl_options&& ssl);
  246. /**
  247. * Returns whether the server should remember state for the client
  248. * across reconnects. This only applies to MQTT v3.x connections.
  249. * @return @em true if requesting a clean session, @em false if not.
  250. */
  251. bool is_clean_session() const { return to_bool(opts_.cleansession); }
  252. /**
  253. * Returns whether the server should remember state for the client
  254. * across reconnects. This only applies to MQTT v5 connections.
  255. * @return @em true if requesting a clean start, @em false if not.
  256. */
  257. bool is_clean_start() const { return to_bool(opts_.cleanstart); }
  258. /**
  259. * Gets the token used as the callback context.
  260. * @return The delivery token used as the callback context.
  261. */
  262. token_ptr get_token() const { return tok_; }
  263. /**
  264. * Gets the list of servers to which the client will connect.
  265. * @return A collection of server URI's. Each entry should be of the
  266. * form @em protocol://host:port where @em protocol must be tcp
  267. * or @em ssl. For @em host, you can specify either an IP
  268. * address or a domain name.
  269. */
  270. const_string_collection_ptr get_servers() const { return serverURIs_; }
  271. /**
  272. * Gets the version of MQTT to be used for the connection.
  273. * @return The version of MQTT to be used for the connection:
  274. * @li MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if that
  275. * fails, fall back to 3.1
  276. * @li MQTTVERSION_3_1 (3) = only try version 3.1
  277. * @li MQTTVERSION_3_1_1 (4) = only try version 3.1.1
  278. */
  279. int get_mqtt_version() const { return opts_.MQTTVersion; }
  280. /**
  281. * Determines if the options have been configured for automatic
  282. * reconnect.
  283. * @return @em true if configured for automatic reconnect, @em false if
  284. * not.
  285. */
  286. bool get_automatic_reconnect() const { return to_bool(opts_.automaticReconnect); }
  287. /**
  288. * Gets the minimum retry interval for automatic reconnect.
  289. * @return The minimum retry interval for automatic reconnect, in
  290. * seconds.
  291. */
  292. std::chrono::seconds get_min_retry_interval() const {
  293. return std::chrono::seconds(opts_.minRetryInterval);
  294. }
  295. /**
  296. * Gets the maximum retry interval for automatic reconnect.
  297. * @return The maximum retry interval for automatic reconnect, in
  298. * seconds.
  299. */
  300. std::chrono::seconds get_max_retry_interval() const {
  301. return std::chrono::seconds(opts_.maxRetryInterval);
  302. }
  303. /**
  304. * Sets whether the server should remember state for the client across
  305. * reconnects. (MQTT v3.x only)
  306. *
  307. * This will only take effect if the version is _already_ set to v3.x
  308. * (not v5).
  309. *
  310. * @param cleanSession @em true if the server should NOT remember state
  311. * for the client across reconnects, @em false otherwise.
  312. */
  313. void set_clean_session(bool cleanSession);
  314. /**
  315. * Sets whether the server should remember state for the client across
  316. * reconnects. (MQTT v5 only)
  317. *
  318. * If a persistent session is desired (turning this off), then the app
  319. * should also set the `Session Expiry Interval` property, and add that
  320. * to the connect options.
  321. *
  322. * This will only take effect if the MQTT version is set to v5
  323. *
  324. * @param cleanStart @em true if the server should NOT remember state
  325. * for the client across reconnects, @em false otherwise.
  326. */
  327. void set_clean_start(bool cleanStart);
  328. /**
  329. * Sets the "keep alive" interval.
  330. * This is the maximum time that should pass without communications
  331. * between client and server. If no messages pass in this time, the
  332. * client will ping the broker.
  333. * @param keepAliveInterval The keep alive interval in seconds.
  334. */
  335. void set_keep_alive_interval(int keepAliveInterval) {
  336. opts_.keepAliveInterval = keepAliveInterval;
  337. }
  338. /**
  339. * Sets the "keep alive" interval with a chrono duration.
  340. * This is the maximum time that should pass without communications
  341. * between client and server. If no messages pass in this time, the
  342. * client will ping the broker.
  343. * @param interval The keep alive interval.
  344. */
  345. template <class Rep, class Period>
  346. void set_keep_alive_interval(const std::chrono::duration<Rep, Period>& interval) {
  347. // TODO: Check range
  348. set_keep_alive_interval((int)to_seconds_count(interval));
  349. }
  350. /**
  351. * Sets the connect timeout in seconds.
  352. * This is the maximum time that the underlying library will wait for a
  353. * connection before failing.
  354. * @param timeout The connect timeout in seconds.
  355. */
  356. void set_connect_timeout(int timeout) { opts_.connectTimeout = timeout; }
  357. /**
  358. * Sets the connect timeout with a chrono duration.
  359. * This is the maximum time that the underlying library will wait for a
  360. * connection before failing.
  361. * @param timeout The connect timeout in seconds.
  362. */
  363. template <class Rep, class Period>
  364. void set_connect_timeout(const std::chrono::duration<Rep, Period>& timeout) {
  365. // TODO: check range
  366. set_connect_timeout((int)to_seconds_count(timeout));
  367. }
  368. /**
  369. * Sets the user name to use for the connection.
  370. * @param userName The user name for connecting to the MQTT broker.
  371. */
  372. void set_user_name(string_ref userName);
  373. /**
  374. * Sets the password to use for the connection.
  375. * @param password The password for connecting to the MQTT broker.
  376. */
  377. void set_password(binary_ref password);
  378. /**
  379. * Sets the maximum number of messages that can be in-flight
  380. * simultaneously.
  381. * @param n The maximum number of inflight messages.
  382. */
  383. void set_max_inflight(int n) { opts_.maxInflight = n; }
  384. /**
  385. * Sets the "Last Will and Testament" (LWT) for the connection.
  386. * @param will The LWT options.
  387. */
  388. void set_will(const will_options& will);
  389. /**
  390. * Sets the "Last Will and Testament" (LWT) for the connection.
  391. * @param will The LWT options.
  392. */
  393. void set_will(will_options&& will);
  394. /**
  395. * Sets the "Last Will and Testament" (LWT) as a message
  396. * @param msg The LWT message
  397. */
  398. void set_will_message(const message& msg) { set_will(will_options(msg)); }
  399. /**
  400. * Sets the "Last Will and Testament" (LWT) as a message
  401. * @param msg Pointer to a LWT message
  402. */
  403. void set_will_message(const_message_ptr msg) {
  404. if (msg)
  405. set_will(will_options(*msg));
  406. }
  407. /**
  408. * Sets the callback context to a delivery token.
  409. * @param tok The delivery token to be used as the callback context.
  410. */
  411. void set_token(const token_ptr& tok);
  412. /**
  413. * Sets the list of servers to which the client will connect.
  414. * @param serverURIs A pointer to a collection of server URI's. Each
  415. * entry should be of the form @em
  416. * protocol://host:port where @em protocol must be
  417. * @em tcp or @em ssl. For @em host, you can specify
  418. * either an IP address or a domain name.
  419. */
  420. void set_servers(const_string_collection_ptr serverURIs);
  421. /**
  422. * Sets the version of MQTT to be used on the connect.
  423. *
  424. * This will also set other connect options to legal values dependent on
  425. * the selected version.
  426. *
  427. * @param mqttVersion The MQTT version to use for the connection:
  428. * @li MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if
  429. * that fails, fall back to 3.1
  430. * @li MQTTVERSION_3_1 (3) = only try version 3.1
  431. * @li MQTTVERSION_3_1_1 (4) = only try version 3.1.1
  432. * @li MQTTVERSION_5 (5) = only try version 5
  433. *
  434. * Note that it is preferable to create the options for the desired
  435. * version rather than using this function to change the version after
  436. * some parameters have already been set. If you do use this function,
  437. * call it before setting any other version-specific options. @sa
  438. * connect_options::v5()
  439. */
  440. void set_mqtt_version(int mqttVersion);
  441. /**
  442. * Enable or disable automatic reconnects.
  443. * The retry intervals are not affected.
  444. * @param on Whether to turn reconnects on or off
  445. */
  446. void set_automatic_reconnect(bool on) { opts_.automaticReconnect = to_int(on); }
  447. /**
  448. * Enable or disable automatic reconnects.
  449. * @param minRetryInterval Minimum retry interval in seconds. Doubled
  450. * on each failed retry.
  451. * @param maxRetryInterval Maximum retry interval in seconds. The
  452. * doubling stops here on failed retries.
  453. */
  454. void set_automatic_reconnect(int minRetryInterval, int maxRetryInterval);
  455. /**
  456. * Enable or disable automatic reconnects.
  457. * @param minRetryInterval Minimum retry interval. Doubled on each
  458. * failed retry.
  459. * @param maxRetryInterval Maximum retry interval. The doubling stops
  460. * here on failed retries.
  461. */
  462. template <class Rep1, class Period1, class Rep2, class Period2>
  463. void set_automatic_reconnect(
  464. const std::chrono::duration<Rep1, Period1>& minRetryInterval,
  465. const std::chrono::duration<Rep2, Period2>& maxRetryInterval
  466. ) {
  467. set_automatic_reconnect(
  468. (int)to_seconds_count(minRetryInterval), (int)to_seconds_count(maxRetryInterval)
  469. );
  470. }
  471. /**
  472. * Gets the connect properties.
  473. * @return A const reference to the properties for the connect.
  474. */
  475. const properties& get_properties() const { return props_; }
  476. /**
  477. * Gets a mutable reference to the connect properties.
  478. * @return A reference to the properties for the connect.
  479. */
  480. properties& get_properties() { return props_; }
  481. /**
  482. * Sets the properties for the connect.
  483. * @param props The properties to place into the message.
  484. */
  485. void set_properties(const properties& props);
  486. /**
  487. * Moves the properties for the connect.
  488. * @param props The properties to move into the connect object.
  489. */
  490. void set_properties(properties&& props);
  491. /**
  492. * Gets the HTTP headers
  493. * @return A const reference to the HTTP headers name/value collection.
  494. */
  495. const name_value_collection& get_http_headers() const { return httpHeaders_; }
  496. /**
  497. * Sets the HTTP headers for the connection.
  498. * @param httpHeaders The header nam/value collection.
  499. */
  500. void set_http_headers(const name_value_collection& httpHeaders) {
  501. httpHeaders_ = httpHeaders;
  502. opts_.httpHeaders = httpHeaders_.empty() ? nullptr : httpHeaders_.c_arr();
  503. }
  504. /**
  505. * Sets the HTTP headers for the connection.
  506. * @param httpHeaders The header nam/value collection.
  507. */
  508. void set_http_headers(name_value_collection&& httpHeaders) {
  509. httpHeaders_ = std::move(httpHeaders);
  510. opts_.httpHeaders = httpHeaders_.empty() ? nullptr : httpHeaders_.c_arr();
  511. }
  512. /**
  513. * Gets the HTTP proxy setting.
  514. * @return The HTTP proxy setting. An empty string means no proxy.
  515. */
  516. string get_http_proxy() const { return httpProxy_; }
  517. /**
  518. * Sets the HTTP proxy setting.
  519. * @param httpProxy The HTTP proxy setting. An empty string means no
  520. * proxy.
  521. */
  522. void set_http_proxy(const string& httpProxy);
  523. /**
  524. * Gets the secure HTTPS proxy setting.
  525. * @return The HTTPS proxy setting. An empty string means no proxy.
  526. */
  527. string get_https_proxy() const { return httpsProxy_; }
  528. /**
  529. * Sets the secure HTTPS proxy setting.
  530. * @param httpsProxy The HTTPS proxy setting. An empty string means no
  531. * proxy.
  532. */
  533. void set_https_proxy(const string& httpsProxy);
  534. };
  535. /** Smart/shared pointer to a connection options object. */
  536. using connect_options_ptr = connect_options::ptr_t;
  537. /////////////////////////////////////////////////////////////////////////////
  538. /**
  539. * The connect options that can be updated before an automatic reconnect.
  540. */
  541. class connect_data
  542. {
  543. /** The default C struct */
  544. PAHO_MQTTPP_EXPORT static constexpr MQTTAsync_connectData DFLT_C_STRUCT
  545. MQTTAsync_connectData_initializer;
  546. /** The underlying C connect data */
  547. MQTTAsync_connectData data_{DFLT_C_STRUCT};
  548. /** The user name to use for the connection. */
  549. string_ref userName_;
  550. /** The password to use for the connection. (Optional) */
  551. binary_ref password_;
  552. /** The client has special access */
  553. friend class async_client;
  554. /**
  555. * Updates the underlying C structure to match our strings.
  556. */
  557. void update_c_struct();
  558. /**
  559. * Create data from a C struct
  560. * This is a deep copy of the data from the C struct.
  561. * @param cdata The C connect data.
  562. */
  563. connect_data(const MQTTAsync_connectData& cdata);
  564. public:
  565. /**
  566. * Creates an empty set of connection data.
  567. */
  568. connect_data();
  569. /**
  570. * Creates connection data with a user name, but no password.
  571. * @param userName The user name for reconnecting to the MQTT broker.
  572. */
  573. explicit connect_data(string_ref userName);
  574. /**
  575. * Creates connection data with a user name and password.
  576. * @param userName The user name for reconnecting to the MQTT broker.
  577. * @param password The password for connecting to the MQTT broker.
  578. */
  579. connect_data(string_ref userName, binary_ref password);
  580. /**
  581. * Copy constructor
  582. * @param other Another data struct to copy into this one.
  583. */
  584. connect_data(const connect_data& other);
  585. /**
  586. * Copy the connection data.
  587. * @param rhs Another data struct to copy into this one.
  588. * @return A reference to this data
  589. */
  590. connect_data& operator=(const connect_data& rhs);
  591. /**
  592. * Gets the user name to use for the connection.
  593. * @return The user name to use for the connection.
  594. */
  595. string get_user_name() const { return userName_ ? userName_.to_string() : string(); }
  596. /**
  597. * Gets the password to use for the connection.
  598. * @return The password to use for the connection.
  599. */
  600. binary_ref get_password() const { return password_; }
  601. /**
  602. * Sets the user name to use for the connection.
  603. * @param userName The user name for connecting to the MQTT broker.
  604. */
  605. void set_user_name(string_ref userName);
  606. /**
  607. * Sets the password to use for the connection.
  608. * @param password The password for connecting to the MQTT broker.
  609. */
  610. void set_password(binary_ref password);
  611. };
  612. /////////////////////////////////////////////////////////////////////////////
  613. /**
  614. * Class to build connect options.
  615. */
  616. class connect_options_builder
  617. {
  618. connect_options opts_;
  619. public:
  620. /** This class */
  621. using self = connect_options_builder;
  622. /**
  623. * Default constructor.
  624. *
  625. * @param ver The MQTT version for the connection. Defaults to the most
  626. * recent v3 supported by the server.
  627. */
  628. explicit connect_options_builder(int ver = MQTTVERSION_DEFAULT) : opts_(ver) {}
  629. /**
  630. * Copy constructor from an existing set of options.
  631. */
  632. explicit connect_options_builder(const connect_options& opts) : opts_(opts) {}
  633. /**
  634. * Move constructor from an existing set of options.
  635. */
  636. explicit connect_options_builder(const connect_options&& opts) : opts_(std::move(opts)) {}
  637. /**
  638. * Creates the default options builder for an MQTT v3.x connection.
  639. * @return An options builder for an MQTT v3.x connection.
  640. */
  641. static connect_options_builder v3() {
  642. return connect_options_builder{connect_options::v3()};
  643. }
  644. /**
  645. * Creates the default options builder for an MQTT v5 connection.
  646. * @return An options builder for an MQTT v5 connection.
  647. */
  648. static connect_options_builder v5() {
  649. return connect_options_builder{connect_options::v5()};
  650. }
  651. /**
  652. * Creates the default options builder for an MQTT v3.x connection using
  653. * WebSockets.
  654. *
  655. * The keepalive interval is set to 45 seconds to avoid webserver 60
  656. * second inactivity timeouts.
  657. *
  658. * @return An options builder for an MQTT v3.x connection using
  659. * websockets.
  660. */
  661. static connect_options_builder ws() {
  662. return connect_options_builder{connect_options::ws()};
  663. }
  664. /**
  665. * Creates the default options for an MQTT v5 connection using
  666. * WebSockets
  667. * .
  668. * The keepalive interval is set to 45 seconds to avoid webserver 60
  669. * second inactivity timeouts.
  670. *
  671. * @return An options builder for an MQTT v5 connection using
  672. * websockets.
  673. */
  674. static connect_options_builder v5_ws() {
  675. return connect_options_builder{connect_options::v5_ws()};
  676. }
  677. /**
  678. * Sets whether the server should remember state for the client across
  679. * reconnects. (MQTT v3.x only)
  680. * @param on @em true if the server should NOT remember state for the
  681. * client across reconnects, @em false otherwise.
  682. */
  683. auto clean_session(bool on = true) -> self& {
  684. opts_.set_clean_session(on);
  685. return *this;
  686. }
  687. /**
  688. * Sets the "keep alive" interval with a chrono duration.
  689. * This is the maximum time that should pass without communications
  690. * between client and server. If no messages pass in this time, the
  691. * client will ping the broker.
  692. * @param interval The keep alive interval.
  693. */
  694. template <class Rep, class Period>
  695. auto keep_alive_interval(const std::chrono::duration<Rep, Period>& interval) -> self& {
  696. opts_.set_keep_alive_interval(interval);
  697. return *this;
  698. }
  699. /**
  700. * Sets the connect timeout with a chrono duration.
  701. * This is the maximum time that the underlying library will wait for a
  702. * connection before failing.
  703. * @param timeout The connect timeout in seconds.
  704. */
  705. template <class Rep, class Period>
  706. auto connect_timeout(const std::chrono::duration<Rep, Period>& timeout) -> self& {
  707. opts_.set_connect_timeout(timeout);
  708. return *this;
  709. }
  710. /**
  711. * Sets the user name for the connection.
  712. * @param userName The user name for the connection.
  713. */
  714. auto user_name(string_ref userName) -> self& {
  715. opts_.set_user_name(userName);
  716. return *this;
  717. }
  718. /**
  719. * Sets the password for the connection.
  720. * @param password The password for the connection.
  721. */
  722. auto password(binary_ref password) -> self& {
  723. opts_.set_password(password);
  724. return *this;
  725. }
  726. /**
  727. * Sets the maximum number of messages that can be in-flight
  728. * simultaneously.
  729. * @param n The maximum number of inflight messages.
  730. */
  731. auto max_inflight(int n) -> self& {
  732. opts_.set_max_inflight(n);
  733. return *this;
  734. }
  735. /**
  736. * Sets the "Last Will and Testament" (LWT) for the connection.
  737. * @param will The LWT options.
  738. */
  739. auto will(const will_options& will) -> self& {
  740. opts_.set_will(will);
  741. return *this;
  742. }
  743. /**
  744. * Sets the "Last Will and Testament" (LWT) for the connection.
  745. * @param will The LWT options.
  746. */
  747. auto will(will_options&& will) -> self& {
  748. opts_.set_will(std::move(will));
  749. return *this;
  750. }
  751. /**
  752. * Sets the "Last Will and Testament" (LWT) as a message
  753. * @param msg The LWT message
  754. */
  755. auto will(const message& msg) -> self& {
  756. opts_.set_will_message(msg);
  757. return *this;
  758. }
  759. /**
  760. * Sets the SSL options for the connection.
  761. * These will only have an effect if compiled against the SSL version of
  762. * the Paho C library, and connecting with a secure URI.
  763. * @param ssl The SSL options.
  764. */
  765. auto ssl(const ssl_options& ssl) -> self& {
  766. opts_.set_ssl(ssl);
  767. return *this;
  768. }
  769. /**
  770. * Sets the SSL options for the connection.
  771. * These will only have an effect if compiled against the SSL version of
  772. * the Paho C library, and connecting with a secure URI.
  773. * @param ssl The SSL options.
  774. */
  775. auto ssl(ssl_options&& ssl) -> self& {
  776. opts_.set_ssl(std::move(ssl));
  777. return *this;
  778. }
  779. /**
  780. * Sets the callback context to a delivery token.
  781. * @param tok The delivery token to be used as the callback context.
  782. */
  783. auto token(const token_ptr& tok) -> self& {
  784. opts_.set_token(tok);
  785. return *this;
  786. }
  787. /**
  788. * Sets the list of servers to which the client will connect.
  789. * @param serverURIs A pointer to a collection of server URI's. Each
  790. * entry should be of the form @em
  791. * protocol://host:port where @em protocol must be
  792. * @em tcp or @em ssl. For @em host, you can specify
  793. * either an IP address or a domain name.
  794. */
  795. auto servers(const_string_collection_ptr serverURIs) -> self& {
  796. opts_.set_servers(serverURIs);
  797. return *this;
  798. }
  799. /**
  800. * Sets the version of MQTT to be used on the connect.
  801. *
  802. * This will also set other connect options to legal values dependent on
  803. * the selected version.
  804. *
  805. * @param ver The MQTT protocol version to use for the connection:
  806. * @li MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if
  807. * that fails, fall back to 3.1
  808. * @li MQTTVERSION_3_1 (3) = only try version 3.1
  809. * @li MQTTVERSION_3_1_1 (4) = only try version 3.1.1
  810. * @li MQTTVERSION_5 (5) = only try version 5
  811. *
  812. * Note that it is preferable to create the options builder for the
  813. * desired version rather than using this function to change the version
  814. * after some parameters have already been set. If you do use this
  815. * function, call it before setting any other version-specific options.
  816. * @sa connect_options_builder::v5()
  817. */
  818. auto mqtt_version(int ver) -> self& {
  819. opts_.set_mqtt_version(ver);
  820. return *this;
  821. }
  822. /**
  823. * Enable or disable automatic reconnects.
  824. * The retry intervals are not affected.
  825. * @param on Whether to turn reconnects on or off
  826. */
  827. auto automatic_reconnect(bool on = true) -> self& {
  828. opts_.set_automatic_reconnect(on);
  829. return *this;
  830. }
  831. /**
  832. * Enable or disable automatic reconnects.
  833. * @param minRetryInterval Minimum retry interval. Doubled on each
  834. * failed retry.
  835. * @param maxRetryInterval Maximum retry interval. The doubling stops
  836. * here on failed retries.
  837. */
  838. template <class Rep1, class Period1, class Rep2, class Period2>
  839. auto automatic_reconnect(
  840. const std::chrono::duration<Rep1, Period1>& minRetryInterval,
  841. const std::chrono::duration<Rep2, Period2>& maxRetryInterval
  842. ) -> self& {
  843. opts_.set_automatic_reconnect(minRetryInterval, maxRetryInterval);
  844. return *this;
  845. }
  846. /**
  847. * Sets the 'clean start' flag for the connection. (MQTT v5 only)
  848. * @param on @em true to set the 'clean start' flag for the connect,
  849. * @em false otherwise.
  850. */
  851. auto clean_start(bool on = true) -> self& {
  852. opts_.set_clean_start(on);
  853. return *this;
  854. }
  855. /**
  856. * Sets the properties for the connect message.
  857. * @param props The properties for the connect message.
  858. */
  859. auto properties(const mqtt::properties& props) -> self& {
  860. opts_.set_properties(props);
  861. return *this;
  862. }
  863. /**
  864. * Sets the properties for the connect message.
  865. * @param props The properties for the connect message.
  866. */
  867. auto properties(mqtt::properties&& props) -> self& {
  868. opts_.set_properties(std::move(props));
  869. return *this;
  870. }
  871. /**
  872. * Sets the HTTP headers for the connection.
  873. * @param headers The header nam/value collection.
  874. */
  875. auto http_headers(const name_value_collection& headers) -> self& {
  876. opts_.set_http_headers(headers);
  877. return *this;
  878. }
  879. /**
  880. * Sets the HTTP headers for the connection.
  881. * @param headers The header nam/value collection.
  882. */
  883. auto http_headers(name_value_collection&& headers) -> self& {
  884. opts_.set_http_headers(std::move(headers));
  885. return *this;
  886. }
  887. /**
  888. * Sets the HTTP proxy setting.
  889. * @param httpProxy The HTTP proxy setting. An empty string means no
  890. * proxy.
  891. */
  892. auto http_proxy(const string& httpProxy) -> self& {
  893. opts_.set_http_proxy(httpProxy);
  894. return *this;
  895. }
  896. /**
  897. * Sets the secure HTTPS proxy setting.
  898. * @param httpsProxy The HTTPS proxy setting. An empty string means no
  899. * proxy.
  900. */
  901. auto https_proxy(const string& httpsProxy) -> self& {
  902. opts_.set_https_proxy(httpsProxy);
  903. return *this;
  904. }
  905. /**
  906. * Finish building the options and return them.
  907. * @return The option struct as built.
  908. */
  909. connect_options finalize() { return opts_; }
  910. };
  911. /////////////////////////////////////////////////////////////////////////////
  912. } // namespace mqtt
  913. #endif // __mqtt_connect_options_h