forked from boostorg/mqtt5
async_disconnect will close the connection regardless of the current connection state
Summary: related to T13761 Reviewers: ivica Reviewed By: ivica Subscribers: miljen, iljazovic Differential Revision: https://repo.mireo.local/D28057
This commit is contained in:
1 parent
1c29b86030
commit
634a4bdfa9
11 files changed
+362
-71
No files matched your search
@@ -2,7 +2,10 @@
|
||||
#define ASYNC_MQTT5_DISCONNECT_OP_HPP
|
||||
|
||||
#include <boost/asio/consign.hpp>
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/prepend.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
#include <boost/asio/experimental/parallel_group.hpp>
|
||||
|
||||
#include <async_mqtt5/types.hpp>
|
||||
|
||||
@@ -45,7 +48,13 @@ public:
|
||||
_svc_ptr(svc_ptr),
|
||||
_context(std::move(context)),
|
||||
_handler(std::move(handler), _svc_ptr->get_executor())
|
||||
{}
|
||||
{
|
||||
auto slot = asio::get_associated_cancellation_slot(_handler);
|
||||
if (slot.is_connected())
|
||||
slot.assign([&svc = *_svc_ptr](asio::cancellation_type_t) {
|
||||
svc.cancel();
|
||||
});
|
||||
}
|
||||
|
||||
disconnect_op(disconnect_op&&) noexcept = default;
|
||||
disconnect_op(const disconnect_op&) = delete;
|
||||
@@ -146,10 +155,88 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ClientService, typename Handler>
|
||||
class terminal_disconnect_op {
|
||||
using client_service = ClientService;
|
||||
|
||||
static constexpr uint8_t seconds = 5;
|
||||
|
||||
std::shared_ptr<client_service> _svc_ptr;
|
||||
std::unique_ptr<asio::steady_timer> _timer;
|
||||
|
||||
using handler_type = cancellable_handler<
|
||||
Handler, typename ClientService::executor_type
|
||||
>;
|
||||
handler_type _handler;
|
||||
|
||||
public:
|
||||
terminal_disconnect_op(
|
||||
const std::shared_ptr<client_service>& svc_ptr,
|
||||
Handler&& handler
|
||||
) :
|
||||
_svc_ptr(svc_ptr),
|
||||
_timer(new asio::steady_timer(_svc_ptr->get_executor())),
|
||||
_handler(std::move(handler), _svc_ptr->get_executor())
|
||||
{}
|
||||
|
||||
terminal_disconnect_op(terminal_disconnect_op&&) noexcept = default;
|
||||
terminal_disconnect_op(const terminal_disconnect_op&) = delete;
|
||||
|
||||
using executor_type = asio::associated_executor_t<handler_type>;
|
||||
executor_type get_executor() const noexcept {
|
||||
return asio::get_associated_executor(_handler);
|
||||
}
|
||||
|
||||
using allocator_type = asio::associated_allocator_t<handler_type>;
|
||||
allocator_type get_allocator() const noexcept {
|
||||
return asio::get_associated_allocator(_handler);
|
||||
}
|
||||
|
||||
using cancellation_slot_type = asio::associated_cancellation_slot_t<handler_type>;
|
||||
cancellation_slot_type get_cancellation_slot() const noexcept {
|
||||
return asio::get_associated_cancellation_slot(_handler);
|
||||
}
|
||||
|
||||
template <typename DisconnectContext>
|
||||
void perform(DisconnectContext&& context) {
|
||||
namespace asioex = boost::asio::experimental;
|
||||
|
||||
auto init_disconnect = [](
|
||||
auto handler, disconnect_ctx ctx,
|
||||
const std::shared_ptr<ClientService>& svc_ptr
|
||||
) {
|
||||
disconnect_op {
|
||||
svc_ptr, std::move(ctx), std::move(handler)
|
||||
}.perform();
|
||||
};
|
||||
|
||||
_timer->expires_after(std::chrono::seconds(seconds));
|
||||
|
||||
auto timed_disconnect = asioex::make_parallel_group(
|
||||
asio::async_initiate<const asio::deferred_t, void (error_code)>(
|
||||
init_disconnect, asio::deferred,
|
||||
std::forward<DisconnectContext>(context), _svc_ptr
|
||||
),
|
||||
_timer->async_wait(asio::deferred)
|
||||
);
|
||||
|
||||
timed_disconnect.async_wait(
|
||||
asioex::wait_for_one(), asio::prepend(std::move(*this))
|
||||
);
|
||||
}
|
||||
|
||||
void operator()(
|
||||
std::array<std::size_t, 2> /* ord */,
|
||||
error_code disconnect_ec, error_code /* timer_ec */
|
||||
) {
|
||||
_handler.complete(disconnect_ec);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ClientService, typename CompletionToken>
|
||||
decltype(auto) async_disconnect(
|
||||
disconnect_rc_e reason_code, const disconnect_props& props,
|
||||
bool terminal, const std::shared_ptr<ClientService>& svc_ptr,
|
||||
const std::shared_ptr<ClientService>& svc_ptr,
|
||||
CompletionToken&& token
|
||||
) {
|
||||
using Signature = void (error_code);
|
||||
@@ -165,11 +252,34 @@ decltype(auto) async_disconnect(
|
||||
|
||||
return asio::async_initiate<CompletionToken, Signature>(
|
||||
initiation, token,
|
||||
disconnect_ctx { reason_code, props, terminal },
|
||||
disconnect_ctx { reason_code, props, false },
|
||||
svc_ptr
|
||||
);
|
||||
}
|
||||
|
||||
template <typename ClientService, typename CompletionToken>
|
||||
decltype(auto) async_terminal_disconnect(
|
||||
disconnect_rc_e reason_code, const disconnect_props& props,
|
||||
const std::shared_ptr<ClientService>& svc_ptr,
|
||||
CompletionToken&& token
|
||||
) {
|
||||
using Signature = void (error_code);
|
||||
|
||||
auto initiation = [](
|
||||
auto handler, disconnect_ctx ctx,
|
||||
const std::shared_ptr<ClientService>& svc_ptr
|
||||
) {
|
||||
terminal_disconnect_op {
|
||||
svc_ptr, std::move(handler)
|
||||
}.perform(std::move(ctx));
|
||||
};
|
||||
|
||||
return asio::async_initiate<CompletionToken, Signature>(
|
||||
initiation, token,
|
||||
disconnect_ctx { reason_code, props, true },
|
||||
svc_ptr
|
||||
);
|
||||
}
|
||||
|
||||
} // end namespace async_mqtt5::detail
|
||||
|
||||
|
||||
@@ -191,7 +191,7 @@ private:
|
||||
props[prop::reason_string] = reason;
|
||||
return async_disconnect(
|
||||
disconnect_rc_e::malformed_packet, props,
|
||||
false, _svc_ptr, asio::detached
|
||||
_svc_ptr, asio::detached
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -400,7 +400,7 @@ private:
|
||||
auto props = disconnect_props {};
|
||||
props[prop::reason_string] = reason;
|
||||
async_disconnect(
|
||||
disconnect_rc_e::malformed_packet, props, false, _svc_ptr,
|
||||
disconnect_rc_e::malformed_packet, props, _svc_ptr,
|
||||
asio::detached
|
||||
);
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ private:
|
||||
auto props = disconnect_props {};
|
||||
props[prop::reason_string] = std::move(message);
|
||||
|
||||
async_disconnect(reason, props, false, _svc_ptr, asio::detached);
|
||||
async_disconnect(reason, props, _svc_ptr, asio::detached);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -127,7 +127,7 @@ private:
|
||||
auto svc_ptr = _svc_ptr; // copy before this is moved
|
||||
|
||||
async_disconnect(
|
||||
disconnect_rc_e::malformed_packet, props, false, svc_ptr,
|
||||
disconnect_rc_e::malformed_packet, props, svc_ptr,
|
||||
asio::prepend(std::move(*this), on_disconnect {})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public:
|
||||
props[prop::reason_string] = "No reply received within 20 seconds";
|
||||
auto svc_ptr = _svc_ptr;
|
||||
return async_disconnect(
|
||||
disconnect_rc_e::unspecified_error, props, false, svc_ptr,
|
||||
disconnect_rc_e::unspecified_error, props, svc_ptr,
|
||||
asio::prepend(std::move(*this), on_disconnect {})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -255,7 +255,7 @@ private:
|
||||
auto props = disconnect_props {};
|
||||
props[prop::reason_string] = reason;
|
||||
async_disconnect(
|
||||
disconnect_rc_e::malformed_packet, props, false, _svc_ptr,
|
||||
disconnect_rc_e::malformed_packet, props, _svc_ptr,
|
||||
asio::detached
|
||||
);
|
||||
}
|
||||
|
||||
@@ -200,7 +200,7 @@ private:
|
||||
auto props = disconnect_props {};
|
||||
props[prop::reason_string] = reason;
|
||||
async_disconnect(
|
||||
disconnect_rc_e::malformed_packet, props, false, _svc_ptr,
|
||||
disconnect_rc_e::malformed_packet, props, _svc_ptr,
|
||||
asio::detached
|
||||
);
|
||||
}
|
||||
|
||||
@@ -806,16 +806,14 @@ public:
|
||||
* \brief Disconnect the Client by sending a \__DISCONNECT\__ packet
|
||||
* with a specified Reason Code. This function has terminal effects.
|
||||
*
|
||||
* \details Send a \__DISCONNECT\__ packet to the Broker with a Reason Code
|
||||
* describing the reason for disconnection.
|
||||
* \details The Client will attempt to send a \__DISCONNECT\__ packet to the Broker
|
||||
* with a Reason Code describing the reason for disconnection.
|
||||
* If the \__DISCONNECT\__ packet is successfully transmitted,
|
||||
* or if `5 seconds` elapsed without a successful send, the Client will terminate the connection.
|
||||
*
|
||||
* \attention This function has terminal effects and will close the Client.
|
||||
* See \ref mqtt_client::cancel.
|
||||
*
|
||||
* \note If you wish to close the Client regardless of its state,
|
||||
* prefer calling the \ref cancel function instead. This function will only
|
||||
* take effect when the connection has been successfully established.
|
||||
*
|
||||
* \param reason_code Reason Code to notify
|
||||
* the Broker of the reason for disconnection.
|
||||
* \param props An instance of \__DISCONNECT_PROPS\__.
|
||||
@@ -841,7 +839,10 @@ public:
|
||||
* \par Error codes
|
||||
* The list of all possible error codes that this operation can finish with:\n
|
||||
* - `boost::system::errc::errc_t::success`\n
|
||||
* - `boost::asio::error::operation_aborted`\n
|
||||
* - `boost::asio::error::operation_aborted`[footnote
|
||||
This error code can appear if the Client failed to send the \__DISCONNECT\__ packet to the Server.
|
||||
Regardless, the connection to the Server is terminated, and the Client is cancelled.
|
||||
]\n
|
||||
* - \link async_mqtt5::client::error::malformed_packet \endlink
|
||||
*
|
||||
* Refer to the section on \__ERROR_HANDLING\__ to find the underlying causes for each error code.
|
||||
@@ -853,10 +854,9 @@ public:
|
||||
) {
|
||||
auto impl = _impl;
|
||||
_impl = impl->dup();
|
||||
return detail::async_disconnect(
|
||||
return detail::async_terminal_disconnect(
|
||||
detail::disconnect_rc_e(static_cast<uint8_t>(reason_code)),
|
||||
props, true, impl,
|
||||
std::forward<CompletionToken>(token)
|
||||
props, impl, std::forward<CompletionToken>(token)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user