disconnect_options.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file disconnect_options.h
  3. /// Implementation of the class 'disconnect_options'
  4. /// @date 26-Aug-2016
  5. /////////////////////////////////////////////////////////////////////////////
  6. /****************************************************************************
  7. * Copyright (c) 2016-2017 Frank Pagliughi <fpagliughi@mindspring.com>
  8. *
  9. * All rights reserved. This program and the accompanying materials
  10. * are made available under the terms of the Eclipse Public License v1.0
  11. * and Eclipse Distribution License v1.0 which accompany this distribution.
  12. *
  13. * The Eclipse Public License is available at
  14. * http://www.eclipse.org/legal/epl-v10.html
  15. * and the Eclipse Distribution License is available at
  16. * http://www.eclipse.org/org/documents/edl-v10.php.
  17. *
  18. * Contributors:
  19. * Frank Pagliughi - initial implementation and documentation
  20. ***************************************************************************/
  21. #ifndef __mqtt_disconnect_options_h
  22. #define __mqtt_disconnect_options_h
  23. #include "MQTTAsync.h"
  24. #include "mqtt/types.h"
  25. #include "mqtt/token.h"
  26. #include "mqtt/properties.h"
  27. #include <chrono>
  28. namespace mqtt {
  29. /////////////////////////////////////////////////////////////////////////////
  30. /**
  31. * Options for disconnecting from an MQTT broker.
  32. */
  33. class disconnect_options
  34. {
  35. /** The default C struct */
  36. static const MQTTAsync_disconnectOptions DFLT_C_STRUCT;
  37. /** The underlying C disconnect options */
  38. MQTTAsync_disconnectOptions opts_;
  39. /** Shared token pointer for context, if any */
  40. token_ptr tok_;
  41. /** Disconnect message properties */
  42. properties props_;
  43. /** The client has special access */
  44. friend class async_client;
  45. friend class disconnect_options_test;
  46. public:
  47. /**
  48. * Create an empty delivery response object.
  49. */
  50. disconnect_options();
  51. /**
  52. * Creates disconnect options tied to the specific token.
  53. * @param timeout The timeout (in milliseconds).
  54. */
  55. disconnect_options(int timeout) : disconnect_options() {
  56. set_timeout(timeout);
  57. }
  58. /**
  59. * Creates disconnect options tied to the specific token.
  60. * @param to The timeout.
  61. */
  62. template <class Rep, class Period>
  63. disconnect_options(const std::chrono::duration<Rep, Period>& to)
  64. : disconnect_options() {
  65. set_timeout(to);
  66. }
  67. /**
  68. * Copy constructor.
  69. * @param opt Another object to copy.
  70. */
  71. disconnect_options(const disconnect_options& opt);
  72. /**
  73. * Move constructor.
  74. * @param opt Another object to move into this new one.
  75. */
  76. disconnect_options(disconnect_options&& opt);
  77. /**
  78. * Copy assignment.
  79. * @param opt Another object to copy.
  80. */
  81. disconnect_options& operator=(const disconnect_options& opt);
  82. /**
  83. * Move assignment.
  84. * @param opt Another object to move into this new one.
  85. */
  86. disconnect_options& operator=(disconnect_options&& opt);
  87. /**
  88. * Gets the timeout used for disconnecting.
  89. * @return The timeout for disconnecting (in milliseconds).
  90. */
  91. std::chrono::milliseconds get_timeout() const {
  92. return std::chrono::milliseconds(opts_.timeout);
  93. }
  94. /**
  95. * Sets the timeout for disconnecting.
  96. * This allows for any remaining in-flight messages to be delivered.
  97. * @param timeout The timeout (in milliseconds).
  98. */
  99. void set_timeout(int timeout) { opts_.timeout = timeout; }
  100. /**
  101. * Sets the connect timeout with a chrono duration.
  102. * This is the maximum time that the underlying library will wait for a
  103. * connection before failing.
  104. * @param to The connect timeout in seconds.
  105. */
  106. template <class Rep, class Period>
  107. void set_timeout(const std::chrono::duration<Rep, Period>& to) {
  108. // TODO: check range
  109. set_timeout((int) to_milliseconds_count(to));
  110. }
  111. /**
  112. * Sets the callback context to a delivery token.
  113. * @param tok The delivery token to be used as the callback context.
  114. * @param mqttVersion The version of MQTT we're using for the
  115. * connection.
  116. */
  117. void set_token(const token_ptr& tok, int mqttVersion);
  118. /**
  119. * Gets the callback context to a delivery token.
  120. * @return The delivery token to be used as the callback context.
  121. */
  122. token_ptr get_token() const { return tok_; }
  123. /**
  124. * Gets the connect properties.
  125. * @return A const reference to the properties for the connect.
  126. */
  127. const properties& get_properties() const { return props_; }
  128. /**
  129. * Sets the properties for the connect.
  130. * @param props The properties to place into the message.
  131. */
  132. void set_properties(const properties& props) {
  133. props_ = props;
  134. opts_.properties = props_.c_struct();
  135. }
  136. /**
  137. * Moves the properties for the connect.
  138. * @param props The properties to move into the connect object.
  139. */
  140. void set_properties(properties&& props) {
  141. props_ = props;
  142. opts_.properties = props_.c_struct();
  143. }
  144. /**
  145. * Gets the reason code for the disconnect.
  146. * @return The reason code for the disconnect.
  147. */
  148. ReasonCode get_reason_code() const {
  149. return ReasonCode(opts_.reasonCode);
  150. }
  151. /**
  152. * Sets the reason code for the disconnect.
  153. * @param code The reason code for the disconnect.
  154. */
  155. void set_reason_code(ReasonCode code) {
  156. opts_.reasonCode = MQTTReasonCodes(code);
  157. }
  158. };
  159. /////////////////////////////////////////////////////////////////////////////
  160. // end namespace 'mqtt'
  161. }
  162. #endif // __mqtt_disconnect_options_h