Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:38:22

0001 //
0002 // Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MQTT5_SENTRY_OP_HPP
0009 #define BOOST_MQTT5_SENTRY_OP_HPP
0010 
0011 #include <boost/mqtt5/error.hpp>
0012 #include <boost/mqtt5/types.hpp>
0013 
0014 #include <boost/mqtt5/impl/disconnect_op.hpp>
0015 
0016 #include <boost/asio/prepend.hpp>
0017 
0018 #include <chrono>
0019 #include <memory>
0020 
0021 namespace boost::mqtt5::detail {
0022 
0023 namespace asio = boost::asio;
0024 
0025 template <typename ClientService, typename Handler>
0026 class sentry_op {
0027     using client_service = ClientService;
0028     using handler_type = Handler;
0029 
0030     struct on_timer {};
0031     struct on_disconnect {};
0032 
0033     static constexpr auto check_interval = std::chrono::seconds(3);
0034 
0035     std::shared_ptr<client_service> _svc_ptr;
0036     handler_type _handler;
0037 
0038 public:
0039     sentry_op(std::shared_ptr<client_service> svc_ptr, Handler&& handler) :
0040         _svc_ptr(std::move(svc_ptr)), _handler(std::move(handler))
0041     {}
0042 
0043     sentry_op(sentry_op&&) noexcept = default;
0044     sentry_op(const sentry_op&) = delete;
0045 
0046     sentry_op& operator=(sentry_op&&) noexcept = default;
0047     sentry_op& operator=(const sentry_op&) = delete;
0048 
0049     using allocator_type = asio::associated_allocator_t<handler_type>;
0050     allocator_type get_allocator() const noexcept {
0051         return asio::get_associated_allocator(_handler);
0052     }
0053 
0054     using executor_type = typename client_service::executor_type;
0055     executor_type get_executor() const noexcept {
0056         return _svc_ptr->get_executor();
0057     }
0058 
0059     void perform() {
0060         _svc_ptr->_sentry_timer.expires_after(check_interval);
0061         _svc_ptr->_sentry_timer.async_wait(
0062             asio::prepend(std::move(*this), on_timer {})
0063         );
0064     }
0065 
0066     void operator()(on_timer, error_code) {
0067         if (!_svc_ptr->is_open())
0068             return complete();
0069 
0070         if (_svc_ptr->_replies.any_expired()) {
0071             auto props = disconnect_props {};
0072             // TODO add what packet was expected?
0073             props[prop::reason_string] = "No reply received within 20 seconds";
0074             auto svc_ptr = _svc_ptr;
0075             return async_disconnect(
0076                 disconnect_rc_e::unspecified_error, props, svc_ptr,
0077                 asio::prepend(std::move(*this), on_disconnect {})
0078             );
0079         }
0080 
0081         perform();
0082     }
0083 
0084     void operator()(on_disconnect, error_code ec) {
0085         if (ec)
0086             return complete();
0087         
0088         perform();
0089     }
0090 
0091 private:
0092     void complete() {
0093         return std::move(_handler)();
0094     }
0095 };
0096 
0097 
0098 } // end namespace boost::mqtt5::detail
0099 
0100 #endif // !BOOST_MQTT5_SENTRY_OP_HPP