@@ -85,24 +85,25 @@ impl AsyncTcpStream {
8585
8686 #[ cfg( feature = "async-std-runtime" ) ]
8787 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 ;
9089
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) ;
9591
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 ?
9994 } 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+ } ;
10497 stream. set_nodelay ( true ) ?;
10598
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+
106107 Ok ( stream. into ( ) )
107108 }
108109
0 commit comments