From 066588546b388a6f1b6dd5e68b263732b0e3437b Mon Sep 17 00:00:00 2001 From: Marcelo Zimbres Date: Sun, 13 Oct 2024 22:26:41 +0200 Subject: [PATCH] Simplifies the connect operations. --- include/boost/redis/detail/connector.hpp | 76 +++++------------------- include/boost/redis/detail/runner.hpp | 1 - 2 files changed, 14 insertions(+), 63 deletions(-) diff --git a/include/boost/redis/detail/connector.hpp b/include/boost/redis/detail/connector.hpp index bd516137..681856d5 100644 --- a/include/boost/redis/detail/connector.hpp +++ b/include/boost/redis/detail/connector.hpp @@ -12,9 +12,8 @@ #include #include #include -#include #include -#include +#include #include #include @@ -30,48 +29,20 @@ struct connect_op { template void operator()( Self& self - , std::array const& order = {} - , system::error_code const& ec1 = {} - , asio::ip::tcp::endpoint const& ep= {} - , system::error_code const& ec2 = {}) + , system::error_code const& ec = {} + , asio::ip::tcp::endpoint const& ep= {}) { BOOST_ASIO_CORO_REENTER (coro) { - ctor_->timer_.expires_after(ctor_->timeout_); - BOOST_ASIO_CORO_YIELD - asio::experimental::make_parallel_group( - [this](auto token) - { - auto f = [](system::error_code const&, auto const&) { return true; }; - return asio::async_connect(*stream, *res_, f, token); - }, - [this](auto token) { return ctor_->timer_.async_wait(token);} - ).async_wait( - asio::experimental::wait_for_one(), - std::move(self)); - - if (is_cancelled(self)) { - self.complete(asio::error::operation_aborted); - return; - } - - switch (order[0]) { - case 0: { - ctor_->endpoint_ = ep; - self.complete(ec1); - } break; - case 1: - { - if (ec2) { - self.complete(ec2); - } else { - self.complete(error::connect_timeout); - } - } break; - - default: BOOST_ASSERT(false); - } + asio::async_connect(*stream, *res_, + [](system::error_code const&, auto const&) { return true; }, + asio::cancel_after(ctor_->timeout_, std::move(self))); + + ctor_->endpoint_ = ep; + + // TODO: map operation_canceled into error::connect_timeout + self.complete(ec); } } }; @@ -79,15 +50,7 @@ struct connect_op { template class connector { public: - using timer_type = - asio::basic_waitable_timer< - std::chrono::steady_clock, - asio::wait_traits, - Executor>; - - connector(Executor ex) - : timer_{ex} - {} + connector(Executor) {} void set_config(config const& cfg) { timeout_ = cfg.connect_timeout; } @@ -102,28 +65,17 @@ class connector { return asio::async_compose < CompletionToken , void(system::error_code) - >(connect_op{this, &stream, &res}, token, timer_); + >(connect_op{this, &stream, &res}, token); } std::size_t cancel(operation op) - { - switch (op) { - case operation::connect: - case operation::all: - timer_.cancel(); - break; - default: /* ignore */; - } - - return 0; - } + { return 0; } auto const& endpoint() const noexcept { return endpoint_;} private: template friend struct connect_op; - timer_type timer_; std::chrono::steady_clock::duration timeout_ = std::chrono::seconds{2}; asio::ip::tcp::endpoint endpoint_; }; diff --git a/include/boost/redis/detail/runner.hpp b/include/boost/redis/detail/runner.hpp index 90e51f44..5ffa542f 100644 --- a/include/boost/redis/detail/runner.hpp +++ b/include/boost/redis/detail/runner.hpp @@ -205,7 +205,6 @@ class runner { using connector_type = connector; using handshaker_type = detail::handshaker; using health_checker_type = health_checker; - using timer_type = typename connector_type::timer_type; template friend class runner_op; template friend struct hello_op;