From 12143833b89ab8e2cdaf637918bd7106b91e7ed9 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 21 Aug 2024 17:07:47 +0500 Subject: [PATCH 1/2] Add from_std methods --- compio-net/src/socket.rs | 69 +++++++++++++++++++++------------------- compio-net/src/tcp.rs | 5 +++ compio-net/src/unix.rs | 6 ++++ 3 files changed, 48 insertions(+), 32 deletions(-) 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..f72f9fc9 100644 --- a/compio-net/src/tcp.rs +++ b/compio-net/src/tcp.rs @@ -167,6 +167,11 @@ 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..361253fd 100644 --- a/compio-net/src/unix.rs +++ b/compio-net/src/unix.rs @@ -147,6 +147,12 @@ 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> { From b621d247595f925bf343dae5e679055f6d017b51 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 26 Aug 2024 17:07:42 +0500 Subject: [PATCH 2/2] fmt --- compio-net/src/tcp.rs | 4 +++- compio-net/src/unix.rs | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/compio-net/src/tcp.rs b/compio-net/src/tcp.rs index f72f9fc9..e2f9f237 100644 --- a/compio-net/src/tcp.rs +++ b/compio-net/src/tcp.rs @@ -169,7 +169,9 @@ impl TcpStream { /// 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))? }) + Ok(Self { + inner: Socket::from_socket2(Socket2::from(stream))?, + }) } /// Close the socket. If the returned future is dropped before polling, the diff --git a/compio-net/src/unix.rs b/compio-net/src/unix.rs index 361253fd..f99e7dcc 100644 --- a/compio-net/src/unix.rs +++ b/compio-net/src/unix.rs @@ -149,8 +149,10 @@ impl UnixStream { #[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))? }) + 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