12
12
#include < boost/asio/compose.hpp>
13
13
#include < boost/asio/connect.hpp>
14
14
#include < boost/asio/coroutine.hpp>
15
- #include < boost/asio/experimental/parallel_group.hpp>
16
15
#include < boost/asio/ip/tcp.hpp>
17
- #include < boost/asio/steady_timer .hpp>
16
+ #include < boost/asio/cancel_after .hpp>
18
17
#include < string>
19
18
#include < chrono>
20
19
@@ -30,65 +29,29 @@ struct connect_op {
30
29
31
30
template <class Self >
32
31
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= {})
37
34
{
38
35
BOOST_ASIO_CORO_REENTER (coro)
39
36
{
40
- ctor_->timer_ .expires_after (ctor_->timeout_ );
41
-
42
37
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)));
58
41
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;
72
43
73
- default : BOOST_ASSERT (false );
44
+ if (ec == asio::error::operation_aborted) {
45
+ ec == error::connect_timeout;
74
46
}
47
+
48
+ self.complete (ec);
75
49
}
76
50
}
77
51
};
78
52
79
- template <class Executor >
80
53
class connector {
81
54
public:
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
-
92
55
void set_config (config const & cfg)
93
56
{ timeout_ = cfg.connect_timeout ; }
94
57
@@ -102,28 +65,14 @@ class connector {
102
65
return asio::async_compose
103
66
< CompletionToken
104
67
, 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);
119
69
}
120
70
121
71
auto const & endpoint () const noexcept { return endpoint_;}
122
72
123
73
private:
124
74
template <class , class > friend struct connect_op ;
125
75
126
- timer_type timer_;
127
76
std::chrono::steady_clock::duration timeout_ = std::chrono::seconds{2 };
128
77
asio::ip::tcp::endpoint endpoint_;
129
78
};
0 commit comments