Skip to content

Commit f616fdc

Browse files
authored
Merge pull request #290 from fafhrd91/master
Add from_std methods
2 parents c6edc2c + b621d24 commit f616fdc

File tree

3 files changed

+52
-32
lines changed

3 files changed

+52
-32
lines changed

compio-net/src/socket.rs

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,42 @@ pub struct Socket {
2424

2525
impl Socket {
2626
pub fn from_socket2(socket: Socket2) -> io::Result<Self> {
27+
#[cfg(unix)]
28+
{
29+
#[cfg(not(any(
30+
target_os = "android",
31+
target_os = "dragonfly",
32+
target_os = "freebsd",
33+
target_os = "fuchsia",
34+
target_os = "hurd",
35+
target_os = "illumos",
36+
target_os = "linux",
37+
target_os = "netbsd",
38+
target_os = "openbsd",
39+
target_os = "espidf",
40+
target_os = "vita",
41+
)))]
42+
socket.set_cloexec(true)?;
43+
#[cfg(any(
44+
target_os = "ios",
45+
target_os = "macos",
46+
target_os = "tvos",
47+
target_os = "watchos",
48+
))]
49+
socket.set_nosigpipe(true)?;
50+
// On Linux we use blocking socket
51+
// Newer kernels have the patch that allows to arm io_uring poll mechanism for
52+
// non blocking socket when there is no connections in listen queue
53+
//
54+
// https://patchwork.kernel.org/project/linux-block/patch/[email protected]/#22949861
55+
if cfg!(all(
56+
unix,
57+
not(all(target_os = "linux", feature = "io-uring"))
58+
)) {
59+
socket.set_nonblocking(true)?;
60+
}
61+
}
62+
2763
Ok(Self {
2864
socket: Attacher::new(socket)?,
2965
})
@@ -83,38 +119,7 @@ impl Socket {
83119
);
84120
let BufResult(res, _) = compio_runtime::submit(op).await;
85121
let socket = unsafe { Socket2::from_raw_fd(res? as _) };
86-
#[cfg(not(any(
87-
target_os = "android",
88-
target_os = "dragonfly",
89-
target_os = "freebsd",
90-
target_os = "fuchsia",
91-
target_os = "hurd",
92-
target_os = "illumos",
93-
target_os = "linux",
94-
target_os = "netbsd",
95-
target_os = "openbsd",
96-
target_os = "espidf",
97-
target_os = "vita",
98-
)))]
99-
socket.set_cloexec(true)?;
100-
#[cfg(any(
101-
target_os = "ios",
102-
target_os = "macos",
103-
target_os = "tvos",
104-
target_os = "watchos",
105-
))]
106-
socket.set_nosigpipe(true)?;
107-
// On Linux we use blocking socket
108-
// Newer kernels have the patch that allows to arm io_uring poll mechanism for
109-
// non blocking socket when there is no connections in listen queue
110-
//
111-
// https://patchwork.kernel.org/project/linux-block/patch/[email protected]/#22949861
112-
if cfg!(all(
113-
unix,
114-
not(all(target_os = "linux", feature = "io-uring"))
115-
)) {
116-
socket.set_nonblocking(true)?;
117-
}
122+
118123
Self::from_socket2(socket)
119124
}
120125

compio-net/src/tcp.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ impl TcpStream {
167167
.await
168168
}
169169

170+
/// Creates new TcpStream from a std::net::TcpStream.
171+
pub fn from_std(stream: std::net::TcpStream) -> io::Result<Self> {
172+
Ok(Self {
173+
inner: Socket::from_socket2(Socket2::from(stream))?,
174+
})
175+
}
176+
170177
/// Close the socket. If the returned future is dropped before polling, the
171178
/// socket won't be closed.
172179
pub fn close(self) -> impl Future<Output = io::Result<()>> {

compio-net/src/unix.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ impl UnixStream {
147147
Ok(unix_stream)
148148
}
149149

150+
#[cfg(unix)]
151+
/// Creates new UnixStream from a std::os::unix::net::UnixStream.
152+
pub fn from_std(stream: std::os::unix::net::UnixStream) -> io::Result<Self> {
153+
Ok(Self {
154+
inner: Socket::from_socket2(Socket2::from(stream))?,
155+
})
156+
}
157+
150158
/// Close the socket. If the returned future is dropped before polling, the
151159
/// socket won't be closed.
152160
pub fn close(self) -> impl Future<Output = io::Result<()>> {

0 commit comments

Comments
 (0)