1212#include < boost/asio/compose.hpp>
1313#include < boost/asio/connect.hpp>
1414#include < boost/asio/coroutine.hpp>
15- #include < boost/asio/experimental/parallel_group.hpp>
1615#include < boost/asio/ip/tcp.hpp>
17- #include < boost/asio/steady_timer .hpp>
16+ #include < boost/asio/cancel_after .hpp>
1817#include < string>
1918#include < chrono>
2019
@@ -30,65 +29,29 @@ struct connect_op {
3029
3130 template <class Self >
3231 void operator ()( Self& self
33- , std::array<std::size_t , 2 > const & order = {}
34- , system::error_code const & ec1 = {}
35- , asio::ip::tcp::endpoint const & ep= {}
36- , system::error_code const & ec2 = {})
32+ , system::error_code const & ec = {}
33+ , asio::ip::tcp::endpoint const & ep= {})
3734 {
3835 BOOST_ASIO_CORO_REENTER (coro)
3936 {
40- ctor_->timer_ .expires_after (ctor_->timeout_ );
41-
4237 BOOST_ASIO_CORO_YIELD
43- asio::experimental::make_parallel_group (
44- [this ](auto token)
45- {
46- auto f = [](system::error_code const &, auto const &) { return true ; };
47- return asio::async_connect (*stream, *res_, f, token);
48- },
49- [this ](auto token) { return ctor_->timer_ .async_wait (token);}
50- ).async_wait (
51- asio::experimental::wait_for_one (),
52- std::move (self));
53-
54- if (is_cancelled (self)) {
55- self.complete (asio::error::operation_aborted);
56- return ;
57- }
38+ asio::async_connect (*stream, *res_,
39+ [](system::error_code const &, auto const &) { return true ; },
40+ asio::cancel_after (ctor_->timeout_ , std::move (self)));
5841
59- switch (order[0 ]) {
60- case 0 : {
61- ctor_->endpoint_ = ep;
62- self.complete (ec1);
63- } break ;
64- case 1 :
65- {
66- if (ec2) {
67- self.complete (ec2);
68- } else {
69- self.complete (error::connect_timeout);
70- }
71- } break ;
42+ ctor_->endpoint_ = ep;
7243
73- default : BOOST_ASSERT (false );
44+ if (ec == asio::error::operation_aborted) {
45+ ec == error::connect_timeout;
7446 }
47+
48+ self.complete (ec);
7549 }
7650 }
7751};
7852
79- template <class Executor >
8053class connector {
8154public:
82- using timer_type =
83- asio::basic_waitable_timer<
84- std::chrono::steady_clock,
85- asio::wait_traits<std::chrono::steady_clock>,
86- Executor>;
87-
88- connector (Executor ex)
89- : timer_{ex}
90- {}
91-
9255 void set_config (config const & cfg)
9356 { timeout_ = cfg.connect_timeout ; }
9457
@@ -102,28 +65,14 @@ class connector {
10265 return asio::async_compose
10366 < CompletionToken
10467 , void (system::error_code)
105- >(connect_op<connector, Stream>{this , &stream, &res}, token, timer_);
106- }
107-
108- std::size_t cancel (operation op)
109- {
110- switch (op) {
111- case operation::connect:
112- case operation::all:
113- timer_.cancel ();
114- break ;
115- default : /* ignore */ ;
116- }
117-
118- return 0 ;
68+ >(connect_op<connector, Stream>{this , &stream, &res}, token);
11969 }
12070
12171 auto const & endpoint () const noexcept { return endpoint_;}
12272
12373private:
12474 template <class , class > friend struct connect_op ;
12575
126- timer_type timer_;
12776 std::chrono::steady_clock::duration timeout_ = std::chrono::seconds{2 };
12877 asio::ip::tcp::endpoint endpoint_;
12978};
0 commit comments