diff --git a/compio-net/src/socket.rs b/compio-net/src/socket.rs index d7bc5a8d..36c7f53f 100644 --- a/compio-net/src/socket.rs +++ b/compio-net/src/socket.rs @@ -24,6 +24,42 @@ pub struct Socket { impl Socket { pub fn from_socket2(socket: Socket2) -> io::Result { + #[cfg(unix)] + { + #[cfg(not(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "fuchsia", + target_os = "hurd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "openbsd", + target_os = "espidf", + target_os = "vita", + )))] + socket.set_cloexec(true)?; + #[cfg(any( + target_os = "ios", + target_os = "macos", + target_os = "tvos", + target_os = "watchos", + ))] + socket.set_nosigpipe(true)?; + // On Linux we use blocking socket + // Newer kernels have the patch that allows to arm io_uring poll mechanism for + // non blocking socket when there is no connections in listen queue + // + // https://patchwork.kernel.org/project/linux-block/patch/f999615b-205c-49b7-b272-c4e42e45e09d@kernel.dk/#22949861 + if cfg!(all( + unix, + not(all(target_os = "linux", feature = "io-uring")) + )) { + socket.set_nonblocking(true)?; + } + } + Ok(Self { socket: Attacher::new(socket)?, }) @@ -83,38 +119,7 @@ impl Socket { ); let BufResult(res, _) = compio_runtime::submit(op).await; let socket = unsafe { Socket2::from_raw_fd(res? as _) }; - #[cfg(not(any( - target_os = "android", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "fuchsia", - target_os = "hurd", - target_os = "illumos", - target_os = "linux", - target_os = "netbsd", - target_os = "openbsd", - target_os = "espidf", - target_os = "vita", - )))] - socket.set_cloexec(true)?; - #[cfg(any( - target_os = "ios", - target_os = "macos", - target_os = "tvos", - target_os = "watchos", - ))] - socket.set_nosigpipe(true)?; - // On Linux we use blocking socket - // Newer kernels have the patch that allows to arm io_uring poll mechanism for - // non blocking socket when there is no connections in listen queue - // - // https://patchwork.kernel.org/project/linux-block/patch/f999615b-205c-49b7-b272-c4e42e45e09d@kernel.dk/#22949861 - if cfg!(all( - unix, - not(all(target_os = "linux", feature = "io-uring")) - )) { - socket.set_nonblocking(true)?; - } + Self::from_socket2(socket) } diff --git a/compio-net/src/tcp.rs b/compio-net/src/tcp.rs index 782591df..e2f9f237 100644 --- a/compio-net/src/tcp.rs +++ b/compio-net/src/tcp.rs @@ -167,6 +167,13 @@ impl TcpStream { .await } + /// Creates new TcpStream from a std::net::TcpStream. + pub fn from_std(stream: std::net::TcpStream) -> io::Result { + Ok(Self { + inner: Socket::from_socket2(Socket2::from(stream))?, + }) + } + /// Close the socket. If the returned future is dropped before polling, the /// socket won't be closed. pub fn close(self) -> impl Future> { diff --git a/compio-net/src/unix.rs b/compio-net/src/unix.rs index e4ca0f61..f99e7dcc 100644 --- a/compio-net/src/unix.rs +++ b/compio-net/src/unix.rs @@ -147,6 +147,14 @@ impl UnixStream { Ok(unix_stream) } + #[cfg(unix)] + /// Creates new UnixStream from a std::os::unix::net::UnixStream. + pub fn from_std(stream: std::os::unix::net::UnixStream) -> io::Result { + Ok(Self { + inner: Socket::from_socket2(Socket2::from(stream))?, + }) + } + /// Close the socket. If the returned future is dropped before polling, the /// socket won't be closed. pub fn close(self) -> impl Future> {