@@ -77,6 +77,7 @@ use std::os::windows::io::{AsRawSocket, RawSocket};
77
77
use futures_lite:: io:: { AsyncRead , AsyncWrite } ;
78
78
use futures_lite:: stream:: { self , Stream } ;
79
79
use futures_lite:: { future, pin, ready} ;
80
+ use socket2:: { Domain , Protocol , SockAddr , Socket , Type } ;
80
81
81
82
use crate :: reactor:: { Reactor , Source } ;
82
83
@@ -1190,7 +1191,10 @@ impl Async<TcpStream> {
1190
1191
/// ```
1191
1192
pub async fn connect < A : Into < SocketAddr > > ( addr : A ) -> io:: Result < Async < TcpStream > > {
1192
1193
// Begin async connect.
1193
- let stream = Async :: new ( nb_connect:: tcp ( addr) ?) ?;
1194
+ let addr = addr. into ( ) ;
1195
+ let domain = Domain :: for_address ( addr) ;
1196
+ let socket = connect ( addr. into ( ) , domain, Some ( Protocol :: TCP ) ) ?;
1197
+ let stream = Async :: new ( TcpStream :: from ( socket) ) ?;
1194
1198
1195
1199
// The stream becomes writable when connected.
1196
1200
stream. writable ( ) . await ?;
@@ -1518,7 +1522,8 @@ impl Async<UnixStream> {
1518
1522
/// ```
1519
1523
pub async fn connect < P : AsRef < Path > > ( path : P ) -> io:: Result < Async < UnixStream > > {
1520
1524
// Begin async connect.
1521
- let stream = Async :: new ( nb_connect:: unix ( path) ?) ?;
1525
+ let socket = connect ( SockAddr :: unix ( path) ?, Domain :: UNIX , None ) ?;
1526
+ let stream = Async :: new ( UnixStream :: from ( socket) ) ?;
1522
1527
1523
1528
// The stream becomes writable when connected.
1524
1529
stream. writable ( ) . await ?;
@@ -1726,3 +1731,41 @@ async fn optimistic(fut: impl Future<Output = io::Result<()>>) -> io::Result<()>
1726
1731
} )
1727
1732
. await
1728
1733
}
1734
+
1735
+ fn connect ( addr : SockAddr , domain : Domain , protocol : Option < Protocol > ) -> io:: Result < Socket > {
1736
+ let sock_type = Type :: STREAM ;
1737
+ #[ cfg( any(
1738
+ target_os = "android" ,
1739
+ target_os = "dragonfly" ,
1740
+ target_os = "freebsd" ,
1741
+ target_os = "fuchsia" ,
1742
+ target_os = "illumos" ,
1743
+ target_os = "linux" ,
1744
+ target_os = "netbsd" ,
1745
+ target_os = "openbsd"
1746
+ ) ) ]
1747
+ // If we can, set nonblocking at socket creation for unix
1748
+ let sock_type = sock_type. nonblocking ( ) ;
1749
+ // This automatically handles cloexec on unix, no_inherit on windows and nosigpipe on macos
1750
+ let socket = Socket :: new ( domain, sock_type, protocol) ?;
1751
+ #[ cfg( not( any(
1752
+ target_os = "android" ,
1753
+ target_os = "dragonfly" ,
1754
+ target_os = "freebsd" ,
1755
+ target_os = "fuchsia" ,
1756
+ target_os = "illumos" ,
1757
+ target_os = "linux" ,
1758
+ target_os = "netbsd" ,
1759
+ target_os = "openbsd"
1760
+ ) ) ) ]
1761
+ // If the current platform doesn't support nonblocking at creation, enable it after creation
1762
+ socket. set_nonblocking ( true ) ?;
1763
+ match socket. connect ( & addr) {
1764
+ Ok ( _) => { }
1765
+ #[ cfg( unix) ]
1766
+ Err ( err) if err. raw_os_error ( ) == Some ( libc:: EINPROGRESS ) => { }
1767
+ Err ( err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => { }
1768
+ Err ( err) => return Err ( err) ,
1769
+ }
1770
+ Ok ( socket)
1771
+ }
0 commit comments