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

Add from_std methods to TCP/Unix Stream/Listener #1124

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ use crate::{event, sys, Interests, Registry, Token};

use std::fmt;
use std::io;
use std::net;
use std::net::SocketAddr;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};

/// A structure representing a socket server
///
Expand Down Expand Up @@ -41,6 +44,20 @@ pub struct TcpListener {
}

impl TcpListener {
/// Creates a new `TcpListener` from a standard `net::TcpListener`.
kleimkuhler marked this conversation as resolved.
Show resolved Hide resolved
///
/// This function is intended to be used to wrap a TCP listener from the
/// standard library in the Mio equivalent. The conversion assumes nothing
/// about the underlying listener.
kleimkuhler marked this conversation as resolved.
Show resolved Hide resolved
pub fn from_std(listener: net::TcpListener) -> Self {
kleimkuhler marked this conversation as resolved.
Show resolved Hide resolved
let sys = sys::TcpListener::from_std(listener);
Self {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Convenience method to bind a new TCP listener to the specified address
/// to receive new connections.
///
Expand Down Expand Up @@ -168,3 +185,28 @@ impl FromRawFd for TcpListener {
}
}
}

#[cfg(windows)]
impl AsRawSocket for TcpListener {
fn as_raw_socket(&self) -> RawSocket {
self.sys.as_raw_socket()
}
}

#[cfg(windows)]
impl FromRawSocket for TcpListener {
unsafe fn from_raw_socket(socket: RawSocket) -> TcpListener {
TcpListener {
sys: FromRawSocket::from_raw_socket(socket),
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}
}

#[cfg(windows)]
impl IntoRawSocket for TcpListener {
fn into_raw_socket(self) -> RawSocket {
self.sys.into_raw_socket()
}
}
46 changes: 46 additions & 0 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::fmt;
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
use std::net;
use std::net::SocketAddr;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};

#[cfg(debug_assertions)]
use crate::poll::SelectorId;
Expand Down Expand Up @@ -55,6 +58,24 @@ impl TcpStream {
}
}

/// Creates a new `TcpStream` from a standard `net::TcpStream`.
///
/// This function is intended to be used to wrap a TCP stream from the
/// standard library in the Mio equivalent. The conversion assumes nothing
/// about the underlying stream.
///
/// Note: The TCP stream here will not have `connect` called on it, so it
kleimkuhler marked this conversation as resolved.
Show resolved Hide resolved
/// should already be connected via some other means (be it manually, or
/// the standard library).
pub fn from_std(stream: net::TcpStream) -> Self {
let sys = sys::TcpStream::from_std(stream);
Self {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Create a new TCP stream and issue a non-blocking connect to the
/// specified address.
pub fn connect(addr: SocketAddr) -> io::Result<TcpStream> {
Expand Down Expand Up @@ -278,3 +299,28 @@ impl FromRawFd for TcpStream {
}
}
}

#[cfg(windows)]
impl AsRawSocket for TcpStream {
fn as_raw_socket(&self) -> RawSocket {
self.sys.as_raw_socket()
}
}

#[cfg(windows)]
impl FromRawSocket for TcpStream {
unsafe fn from_raw_socket(socket: RawSocket) -> TcpStream {
TcpStream {
sys: FromRawSocket::from_raw_socket(socket),
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}
}

#[cfg(windows)]
impl IntoRawSocket for TcpStream {
fn into_raw_socket(self) -> RawSocket {
self.sys.into_raw_socket()
}
}
42 changes: 42 additions & 0 deletions src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ use crate::{event, sys, Interests, Registry, Token};

use std::fmt;
use std::io;
use std::net;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};

/// A User Datagram Protocol socket.
///
Expand Down Expand Up @@ -92,6 +95,20 @@ pub struct UdpSocket {
}

impl UdpSocket {
/// Creates a new `UdpSocket` from a standard `net::UdpSocket`.
///
/// This function is intended to be used to wrap a UDP socket from the
/// standard library in the Mio equivalent. The conversion assumes nothing
/// about the underlying socket.
pub fn from_std(socket: net::UdpSocket) -> Self {
let sys = sys::UdpSocket::from_std(socket);
Self {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Creates a UDP socket from the given address.
///
/// # Examples
Expand Down Expand Up @@ -556,3 +573,28 @@ impl FromRawFd for UdpSocket {
}
}
}

#[cfg(windows)]
impl AsRawSocket for UdpSocket {
fn as_raw_socket(&self) -> RawSocket {
self.sys.as_raw_socket()
}
}

#[cfg(windows)]
impl FromRawSocket for UdpSocket {
unsafe fn from_raw_socket(socket: RawSocket) -> UdpSocket {
UdpSocket {
sys: FromRawSocket::from_raw_socket(socket),
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}
}

#[cfg(windows)]
impl IntoRawSocket for UdpSocket {
fn into_raw_socket(self) -> RawSocket {
self.sys.into_raw_socket()
}
}
15 changes: 15 additions & 0 deletions src/net/uds/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{sys, Interests, Registry, Token};
use std::io;
use std::net::Shutdown;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::net;
use std::path::Path;

/// A Unix datagram socket.
Expand All @@ -25,6 +26,20 @@ impl UnixDatagram {
}
}

/// Creates a new `UnixDatagram` from a standard `net::UnixDatagram`.
///
/// This function is intended to be used to wrap a Unix datagram from the
/// standard library in the Mio equivalent. The conversion assumes nothing
/// about the underlying datagram.
pub fn from_std(datagram: net::UnixDatagram) -> Self {
let sys = sys::UnixDatagram::from_std(datagram);
Self {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Creates a Unix datagram socket bound to the given path.
pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
let sys = sys::UnixDatagram::bind(path.as_ref())?;
Expand Down
15 changes: 15 additions & 0 deletions src/net/uds/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{sys, Interests, Registry, Token};

use std::io;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::net;
use std::path::Path;

/// A non-blocking Unix domain socket server.
Expand All @@ -26,6 +27,20 @@ impl UnixListener {
}
}

/// Creates a new `UnixListener` from a standard `net::UnixListener`.
///
/// This function is intended to be used to wrap a Unix listener from the
/// standard library in the Mio equivalent. The conversion assumes nothing
/// about the underlying listener.
pub fn from_std(listener: net::UnixListener) -> Self {
let sys = sys::UnixListener::from_std(listener);
Self {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Creates a new `UnixListener` bound to the specified socket.
pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
let sys = sys::UnixListener::bind(path.as_ref())?;
Expand Down
19 changes: 19 additions & 0 deletions src/net/uds/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{sys, Interests, Registry, Token};
use std::io::{self, IoSlice, IoSliceMut};
use std::net::Shutdown;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::net;
use std::path::Path;

/// A non-blocking Unix stream socket.
Expand All @@ -25,6 +26,24 @@ impl UnixStream {
}
}

/// Creates a new `UnixStream` from a standard `net::UnixStream`.
///
/// This function is intended to be used to wrap a Unix stream from the
/// standard library in the Mio equivalent. The conversion assumes nothing
/// about the underlying stream.
///
/// Note: The Unix stream here will not have `connect` called on it, so it
/// should already be connected via some other means (be it manually, or
/// the standard library).
pub fn from_std(stream: net::UnixStream) -> Self {
let sys = sys::UnixStream::from_std(stream);
Self {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Connects to the socket named by `path`.
pub fn connect<P: AsRef<Path>>(p: P) -> io::Result<UnixStream> {
let sys = sys::UnixStream::connect(p.as_ref())?;
Expand Down
6 changes: 6 additions & 0 deletions src/sys/unix/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub struct TcpListener {
}

impl TcpListener {
pub(crate) fn from_std(inner: net::TcpListener) -> Self {
kleimkuhler marked this conversation as resolved.
Show resolved Hide resolved
let raw_fd = inner.into_raw_fd();
kleimkuhler marked this conversation as resolved.
Show resolved Hide resolved
let inner = unsafe { FromRawFd::from_raw_fd(raw_fd) };
Self { inner }
}

pub fn bind(addr: SocketAddr) -> io::Result<TcpListener> {
new_ip_socket(addr, libc::SOCK_STREAM).and_then(|socket| {
// Set SO_REUSEADDR (mirrors what libstd does).
Expand Down
6 changes: 6 additions & 0 deletions src/sys/unix/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ impl TcpStream {
TcpStream { inner }
}

pub(crate) fn from_std(inner: net::TcpStream) -> Self {
let raw_fd = inner.into_raw_fd();
let inner = unsafe { FromRawFd::from_raw_fd(raw_fd) };
Self { inner }
}

pub fn connect(addr: SocketAddr) -> io::Result<TcpStream> {
new_ip_socket(addr, libc::SOCK_STREAM)
.and_then(|socket| {
Expand Down
6 changes: 6 additions & 0 deletions src/sys/unix/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ pub struct UdpSocket {
}

impl UdpSocket {
pub(crate) fn from_std(io: net::UdpSocket) -> Self {
let raw_fd = io.into_raw_fd();
let io = unsafe { FromRawFd::from_raw_fd(raw_fd) };
Self { io }
}

pub fn bind(addr: SocketAddr) -> io::Result<UdpSocket> {
// Gives a warning for non Apple platforms.
#[allow(clippy::let_and_return)]
Expand Down
6 changes: 6 additions & 0 deletions src/sys/unix/uds/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ impl UnixDatagram {
UnixDatagram { inner }
}

pub(crate) fn from_std(inner: net::UnixDatagram) -> Self {
let raw_fd = inner.into_raw_fd();
let inner = unsafe { FromRawFd::from_raw_fd(raw_fd) };
UnixDatagram { inner }
}

pub(crate) fn bind(path: &Path) -> io::Result<UnixDatagram> {
let socket = new_socket(libc::AF_UNIX, libc::SOCK_DGRAM)?;
let (sockaddr, socklen) = socket_addr(path)?;
Expand Down
6 changes: 6 additions & 0 deletions src/sys/unix/uds/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ impl UnixListener {
UnixListener { inner }
}

pub(crate) fn from_std(inner: net::UnixListener) -> Self {
let raw_fd = inner.into_raw_fd();
let inner = unsafe { FromRawFd::from_raw_fd(raw_fd) };
Self { inner }
}

pub(crate) fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
let sockaddr = mem::MaybeUninit::<libc::sockaddr_un>::zeroed();

Expand Down
6 changes: 6 additions & 0 deletions src/sys/unix/uds/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ impl UnixStream {
UnixStream { inner }
}

pub(crate) fn from_std(inner: net::UnixStream) -> Self {
let raw_fd = inner.into_raw_fd();
let inner = unsafe { FromRawFd::from_raw_fd(raw_fd) };
Self { inner }
}

pub(crate) fn connect(path: &Path) -> io::Result<UnixStream> {
let socket = new_socket(libc::AF_UNIX, libc::SOCK_STREAM)?;
let (sockaddr, socklen) = socket_addr(path)?;
Expand Down
18 changes: 18 additions & 0 deletions src/sys/windows/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ macro_rules! wouldblock {
}

impl TcpStream {
pub(crate) fn from_std(inner: net::TcpStream) -> Self {
let raw_socket = inner.into_raw_socket();
let inner = unsafe { FromRawSocket::from_raw_socket(raw_socket) };
Self {
internal: Arc::new(Mutex::new(None)),
inner,
}
}

pub fn connect(addr: SocketAddr) -> io::Result<TcpStream> {
init();
new_socket(addr, SOCK_STREAM)
Expand Down Expand Up @@ -324,6 +333,15 @@ impl AsRawSocket for TcpStream {
}

impl TcpListener {
pub(crate) fn from_std(inner: net::TcpListener) -> Self {
let raw_socket = inner.into_raw_socket();
let inner = unsafe { FromRawSocket::from_raw_socket(raw_socket) };
Self {
internal: Arc::new(Mutex::new(None)),
inner,
}
}

pub fn bind(addr: SocketAddr) -> io::Result<TcpListener> {
init();
new_socket(addr, SOCK_STREAM).and_then(|socket| {
Expand Down
Loading