forked from boostorg/mqtt5
Add function to set keep alive/ping interval
Summary: related to T13566 - mqtt_client has a new keep_alive(seconds) function - keep_alive(0) disables ping - if keep_alive() is not called, the client assumes keep_alive=10 - the client respects server_keep_alive if sent by the broker Reviewers: ivica Reviewed By: ivica Subscribers: miljen, iljazovic Differential Revision: https://repo.mireo.local/D27557
This commit is contained in:
1 parent
d6c4884d53
commit
33c8eea890
21 files changed
+361
-100
No files matched your search
@@ -70,6 +70,7 @@ private:
|
||||
struct mqtt_ctx {
|
||||
credentials creds;
|
||||
std::optional<will> will_msg;
|
||||
uint16_t keep_alive = 60;
|
||||
connect_props co_props;
|
||||
connack_props ca_props;
|
||||
session_state state;
|
||||
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
}
|
||||
|
||||
template <typename CompletionCondition>
|
||||
void perform(duration wait_for, CompletionCondition cc) {
|
||||
void perform(CompletionCondition cc) {
|
||||
_read_buff.erase(
|
||||
_read_buff.cbegin(), _data_span.first()
|
||||
);
|
||||
@@ -100,7 +100,7 @@ public:
|
||||
return asio::post(
|
||||
asio::prepend(
|
||||
std::move(*this), on_read {}, error_code {},
|
||||
0, wait_for, std::move(cc)
|
||||
0, std::move(cc)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -110,9 +110,9 @@ public:
|
||||
auto store_size = std::distance(_data_span.last(), _read_buff.cend());
|
||||
|
||||
_svc._stream.async_read_some(
|
||||
asio::buffer(store_begin, store_size), wait_for,
|
||||
asio::buffer(store_begin, store_size), compute_read_timeout(),
|
||||
asio::prepend(
|
||||
asio::append(std::move(*this), wait_for, std::move(cc)),
|
||||
asio::append(std::move(*this), std::move(cc)),
|
||||
on_read {}
|
||||
)
|
||||
);
|
||||
@@ -121,13 +121,13 @@ public:
|
||||
template <typename CompletionCondition>
|
||||
void operator()(
|
||||
on_read, error_code ec, size_t bytes_read,
|
||||
duration wait_for, CompletionCondition cc
|
||||
CompletionCondition cc
|
||||
) {
|
||||
if (ec == asio::error::try_again) {
|
||||
_svc.update_session_state();
|
||||
_svc._async_sender.resend();
|
||||
_data_span = { _read_buff.cend(), _read_buff.cend() };
|
||||
return perform(wait_for, std::move(cc));
|
||||
return perform(std::move(cc));
|
||||
}
|
||||
|
||||
if (ec)
|
||||
@@ -149,7 +149,7 @@ public:
|
||||
|
||||
if (!varlen) {
|
||||
if (_data_span.size() < 5)
|
||||
return perform(wait_for, asio::transfer_at_least(1));
|
||||
return perform(asio::transfer_at_least(1));
|
||||
return complete(client::error::malformed_packet, 0, {}, {});
|
||||
}
|
||||
|
||||
@@ -160,16 +160,23 @@ public:
|
||||
return complete(client::error::malformed_packet, 0, {}, {});
|
||||
|
||||
if (static_cast<uint32_t>(std::distance(first, _data_span.last())) < *varlen)
|
||||
return perform(wait_for, asio::transfer_at_least(1));
|
||||
return perform(asio::transfer_at_least(1));
|
||||
|
||||
_data_span.remove_prefix(
|
||||
std::distance(_data_span.first(), first) + *varlen
|
||||
);
|
||||
|
||||
dispatch(wait_for, control_byte, first, first + *varlen);
|
||||
dispatch(control_byte, first, first + *varlen);
|
||||
}
|
||||
|
||||
private:
|
||||
duration compute_read_timeout() const {
|
||||
auto negotiated_ka = _svc.negotiated_keep_alive();
|
||||
return negotiated_ka ?
|
||||
std::chrono::milliseconds(3 * negotiated_ka * 1000 / 2) :
|
||||
duration(std::numeric_limits<duration::rep>::max());
|
||||
}
|
||||
|
||||
static bool valid_header(uint8_t control_byte) {
|
||||
auto code = control_code_e(control_byte & 0b11110000);
|
||||
|
||||
@@ -183,7 +190,6 @@ private:
|
||||
}
|
||||
|
||||
void dispatch(
|
||||
duration wait_for,
|
||||
uint8_t control_byte, byte_citer first, byte_citer last
|
||||
) {
|
||||
using namespace decoders;
|
||||
@@ -194,7 +200,7 @@ private:
|
||||
auto code = control_code_e(control_byte & 0b11110000);
|
||||
|
||||
if (code == control_code_e::pingresp)
|
||||
return perform(wait_for, asio::transfer_at_least(0));
|
||||
return perform(asio::transfer_at_least(0));
|
||||
|
||||
bool is_reply = code != control_code_e::publish &&
|
||||
code != control_code_e::auth &&
|
||||
@@ -203,7 +209,7 @@ private:
|
||||
if (is_reply) {
|
||||
auto packet_id = decoders::decode_packet_id(first).value();
|
||||
_svc._replies.dispatch(error_code {}, code, packet_id, first, last);
|
||||
return perform(wait_for, asio::transfer_at_least(0));
|
||||
return perform(asio::transfer_at_least(0));
|
||||
}
|
||||
|
||||
complete(error_code {}, control_byte, first, last);
|
||||
|
||||
@@ -46,6 +46,10 @@ public:
|
||||
return _mqtt_context;
|
||||
}
|
||||
|
||||
const auto& mqtt_context() const {
|
||||
return _mqtt_context;
|
||||
}
|
||||
|
||||
auto& tls_context() {
|
||||
return _tls_context;
|
||||
}
|
||||
@@ -116,6 +120,10 @@ public:
|
||||
return _mqtt_context;
|
||||
}
|
||||
|
||||
const auto& mqtt_context() const {
|
||||
return _mqtt_context;
|
||||
}
|
||||
|
||||
auto& session_state() {
|
||||
return _mqtt_context.state;
|
||||
}
|
||||
@@ -283,6 +291,16 @@ public:
|
||||
);
|
||||
}
|
||||
|
||||
uint16_t negotiated_keep_alive() const {
|
||||
return connack_property(prop::server_keep_alive)
|
||||
.value_or(_stream_context.mqtt_context().keep_alive);
|
||||
}
|
||||
|
||||
void keep_alive(uint16_t seconds) {
|
||||
if (!is_open())
|
||||
_stream_context.mqtt_context().keep_alive = seconds;
|
||||
}
|
||||
|
||||
template <typename Prop>
|
||||
const auto& connect_property(Prop p) const {
|
||||
return _stream_context.connect_property(p);
|
||||
@@ -382,21 +400,21 @@ public:
|
||||
}
|
||||
|
||||
template <typename CompletionToken>
|
||||
decltype(auto) async_assemble(duration wait_for, CompletionToken&& token) {
|
||||
decltype(auto) async_assemble(CompletionToken&& token) {
|
||||
using Signature = void (error_code, uint8_t, byte_citer, byte_citer);
|
||||
|
||||
auto initiation = [] (
|
||||
auto handler, self_type& self,
|
||||
duration wait_for, std::string& read_buff, data_span& active_span
|
||||
std::string& read_buff, data_span& active_span
|
||||
) {
|
||||
assemble_op {
|
||||
self, std::move(handler), read_buff, active_span
|
||||
}.perform(wait_for, asio::transfer_at_least(0));
|
||||
}.perform(asio::transfer_at_least(0));
|
||||
};
|
||||
|
||||
return asio::async_initiate<CompletionToken, Signature> (
|
||||
initiation, token, std::ref(*this),
|
||||
wait_for, std::ref(_read_buff), std::ref(_active_span)
|
||||
std::ref(_read_buff), std::ref(_active_span)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -429,6 +447,8 @@ public:
|
||||
session_state.subscriptions_present(false);
|
||||
}
|
||||
}
|
||||
|
||||
_cancel_ping.emit(asio::cancellation_type::total);
|
||||
}
|
||||
|
||||
bool channel_store(decoders::publish_message message) {
|
||||
|
||||
@@ -196,7 +196,7 @@ public:
|
||||
encoders::encode_connect,
|
||||
_ctx.creds.client_id,
|
||||
_ctx.creds.username, _ctx.creds.password,
|
||||
uint16_t(10), false, _ctx.co_props, _ctx.will_msg
|
||||
_ctx.keep_alive, false, _ctx.co_props, _ctx.will_msg
|
||||
);
|
||||
|
||||
auto wire_data = packet.wire_data();
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#ifndef ASYNC_MQTT5_PING_OP_HPP
|
||||
#define ASYNC_MQTT5_PING_OP_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
|
||||
#include <boost/asio/cancellation_state.hpp>
|
||||
#include <boost/asio/consign.hpp>
|
||||
#include <boost/asio/prepend.hpp>
|
||||
#include <boost/asio/recycling_allocator.hpp>
|
||||
@@ -24,15 +26,19 @@ class ping_op {
|
||||
struct on_timer {};
|
||||
struct on_pingreq {};
|
||||
|
||||
static constexpr auto ping_interval = std::chrono::seconds(5);
|
||||
|
||||
std::shared_ptr<client_service> _svc_ptr;
|
||||
std::unique_ptr<asio::steady_timer> _ping_timer;
|
||||
asio::cancellation_state _cancellation_state;
|
||||
|
||||
public:
|
||||
ping_op(const std::shared_ptr<client_service>& svc_ptr) :
|
||||
_svc_ptr(svc_ptr),
|
||||
_ping_timer(new asio::steady_timer(svc_ptr->get_executor()))
|
||||
_ping_timer(new asio::steady_timer(svc_ptr->get_executor())),
|
||||
_cancellation_state(
|
||||
svc_ptr->_cancel_ping.slot(),
|
||||
asio::enable_total_cancellation {},
|
||||
asio::enable_total_cancellation {}
|
||||
)
|
||||
{}
|
||||
|
||||
ping_op(ping_op&&) noexcept = default;
|
||||
@@ -50,21 +56,28 @@ public:
|
||||
|
||||
using cancellation_slot_type = asio::cancellation_slot;
|
||||
asio::cancellation_slot get_cancellation_slot() const noexcept {
|
||||
return _svc_ptr->_cancel_ping.slot();
|
||||
return _cancellation_state.slot();
|
||||
}
|
||||
|
||||
void perform(duration from_now) {
|
||||
_ping_timer->expires_from_now(from_now);
|
||||
void perform() {
|
||||
_ping_timer->expires_from_now(compute_wait_time());
|
||||
_ping_timer->async_wait(
|
||||
asio::prepend(std::move(*this), on_timer {})
|
||||
);
|
||||
}
|
||||
|
||||
void operator()(on_timer, error_code ec) {
|
||||
void operator()(on_timer, error_code) {
|
||||
get_cancellation_slot().clear();
|
||||
|
||||
if (ec == asio::error::operation_aborted || !_svc_ptr->is_open())
|
||||
if (
|
||||
_cancellation_state.cancelled() == asio::cancellation_type::terminal ||
|
||||
!_svc_ptr->is_open()
|
||||
)
|
||||
return;
|
||||
else if (_cancellation_state.cancelled() == asio::cancellation_type::total) {
|
||||
_cancellation_state.clear();
|
||||
return perform();
|
||||
}
|
||||
|
||||
auto pingreq = control_packet<allocator_type>::of(
|
||||
no_pid, get_allocator(), encoders::encode_pingreq
|
||||
@@ -81,11 +94,21 @@ public:
|
||||
);
|
||||
}
|
||||
|
||||
void operator()(on_pingreq, error_code ec) {
|
||||
void operator()(on_pingreq, error_code) {
|
||||
get_cancellation_slot().clear();
|
||||
|
||||
if (!ec || ec == asio::error::try_again)
|
||||
perform(ping_interval - std::chrono::seconds(1));
|
||||
if (_cancellation_state.cancelled() == asio::cancellation_type::terminal)
|
||||
return;
|
||||
|
||||
perform();
|
||||
}
|
||||
|
||||
private:
|
||||
duration compute_wait_time() const {
|
||||
auto negotiated_ka = _svc_ptr->negotiated_keep_alive();
|
||||
return negotiated_ka ?
|
||||
std::chrono::seconds(negotiated_ka) :
|
||||
duration(std::numeric_limits<duration::rep>::max());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -49,7 +49,6 @@ public:
|
||||
|
||||
void perform() {
|
||||
_svc_ptr->async_assemble(
|
||||
std::chrono::seconds(20),
|
||||
asio::prepend(std::move(*this), on_message {})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -43,8 +43,6 @@ private:
|
||||
using stream_type = StreamType;
|
||||
using tls_context_type = TlsContext;
|
||||
|
||||
static constexpr auto read_timeout = std::chrono::seconds(5);
|
||||
|
||||
using client_service_type = detail::client_service<
|
||||
stream_type, tls_context_type
|
||||
>;
|
||||
@@ -183,8 +181,7 @@ public:
|
||||
auto initiation = [] (auto handler, const clisvc_ptr& svc_ptr) {
|
||||
svc_ptr->run(std::move(handler));
|
||||
|
||||
detail::ping_op { svc_ptr }
|
||||
.perform(read_timeout - std::chrono::seconds(1));
|
||||
detail::ping_op { svc_ptr }.perform();
|
||||
detail::read_message_op { svc_ptr }.perform();
|
||||
detail::sentry_op { svc_ptr }.perform();
|
||||
};
|
||||
@@ -301,6 +298,30 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Assign the maximum time interval that is permitted to elapse between
|
||||
* two transmissions from the Client.
|
||||
*
|
||||
* \details A non-zero value initiates a process of sending a \__PINGREQ\__
|
||||
* packet every `seconds`. If this function is not invoked, the Client assumes
|
||||
* a \__KEEP_ALIVE\__ interval of 60 seconds.
|
||||
*
|
||||
* \param seconds Time interval in seconds.
|
||||
*
|
||||
* \note If the Server sends a \__SERVER_KEEP_ALIVE\__,
|
||||
* the Client will send a \__PINGREQ\__ packet every \__SERVER_KEEP_ALIVE\__ seconds.
|
||||
*
|
||||
* \attention This function takes action when the client is in a non-operational state,
|
||||
* meaning the \ref async_run function has not been invoked.
|
||||
* Furthermore, you can use this function after the \ref cancel function has been called,
|
||||
* before the \ref async_run function is invoked again.
|
||||
*
|
||||
*/
|
||||
mqtt_client& keep_alive(uint16_t seconds) {
|
||||
_svc_ptr->keep_alive(seconds);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Assign \__CONNECT_PROPS\__ that will be sent in a \__CONNECT\__ packet.
|
||||
* \param props \__CONNECT_PROPS\__ sent in a \__CONNECT\__ packet.
|
||||
|
||||
Reference in new issue
Block a user