Skip to content

Commit 00e42a4

Browse files
authored
Merge pull request async-rs#55 from smol-rs/socket2
Replace nb-connect with socket2
2 parents f24ec5e + 0178bea commit 00e42a4

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ concurrent-queue = "1.2.2"
1616
fastrand = "1.3.5"
1717
futures-lite = "1.11.0"
1818
log = "0.4.11"
19-
nb-connect = "1.0.0"
2019
once_cell = "1.4.1"
2120
parking = "2.0.0"
2221
polling = "2.0.0"
22+
socket2 = { version = "0.4.0", features = ["all"] }
2323
vec-arena = "1.0.0"
2424
waker-fn = "1.1.0"
2525

src/lib.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ use std::os::windows::io::{AsRawSocket, RawSocket};
7777
use futures_lite::io::{AsyncRead, AsyncWrite};
7878
use futures_lite::stream::{self, Stream};
7979
use futures_lite::{future, pin, ready};
80+
use socket2::{Domain, Protocol, SockAddr, Socket, Type};
8081

8182
use crate::reactor::{Reactor, Source};
8283

@@ -1190,7 +1191,10 @@ impl Async<TcpStream> {
11901191
/// ```
11911192
pub async fn connect<A: Into<SocketAddr>>(addr: A) -> io::Result<Async<TcpStream>> {
11921193
// 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))?;
11941198

11951199
// The stream becomes writable when connected.
11961200
stream.writable().await?;
@@ -1518,7 +1522,8 @@ impl Async<UnixStream> {
15181522
/// ```
15191523
pub async fn connect<P: AsRef<Path>>(path: P) -> io::Result<Async<UnixStream>> {
15201524
// 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))?;
15221527

15231528
// The stream becomes writable when connected.
15241529
stream.writable().await?;
@@ -1726,3 +1731,41 @@ async fn optimistic(fut: impl Future<Output = io::Result<()>>) -> io::Result<()>
17261731
})
17271732
.await
17281733
}
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

Comments
 (0)