@@ -85,24 +85,25 @@ impl AsyncTcpStream {
85
85
86
86
#[ cfg( feature = "async-std-runtime" ) ]
87
87
async fn try_connect ( address : & SocketAddr , connect_timeout : Duration ) -> Result < Self > {
88
- use async_std:: net:: TcpStream ;
89
- use socket2:: { Domain , Protocol , SockAddr , Socket , Type } ;
88
+ use std:: convert:: TryInto ;
90
89
91
- let domain = Domain :: for_address ( * address) ;
92
- let socket = Socket :: new ( domain, Type :: STREAM , Some ( Protocol :: TCP ) ) ?;
93
- let conf = socket2:: TcpKeepalive :: new ( ) . with_time ( KEEPALIVE_TIME ) ;
94
- socket. set_tcp_keepalive ( & conf) ?;
90
+ let stream_future = async_std:: net:: TcpStream :: connect ( address) ;
95
91
96
- let address: SockAddr = ( * address) . into ( ) ;
97
- if connect_timeout == Duration :: from_secs ( 0 ) {
98
- socket. connect ( & address) ?;
92
+ let stream = if connect_timeout. is_zero ( ) {
93
+ stream_future. await ?
99
94
} else {
100
- socket. connect_timeout ( & address, connect_timeout) ?;
101
- }
102
-
103
- let stream: TcpStream = std:: net:: TcpStream :: from ( socket) . into ( ) ;
95
+ runtime:: timeout ( connect_timeout, stream_future) . await ??
96
+ } ;
104
97
stream. set_nodelay ( true ) ?;
105
98
99
+ let std_stream: std:: net:: TcpStream = stream. try_into ( ) ?;
100
+
101
+ let socket = socket2:: Socket :: from ( std_stream) ;
102
+ let conf = socket2:: TcpKeepalive :: new ( ) . with_time ( KEEPALIVE_TIME ) ;
103
+ socket. set_tcp_keepalive ( & conf) ?;
104
+ let std_stream = std:: net:: TcpStream :: from ( socket) ;
105
+ let stream = async_std:: net:: TcpStream :: from ( std_stream) ;
106
+
106
107
Ok ( stream. into ( ) )
107
108
}
108
109
0 commit comments