options.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef __ASIO2_MQTT_OPTIONS_HPP__
  11. #define __ASIO2_MQTT_OPTIONS_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <asio2/base/detail/util.hpp>
  16. #include <asio2/mqtt/message.hpp>
  17. namespace asio2::detail
  18. {
  19. class mqtt_options
  20. {
  21. public:
  22. using self = mqtt_options;
  23. /**
  24. * @brief constructor
  25. */
  26. mqtt_options()
  27. {
  28. _receive_maximum = static_cast<decltype(_receive_maximum )>(65535); // broker.hivemq.com is 10
  29. _topic_alias_maximum = static_cast<decltype(_topic_alias_maximum)>(65535); // broker.hivemq.com is 5
  30. _maximum_qos = static_cast<decltype(_maximum_qos )>(mqtt::qos_type::exactly_once);
  31. _retain_available = static_cast<decltype(_retain_available )>(1);
  32. }
  33. /**
  34. * @brief destructor
  35. */
  36. ~mqtt_options() = default;
  37. /**
  38. * set the mqtt options
  39. */
  40. inline self& set_mqtt_options(const mqtt_options& options)
  41. {
  42. this->_mqtt_options_copy_from(options);
  43. return (*this);
  44. }
  45. // The Client uses this value to limit the number of QoS 1 and QoS 2 publications that it is willing to
  46. // process concurrently. There is no mechanism to limit the QoS 0 publications that the Server might try to send.
  47. // The value of Receive Maximum applies only to the current Network Connection.
  48. // If the Receive Maximum value is absent then its value defaults to 65,535.
  49. template<class Integer>
  50. inline self& receive_maximum(Integer v)
  51. {
  52. static_assert(std::is_integral_v<detail::remove_cvref_t<Integer>>);
  53. _receive_maximum = static_cast<decltype(_receive_maximum)>(v);
  54. return (*this);
  55. }
  56. inline auto receive_maximum() const { return _receive_maximum; }
  57. // This value indicates the highest value that the Client will accept as a Topic Alias sent by the Server.
  58. // The Client uses this value to limit the number of Topic Aliases that it is willing to hold on this Connection.
  59. // The Server MUST NOT send a Topic Alias in a PUBLISH packet to the Client greater than Topic Alias Maximum [MQTT-3.1.2-26].
  60. // A value of 0 indicates that the Client does not accept any Topic Aliases on this connection.
  61. // If Topic Alias Maximum is absent or zero, the Server MUST NOT send any Topic Aliases to the Client [MQTT-3.1.2-27].
  62. template<class Integer>
  63. inline self& topic_alias_maximum(Integer v)
  64. {
  65. static_assert(std::is_integral_v<detail::remove_cvref_t<Integer>>);
  66. _topic_alias_maximum = static_cast<decltype(_topic_alias_maximum)>(v);
  67. return (*this);
  68. }
  69. inline auto topic_alias_maximum() const { return _topic_alias_maximum; }
  70. // Followed by a Byte with a value of either 0 or 1.
  71. // It is a Protocol Error to include Maximum QoS more than once, or to have a value other than 0 or 1.
  72. // If the Maximum QoS is absent, the Client uses a Maximum QoS of 2.
  73. template<class Integer>
  74. inline self& maximum_qos(Integer v)
  75. {
  76. static_assert(std::is_integral_v<detail::remove_cvref_t<Integer>>);
  77. ASIO2_ASSERT(v >= 0 && v <= 2);
  78. _maximum_qos = static_cast<decltype(_maximum_qos)>(v);
  79. return (*this);
  80. }
  81. inline auto maximum_qos() const { return _maximum_qos; }
  82. // If present, this byte declares whether the Server supports retained messages.
  83. // A value of 0 means that retained messages are not supported.
  84. // A value of 1 means retained messages are supported.
  85. // If not present, then retained messages are supported.
  86. // It is a Protocol Error to include Retain Available more than once or to use a value other than 0 or 1.
  87. inline self& retain_available(bool v)
  88. {
  89. _retain_available = static_cast<decltype(_retain_available)>(v);
  90. return (*this);
  91. }
  92. inline bool retain_available() const { return (_retain_available == static_cast<decltype(_retain_available)>(1)); }
  93. protected:
  94. template<class MqttOptions>
  95. inline void _mqtt_options_copy_from(const MqttOptions& o)
  96. {
  97. //this->payload_format_indicator ( o._payload_format_indicator );
  98. //this->message_expiry_interval ( o._message_expiry_interval );
  99. //this->content_type ( o._content_type );
  100. //this->response_topic ( o._response_topic );
  101. //this->correlation_data ( o._correlation_data );
  102. //this->subscription_identifier ( o._subscription_identifier );
  103. //this->session_expiry_interval ( o._session_expiry_interval );
  104. //this->assigned_client_identifier ( o._assigned_client_identifier );
  105. //this->server_keep_alive ( o._server_keep_alive );
  106. //this->authentication_method ( o._authentication_method );
  107. //this->authentication_data ( o._authentication_data );
  108. //this->request_problem_information ( o._request_problem_information );
  109. //this->will_delay_interval ( o._will_delay_interval );
  110. //this->request_response_information ( o._request_response_information );
  111. //this->response_information ( o._response_information );
  112. //this->server_reference ( o._server_reference );
  113. //this->reason_string ( o._reason_string );
  114. this->receive_maximum ( o._receive_maximum );//
  115. this->topic_alias_maximum ( o._topic_alias_maximum );//
  116. //this->topic_alias ( o._topic_alias );
  117. this->maximum_qos ( o._maximum_qos );//
  118. this->retain_available ( o._retain_available );//
  119. //this->user_property ( o._user_property );
  120. //this->maximum_packet_size ( o._maximum_packet_size );
  121. //this->wildcard_subscription_available ( o._wildcard_subscription_available );
  122. //this->subscription_identifier_available ( o._subscription_identifier_available );
  123. //this->shared_subscription_available ( o._shared_subscription_available );
  124. }
  125. protected:
  126. decltype(std::declval<mqtt::v5::payload_format_indicator >().value()) _payload_format_indicator {};
  127. decltype(std::declval<mqtt::v5::message_expiry_interval >().value()) _message_expiry_interval {};
  128. decltype(std::declval<mqtt::v5::content_type >().value()) _content_type {};
  129. decltype(std::declval<mqtt::v5::response_topic >().value()) _response_topic {};
  130. decltype(std::declval<mqtt::v5::correlation_data >().value()) _correlation_data {};
  131. decltype(std::declval<mqtt::v5::subscription_identifier >().value()) _subscription_identifier {};
  132. decltype(std::declval<mqtt::v5::session_expiry_interval >().value()) _session_expiry_interval {};
  133. decltype(std::declval<mqtt::v5::assigned_client_identifier >().value()) _assigned_client_identifier {};
  134. decltype(std::declval<mqtt::v5::server_keep_alive >().value()) _server_keep_alive {};
  135. decltype(std::declval<mqtt::v5::authentication_method >().value()) _authentication_method {};
  136. decltype(std::declval<mqtt::v5::authentication_data >().value()) _authentication_data {};
  137. decltype(std::declval<mqtt::v5::request_problem_information >().value()) _request_problem_information {};
  138. decltype(std::declval<mqtt::v5::will_delay_interval >().value()) _will_delay_interval {};
  139. decltype(std::declval<mqtt::v5::request_response_information >().value()) _request_response_information {};
  140. decltype(std::declval<mqtt::v5::response_information >().value()) _response_information {};
  141. decltype(std::declval<mqtt::v5::server_reference >().value()) _server_reference {};
  142. decltype(std::declval<mqtt::v5::reason_string >().value()) _reason_string {};
  143. decltype(std::declval<mqtt::v5::receive_maximum >().value()) _receive_maximum {};
  144. decltype(std::declval<mqtt::v5::topic_alias_maximum >().value()) _topic_alias_maximum {};
  145. decltype(std::declval<mqtt::v5::topic_alias >().value()) _topic_alias {};
  146. decltype(std::declval<mqtt::v5::maximum_qos >().value()) _maximum_qos {};
  147. decltype(std::declval<mqtt::v5::retain_available >().value()) _retain_available {};
  148. decltype(std::declval<mqtt::v5::user_property >().value()) _user_property {};
  149. decltype(std::declval<mqtt::v5::maximum_packet_size >().value()) _maximum_packet_size {};
  150. decltype(std::declval<mqtt::v5::wildcard_subscription_available >().value()) _wildcard_subscription_available {};
  151. decltype(std::declval<mqtt::v5::subscription_identifier_available >().value()) _subscription_identifier_available {};
  152. decltype(std::declval<mqtt::v5::shared_subscription_available >().value()) _shared_subscription_available {};
  153. };
  154. }
  155. namespace asio2::mqtt
  156. {
  157. using options = asio2::detail::mqtt_options;
  158. }
  159. namespace asio2
  160. {
  161. using mqtt_options = asio2::detail::mqtt_options;
  162. }
  163. #endif // !__ASIO2_MQTT_OPTIONS_HPP__