cancellable_handler.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_CANCELLABLE_HANDLER_HPP
  8. #define BOOST_MQTT5_CANCELLABLE_HANDLER_HPP
  9. #include <boost/mqtt5/detail/async_traits.hpp>
  10. #include <boost/asio/associated_allocator.hpp>
  11. #include <boost/asio/associated_cancellation_slot.hpp>
  12. #include <boost/asio/associated_executor.hpp>
  13. #include <boost/asio/associated_immediate_executor.hpp>
  14. #include <boost/asio/cancellation_state.hpp>
  15. #include <boost/asio/dispatch.hpp>
  16. #include <boost/asio/prepend.hpp>
  17. namespace boost::mqtt5::detail {
  18. template <typename Handler, typename Executor>
  19. class cancellable_handler {
  20. Executor _executor;
  21. Handler _handler;
  22. tracking_type<Handler, Executor> _handler_ex;
  23. asio::cancellation_state _cancellation_state;
  24. public:
  25. cancellable_handler(Handler&& handler, const Executor& ex) :
  26. _executor(ex),
  27. _handler(std::move(handler)),
  28. _handler_ex(tracking_executor(_handler, ex)),
  29. _cancellation_state(
  30. asio::get_associated_cancellation_slot(_handler),
  31. asio::enable_total_cancellation {},
  32. asio::enable_terminal_cancellation {}
  33. )
  34. {}
  35. cancellable_handler(cancellable_handler&&) = default;
  36. cancellable_handler(const cancellable_handler&) = delete;
  37. cancellable_handler& operator=(cancellable_handler&&) = default;
  38. cancellable_handler& operator=(const cancellable_handler&) = delete;
  39. using allocator_type = asio::associated_allocator_t<Handler>;
  40. allocator_type get_allocator() const noexcept {
  41. return asio::get_associated_allocator(_handler);
  42. }
  43. using cancellation_slot_type = asio::associated_cancellation_slot_t<Handler>;
  44. cancellation_slot_type get_cancellation_slot() const noexcept {
  45. return _cancellation_state.slot();
  46. }
  47. using executor_type = tracking_type<Handler, Executor>;
  48. executor_type get_executor() const noexcept {
  49. return _handler_ex;
  50. }
  51. using immediate_executor_type =
  52. asio::associated_immediate_executor_t<Handler, Executor>;
  53. immediate_executor_type get_immediate_executor() const noexcept {
  54. // get_associated_immediate_executor will require asio::execution::blocking.never
  55. // on the default executor.
  56. return asio::get_associated_immediate_executor(_handler, _executor);
  57. }
  58. asio::cancellation_type_t cancelled() const {
  59. return _cancellation_state.cancelled();
  60. }
  61. template <typename... Args>
  62. void complete(Args&&... args) {
  63. asio::get_associated_cancellation_slot(_handler).clear();
  64. asio::dispatch(
  65. _handler_ex,
  66. asio::prepend(std::move(_handler), std::forward<Args>(args)...)
  67. );
  68. }
  69. template <typename... Args>
  70. void complete_immediate(Args&&... args) {
  71. asio::get_associated_cancellation_slot(_handler).clear();
  72. auto ex = get_immediate_executor();
  73. asio::dispatch(
  74. ex,
  75. asio::prepend(std::move(_handler), std::forward<Args>(args)...)
  76. );
  77. }
  78. };
  79. } // end boost::mqtt5::detail
  80. #endif // !BOOST_MQTT5_CANCELLABLE_HANDLER_HPP