From 3d80f47010f450c49ae7df193e70a59780a955f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Korina=20=C5=A0imi=C4=8Devi=C4=87?= Date: Fri, 23 Feb 2024 10:43:57 +0100 Subject: [PATCH] Open and set socket options on the lowest layer before initiating connect_op Summary: related to T13767 Reviewers: ivica Reviewed By: ivica Subscribers: miljen, iljazovic Differential Revision: https://repo.mireo.local/D28111 --- .../async_mqtt5/impl/autoconnect_stream.hpp | 21 ++++++++++++------- include/async_mqtt5/impl/reconnect_op.hpp | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/include/async_mqtt5/impl/autoconnect_stream.hpp b/include/async_mqtt5/impl/autoconnect_stream.hpp index 4a92bac..061ed43 100644 --- a/include/async_mqtt5/impl/autoconnect_stream.hpp +++ b/include/async_mqtt5/impl/autoconnect_stream.hpp @@ -90,9 +90,7 @@ public: } void open() { - error_code ec; - lowest_layer(*_stream_ptr).open(asio::ip::tcp::v4(), ec); - lowest_layer(*_stream_ptr).set_option(asio::ip::tcp::no_delay(true), ec); + open_lowest_layer(_stream_ptr); } void cancel() { @@ -155,6 +153,14 @@ public: } private: + static void open_lowest_layer(stream_ptr sptr) { + error_code ec; + auto& layer = lowest_layer(*sptr); + layer.open(asio::ip::tcp::v4(), ec); + layer.set_option(asio::socket_base::reuse_address(true), ec); + layer.set_option(asio::ip::tcp::no_delay(true), ec); + } + stream_ptr construct_next_layer() const { stream_ptr sptr; if constexpr (has_tls_context) @@ -164,11 +170,12 @@ private: else sptr = std::make_shared(_stream_executor); - error_code ec; - lowest_layer(*sptr).set_option( - asio::socket_base::reuse_address(true), ec - ); + return sptr; + } + stream_ptr construct_and_open_next_layer() const { + auto sptr = construct_next_layer(); + open_lowest_layer(sptr); return sptr; } diff --git a/include/async_mqtt5/impl/reconnect_op.hpp b/include/async_mqtt5/impl/reconnect_op.hpp index ff4780a..d32e5bb 100644 --- a/include/async_mqtt5/impl/reconnect_op.hpp +++ b/include/async_mqtt5/impl/reconnect_op.hpp @@ -143,7 +143,7 @@ public: if (ec == asio::error::host_not_found) return complete(asio::error::no_recovery); - auto sptr = _owner.construct_next_layer(); + auto sptr = _owner.construct_and_open_next_layer(); if constexpr (has_tls_context) setup_tls_sni(