Skip to content

Commit

Permalink
Merge pull request #290 from fafhrd91/master
Browse files Browse the repository at this point in the history
Add from_std methods
  • Loading branch information
Berrysoft committed Aug 26, 2024
2 parents c6edc2c + b621d24 commit f616fdc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
69 changes: 37 additions & 32 deletions compio-net/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@ pub struct Socket {

impl Socket {
pub fn from_socket2(socket: Socket2) -> io::Result<Self> {
#[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)?,
})
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 7 additions & 0 deletions compio-net/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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<Output = io::Result<()>> {
Expand Down
8 changes: 8 additions & 0 deletions compio-net/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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<Output = io::Result<()>> {
Expand Down

0 comments on commit f616fdc

Please sign in to comment.