Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tcp socket from std stream #3838

Merged
merged 6 commits into from Jun 11, 2021
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions tokio/src/net/tcp/socket.rs
Expand Up @@ -482,6 +482,47 @@ impl TcpSocket {
let mio = self.inner.listen(backlog)?;
TcpListener::new(mio)
}

/// Converts a `socket2::Socket` into a `TcpSocket`. The socket2 socket
/// needs to be in a disconnected state when passed as an argument.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Converts a `socket2::Socket` into a `TcpSocket`. The socket2 socket
/// needs to be in a disconnected state when passed as an argument.
/// Converts a [`std::net::TcpStream`] into a `TcpSocket`. The provided
/// socket must not have been connected prior to calling this function. This
/// function is typically used together with crates such as [`socket2`] to
/// configure socket options that are not available on `TcpSocket`.
///
/// [`std::net::TcpStream`]: struct@std::net::TcpStream
/// [`socket2`]: https://docs.rs/socket2/

///
/// # Examples
///
/// ```
/// use tokio::net::TcpSocket;
/// use socket2::{Domain, Socket, Type};
/// use std::net::TcpStream;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your example doesn't compile.

Suggested change
/// async fn main() -> io::Result<()> {
/// async fn main() -> std::io::Result<()> {

///
/// let socket2_socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;
///
/// let stream = TcpStream::from(socket2_socket);
///
/// let socket = TcpSocket::from_std_stream(stream)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer simplifying it a bit to this.

Suggested change
/// let stream = TcpStream::from(socket2_socket);
///
/// let socket = TcpSocket::from_std_stream(stream)?;
/// let socket = TcpSocket::from_std_stream(socket2_socket.into())?;

/// # drop(socket);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't do anything.

Suggested change
/// # drop(socket);

///
/// Ok(())
/// }
/// ```
pub fn from_std_stream(std_stream: std::net::TcpStream) -> TcpSocket {
#[cfg(unix)]
{
use std::os::unix::io::{FromRawFd, IntoRawFd};

let raw_fd = std_stream.into_raw_fd();
unsafe { TcpSocket::from_raw_fd(raw_fd) }
}

#[cfg(windows)]
{
use std::os::windows::io::{FromRawSocket, IntoRawSocket};

let raw_socket = std_stream.into_raw_socket();
unsafe { TcpSocket::from_raw_socket(raw_socket) }
}
}
}

impl fmt::Debug for TcpSocket {
Expand Down