connect_options.h 31 KB

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