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:
Korina Šimičević
2024-02-01 07:53:48 +01:00
parent d6c4884d53
commit 33c8eea890
21 changed files with 361 additions and 100 deletions

View File

@@ -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) {