unsubscribe_op.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MQTT5_UNSUBSCRIBE_OP_HPP
  8. #define BOOST_MQTT5_UNSUBSCRIBE_OP_HPP
  9. #include <boost/mqtt5/error.hpp>
  10. #include <boost/mqtt5/reason_codes.hpp>
  11. #include <boost/mqtt5/types.hpp>
  12. #include <boost/mqtt5/detail/cancellable_handler.hpp>
  13. #include <boost/mqtt5/detail/control_packet.hpp>
  14. #include <boost/mqtt5/detail/internal_types.hpp>
  15. #include <boost/mqtt5/detail/topic_validation.hpp>
  16. #include <boost/mqtt5/impl/codecs/message_decoders.hpp>
  17. #include <boost/mqtt5/impl/codecs/message_encoders.hpp>
  18. #include <boost/mqtt5/impl/disconnect_op.hpp>
  19. #include <boost/asio/associated_allocator.hpp>
  20. #include <boost/asio/associated_cancellation_slot.hpp>
  21. #include <boost/asio/associated_executor.hpp>
  22. #include <boost/asio/cancellation_type.hpp>
  23. #include <boost/asio/detached.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/prepend.hpp>
  26. #include <cstdint>
  27. #include <memory>
  28. #include <string>
  29. #include <vector>
  30. namespace boost::mqtt5::detail {
  31. namespace asio = boost::asio;
  32. template <typename ClientService, typename Handler>
  33. class unsubscribe_op {
  34. using client_service = ClientService;
  35. struct on_unsubscribe {};
  36. struct on_unsuback {};
  37. std::shared_ptr<client_service> _svc_ptr;
  38. using handler_type = cancellable_handler<
  39. Handler,
  40. typename client_service::executor_type
  41. >;
  42. handler_type _handler;
  43. size_t _num_topics { 0 };
  44. public:
  45. unsubscribe_op(
  46. std::shared_ptr<client_service> svc_ptr,
  47. Handler&& handler
  48. ) :
  49. _svc_ptr(std::move(svc_ptr)),
  50. _handler(std::move(handler), _svc_ptr->get_executor())
  51. {
  52. auto slot = asio::get_associated_cancellation_slot(_handler);
  53. if (slot.is_connected())
  54. slot.assign([&svc = *_svc_ptr](asio::cancellation_type_t) {
  55. svc.cancel();
  56. });
  57. }
  58. unsubscribe_op(unsubscribe_op&&) = default;
  59. unsubscribe_op(const unsubscribe_op&) = delete;
  60. unsubscribe_op& operator=(unsubscribe_op&&) = default;
  61. unsubscribe_op& operator=(const unsubscribe_op&) = delete;
  62. using allocator_type = asio::associated_allocator_t<handler_type>;
  63. allocator_type get_allocator() const noexcept {
  64. return asio::get_associated_allocator(_handler);
  65. }
  66. using executor_type = typename client_service::executor_type;
  67. executor_type get_executor() const noexcept {
  68. return _svc_ptr->get_executor();
  69. }
  70. void perform(
  71. const std::vector<std::string>& topics,
  72. const unsubscribe_props& props
  73. ) {
  74. _num_topics = topics.size();
  75. uint16_t packet_id = _svc_ptr->allocate_pid();
  76. if (packet_id == 0)
  77. return complete_immediate(client::error::pid_overrun, packet_id);
  78. if (_num_topics == 0)
  79. return complete_immediate(client::error::invalid_topic, packet_id);
  80. auto ec = validate_unsubscribe(topics, props);
  81. if (ec)
  82. return complete_immediate(ec, packet_id);
  83. auto unsubscribe = control_packet<allocator_type>::of(
  84. with_pid, get_allocator(),
  85. encoders::encode_unsubscribe, packet_id,
  86. topics, props
  87. );
  88. auto max_packet_size = _svc_ptr->connack_property(prop::maximum_packet_size)
  89. .value_or(default_max_send_size);
  90. if (unsubscribe.size() > max_packet_size)
  91. return complete_immediate(client::error::packet_too_large, packet_id);
  92. send_unsubscribe(std::move(unsubscribe));
  93. }
  94. void send_unsubscribe(control_packet<allocator_type> unsubscribe) {
  95. auto wire_data = unsubscribe.wire_data();
  96. _svc_ptr->async_send(
  97. wire_data,
  98. no_serial, send_flag::none,
  99. asio::prepend(
  100. std::move(*this), on_unsubscribe {}, std::move(unsubscribe)
  101. )
  102. );
  103. }
  104. void resend_unsubscribe(control_packet<allocator_type> subscribe) {
  105. if (_handler.cancelled() != asio::cancellation_type_t::none)
  106. return complete(
  107. asio::error::operation_aborted, subscribe.packet_id()
  108. );
  109. send_unsubscribe(std::move(subscribe));
  110. }
  111. void operator()(
  112. on_unsubscribe, control_packet<allocator_type> packet,
  113. error_code ec
  114. ) {
  115. if (ec == asio::error::try_again)
  116. return resend_unsubscribe(std::move(packet));
  117. auto packet_id = packet.packet_id();
  118. if (ec)
  119. return complete(ec, packet_id);
  120. _svc_ptr->async_wait_reply(
  121. control_code_e::unsuback, packet_id,
  122. asio::prepend(std::move(*this), on_unsuback {}, std::move(packet))
  123. );
  124. }
  125. void operator()(
  126. on_unsuback, control_packet<allocator_type> packet,
  127. error_code ec, byte_citer first, byte_citer last
  128. ) {
  129. if (ec == asio::error::try_again) // "resend unanswered"
  130. return resend_unsubscribe(std::move(packet));
  131. uint16_t packet_id = packet.packet_id();
  132. if (ec)
  133. return complete(ec, packet_id);
  134. auto unsuback = decoders::decode_unsuback(
  135. static_cast<uint32_t>(std::distance(first, last)), first
  136. );
  137. if (!unsuback.has_value()) {
  138. on_malformed_packet("Malformed UNSUBACK: cannot decode");
  139. return resend_unsubscribe(std::move(packet));
  140. }
  141. auto& [props, rcs] = *unsuback;
  142. auto reason_codes = to_reason_codes(std::move(rcs));
  143. if (reason_codes.size() != _num_topics) {
  144. on_malformed_packet(
  145. "Malformed UNSUBACK: does not contain a "
  146. "valid Reason Code for every Topic Filter"
  147. );
  148. return resend_unsubscribe(std::move(packet));
  149. }
  150. complete(
  151. ec, packet_id, std::move(reason_codes), std::move(props)
  152. );
  153. }
  154. private:
  155. static error_code validate_unsubscribe(
  156. const std::vector<std::string>& topics,
  157. const unsubscribe_props& props
  158. ) {
  159. for (const auto& topic : topics)
  160. if (validate_topic_filter(topic) != validation_result::valid)
  161. return client::error::invalid_topic;
  162. const auto& user_properties = props[prop::user_property];
  163. for (const auto& user_property: user_properties)
  164. if (!is_valid_string_pair(user_property))
  165. return client::error::malformed_packet;
  166. return error_code {};
  167. }
  168. static std::vector<reason_code> to_reason_codes(std::vector<uint8_t> codes) {
  169. std::vector<reason_code> ret;
  170. for (uint8_t code : codes) {
  171. auto rc = to_reason_code<reason_codes::category::unsuback>(code);
  172. if (rc)
  173. ret.push_back(*rc);
  174. }
  175. return ret;
  176. }
  177. void on_malformed_packet(
  178. const std::string& reason
  179. ) {
  180. auto props = disconnect_props {};
  181. props[prop::reason_string] = reason;
  182. async_disconnect(
  183. disconnect_rc_e::malformed_packet, props, _svc_ptr,
  184. asio::detached
  185. );
  186. }
  187. void complete_immediate(error_code ec, uint16_t packet_id) {
  188. if (packet_id != 0)
  189. _svc_ptr->free_pid(packet_id);
  190. _handler.complete_immediate(
  191. ec, std::vector<reason_code>(_num_topics, reason_codes::empty),
  192. unsuback_props {}
  193. );
  194. }
  195. void complete(
  196. error_code ec, uint16_t packet_id,
  197. std::vector<reason_code> reason_codes = {}, unsuback_props props = {}
  198. ) {
  199. if (reason_codes.empty() && _num_topics)
  200. reason_codes = std::vector<reason_code>(_num_topics, reason_codes::empty);
  201. _svc_ptr->free_pid(packet_id);
  202. _handler.complete(ec, std::move(reason_codes), std::move(props));
  203. }
  204. };
  205. template <typename ClientService>
  206. class initiate_async_unsubscribe {
  207. std::shared_ptr<ClientService> _svc_ptr;
  208. public:
  209. explicit initiate_async_unsubscribe(std::shared_ptr<ClientService> svc_ptr) :
  210. _svc_ptr(std::move(svc_ptr))
  211. {}
  212. using executor_type = typename ClientService::executor_type;
  213. executor_type get_executor() const noexcept {
  214. return _svc_ptr->get_executor();
  215. }
  216. template <typename Handler>
  217. void operator()(
  218. Handler&& handler,
  219. const std::vector<std::string>& topics, const unsubscribe_props& props
  220. ) {
  221. detail::unsubscribe_op { _svc_ptr, std::move(handler) }
  222. .perform(topics, props);
  223. }
  224. };
  225. } // end namespace boost::mqtt5::detail
  226. #endif // !BOOST_MQTT5_UNSUBSCRIBE_OP_HPP