shutdown_op.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_SHUTDOWN_OP_HPP
  8. #define BOOST_MQTT5_SHUTDOWN_OP_HPP
  9. #include <boost/mqtt5/types.hpp>
  10. #include <boost/mqtt5/detail/async_traits.hpp>
  11. #include <boost/mqtt5/detail/shutdown.hpp>
  12. #include <boost/asio/any_completion_handler.hpp>
  13. #include <boost/asio/associated_allocator.hpp>
  14. #include <boost/asio/associated_cancellation_slot.hpp>
  15. #include <boost/asio/associated_executor.hpp>
  16. #include <boost/asio/async_result.hpp>
  17. #include <boost/asio/deferred.hpp>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/experimental/parallel_group.hpp>
  20. #include <boost/asio/ip/tcp.hpp>
  21. #include <boost/asio/prepend.hpp>
  22. #include <array>
  23. #include <chrono>
  24. namespace boost::mqtt5::detail {
  25. template <typename>
  26. constexpr bool is_basic_socket = false;
  27. template <typename P, typename E>
  28. constexpr bool is_basic_socket<asio::basic_stream_socket<P, E>> = true;
  29. namespace asio = boost::asio;
  30. template <typename Owner>
  31. class shutdown_op {
  32. struct on_locked {};
  33. struct on_shutdown {};
  34. Owner& _owner;
  35. using handler_type = asio::any_completion_handler<void (error_code)>;
  36. handler_type _handler;
  37. public:
  38. template <typename Handler>
  39. shutdown_op(Owner& owner, Handler&& handler) :
  40. _owner(owner), _handler(std::move(handler))
  41. {}
  42. shutdown_op(shutdown_op&&) = default;
  43. shutdown_op(const shutdown_op&) = delete;
  44. shutdown_op& operator=(shutdown_op&&) = default;
  45. shutdown_op& operator=(const shutdown_op&) = delete;
  46. using allocator_type = asio::associated_allocator_t<handler_type>;
  47. allocator_type get_allocator() const noexcept {
  48. return asio::get_associated_allocator(_handler);
  49. }
  50. using cancellation_slot_type =
  51. asio::associated_cancellation_slot_t<handler_type>;
  52. cancellation_slot_type get_cancellation_slot() const noexcept {
  53. return asio::get_associated_cancellation_slot(_handler);
  54. }
  55. using executor_type = typename Owner::executor_type;
  56. executor_type get_executor() const noexcept {
  57. return _owner.get_executor();
  58. }
  59. void perform() {
  60. if constexpr (is_basic_socket<typename Owner::stream_type>) {
  61. error_code ec;
  62. _owner._stream_ptr->shutdown(asio::socket_base::shutdown_both, ec);
  63. return std::move(_handler)(error_code {});
  64. }
  65. else {
  66. if (_owner._conn_mtx.is_locked())
  67. return std::move(_handler)(error_code{});
  68. auto s = std::move(_owner._stream_ptr);
  69. _owner.replace_next_layer(_owner.construct_next_layer());
  70. _owner.open();
  71. _owner._conn_mtx.lock(
  72. asio::prepend(std::move(*this), on_locked {}, std::move(s))
  73. );
  74. }
  75. }
  76. void operator()(on_locked, typename Owner::stream_ptr s, error_code ec) {
  77. if (ec == asio::error::operation_aborted)
  78. return complete(s, asio::error::operation_aborted);
  79. if (!_owner.is_open()) {
  80. _owner._conn_mtx.unlock();
  81. return complete(s, asio::error::operation_aborted);
  82. }
  83. namespace asioex = boost::asio::experimental;
  84. // wait max 5 seconds for the shutdown op to finish
  85. _owner._connect_timer.expires_after(std::chrono::seconds(5));
  86. auto init_shutdown = [](
  87. auto handler, typename Owner::stream_type& stream
  88. ) {
  89. async_shutdown(stream, std::move(handler));
  90. };
  91. auto timed_shutdown = asioex::make_parallel_group(
  92. asio::async_initiate<const asio::deferred_t, void(error_code)>(
  93. init_shutdown, asio::deferred, std::ref(*s)
  94. ),
  95. _owner._connect_timer.async_wait(asio::deferred)
  96. );
  97. timed_shutdown.async_wait(
  98. asioex::wait_for_one(),
  99. asio::prepend(
  100. std::move(*this), on_shutdown {},
  101. std::move(s)
  102. )
  103. );
  104. }
  105. void operator()(
  106. on_shutdown, typename Owner::stream_ptr sptr,
  107. std::array<std::size_t, 2> /* ord */,
  108. error_code /* shutdown_ec */, error_code /* timer_ec */
  109. ) {
  110. _owner._conn_mtx.unlock();
  111. if (!_owner.is_open())
  112. return complete(sptr, asio::error::operation_aborted);
  113. // ignore shutdown error_code
  114. complete(sptr, error_code {});
  115. }
  116. private:
  117. void complete(const typename Owner::stream_ptr& sptr, error_code ec) {
  118. asio::get_associated_cancellation_slot(_handler).clear();
  119. error_code close_ec;
  120. lowest_layer(*sptr).close(close_ec);
  121. std::move(_handler)(ec);
  122. }
  123. };
  124. } // end namespace boost::mqtt5::detail
  125. #endif // !BOOST_MQTT5_SHUTDOWN_OP_HPP