Back to home page

EIC code displayed by LXR

 
 

    


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

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_PING_OP_HPP
0009 #define BOOST_MQTT5_PING_OP_HPP
0010 
0011 #include <boost/mqtt5/detail/control_packet.hpp>
0012 #include <boost/mqtt5/detail/internal_types.hpp>
0013 
0014 #include <boost/mqtt5/impl/codecs/message_encoders.hpp>
0015 
0016 #include <boost/asio/consign.hpp>
0017 #include <boost/asio/error.hpp>
0018 #include <boost/asio/prepend.hpp>
0019 
0020 #include <chrono>
0021 #include <limits>
0022 
0023 namespace boost::mqtt5::detail {
0024 
0025 namespace asio = boost::asio;
0026 
0027 template <typename ClientService, typename Handler>
0028 class ping_op {
0029     using client_service = ClientService;
0030     using handler_type = Handler;
0031 
0032     struct on_timer {};
0033     struct on_pingreq {};
0034 
0035     std::shared_ptr<client_service> _svc_ptr;
0036     handler_type _handler;
0037 
0038 public:
0039     ping_op(std::shared_ptr<client_service> svc_ptr, Handler&& handler) :
0040         _svc_ptr(std::move(svc_ptr)), _handler(std::move(handler))
0041     {}
0042 
0043     ping_op(ping_op&&) noexcept = default;
0044     ping_op(const ping_op&) = delete;
0045 
0046     ping_op& operator=(ping_op&&) noexcept = default;
0047     ping_op& operator=(const ping_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->_ping_timer.expires_after(compute_wait_time());
0061         _svc_ptr->_ping_timer.async_wait(
0062             asio::prepend(std::move(*this), on_timer {})
0063         );
0064     }
0065 
0066     void operator()(on_timer, error_code ec) {
0067         if (!_svc_ptr->is_open())
0068             return complete();
0069         else if (ec == asio::error::operation_aborted)
0070             return perform();
0071 
0072         auto pingreq = control_packet<allocator_type>::of(
0073             no_pid, get_allocator(), encoders::encode_pingreq
0074         );
0075 
0076         auto wire_data = pingreq.wire_data();
0077         _svc_ptr->async_send(
0078             wire_data,
0079             no_serial, send_flag::none,
0080             asio::consign(
0081                 asio::prepend(std::move(*this), on_pingreq {}),
0082                 std::move(pingreq)
0083             )
0084         );
0085     }
0086 
0087     void operator()(on_pingreq, error_code ec) {
0088         if (!ec || ec == asio::error::try_again)
0089             return perform();
0090 
0091         complete();
0092     }
0093 
0094 private:
0095     duration compute_wait_time() const {
0096         auto negotiated_ka = _svc_ptr->negotiated_keep_alive();
0097         return negotiated_ka ?
0098             std::chrono::seconds(negotiated_ka) :
0099             duration((std::numeric_limits<duration::rep>::max)());
0100     }
0101 
0102     void complete() {
0103         return std::move(_handler)();
0104     }
0105 };
0106 
0107 
0108 } // end namespace boost::mqtt5::detail
0109 
0110 #endif // !BOOST_MQTT5_PING_OP_HPP