sentry_op.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_SENTRY_OP_HPP
  8. #define BOOST_MQTT5_SENTRY_OP_HPP
  9. #include <boost/mqtt5/error.hpp>
  10. #include <boost/mqtt5/types.hpp>
  11. #include <boost/mqtt5/impl/disconnect_op.hpp>
  12. #include <boost/asio/prepend.hpp>
  13. #include <chrono>
  14. #include <memory>
  15. namespace boost::mqtt5::detail {
  16. namespace asio = boost::asio;
  17. template <typename ClientService, typename Handler>
  18. class sentry_op {
  19. using client_service = ClientService;
  20. using handler_type = Handler;
  21. struct on_timer {};
  22. struct on_disconnect {};
  23. static constexpr auto check_interval = std::chrono::seconds(3);
  24. std::shared_ptr<client_service> _svc_ptr;
  25. handler_type _handler;
  26. public:
  27. sentry_op(std::shared_ptr<client_service> svc_ptr, Handler&& handler) :
  28. _svc_ptr(std::move(svc_ptr)), _handler(std::move(handler))
  29. {}
  30. sentry_op(sentry_op&&) noexcept = default;
  31. sentry_op(const sentry_op&) = delete;
  32. sentry_op& operator=(sentry_op&&) noexcept = default;
  33. sentry_op& operator=(const sentry_op&) = delete;
  34. using allocator_type = asio::associated_allocator_t<handler_type>;
  35. allocator_type get_allocator() const noexcept {
  36. return asio::get_associated_allocator(_handler);
  37. }
  38. using executor_type = typename client_service::executor_type;
  39. executor_type get_executor() const noexcept {
  40. return _svc_ptr->get_executor();
  41. }
  42. void perform() {
  43. _svc_ptr->_sentry_timer.expires_after(check_interval);
  44. _svc_ptr->_sentry_timer.async_wait(
  45. asio::prepend(std::move(*this), on_timer {})
  46. );
  47. }
  48. void operator()(on_timer, error_code) {
  49. if (!_svc_ptr->is_open())
  50. return complete();
  51. if (_svc_ptr->_replies.any_expired()) {
  52. auto props = disconnect_props {};
  53. // TODO add what packet was expected?
  54. props[prop::reason_string] = "No reply received within 20 seconds";
  55. auto svc_ptr = _svc_ptr;
  56. return async_disconnect(
  57. disconnect_rc_e::unspecified_error, props, svc_ptr,
  58. asio::prepend(std::move(*this), on_disconnect {})
  59. );
  60. }
  61. perform();
  62. }
  63. void operator()(on_disconnect, error_code ec) {
  64. if (ec)
  65. return complete();
  66. perform();
  67. }
  68. private:
  69. void complete() {
  70. return std::move(_handler)();
  71. }
  72. };
  73. } // end namespace boost::mqtt5::detail
  74. #endif // !BOOST_MQTT5_SENTRY_OP_HPP