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 all 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
43 changes: 43 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 @@ -58,6 +61,21 @@ impl TcpListener {
})
}

/// Creates a new `TcpListener` from a standard `net::TcpListener`.
///
/// 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; ; it is left up to the user to set it
/// in non-blocking mode.
pub fn from_std(listener: net::TcpListener) -> TcpListener {
let sys = sys::TcpListener::from_std(listener);
TcpListener {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Accepts a new `TcpStream`.
///
/// This may return an `Err(e)` where `e.kind()` is
Expand Down Expand Up @@ -168,3 +186,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()
}
}
49 changes: 49 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 @@ -65,6 +68,27 @@ 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; it is left up to the user to set it in
/// non-blocking mode.
///
/// # Note
///
/// The TCP 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::TcpStream) -> TcpStream {
let sys = sys::TcpStream::from_std(stream);
TcpStream {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Returns the socket address of the remote peer of this TCP connection.
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.sys.peer_addr()
Expand Down Expand Up @@ -278,3 +302,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()
}
}
43 changes: 43 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 @@ -124,6 +127,21 @@ 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; it is left up to the user to set it in
/// non-blocking mode.
pub fn from_std(socket: net::UdpSocket) -> UdpSocket {
let sys = sys::UdpSocket::from_std(socket);
UdpSocket {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Returns the socket address that this socket was created from.
///
/// # Examples
Expand Down Expand Up @@ -556,3 +574,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()
}
}
16 changes: 16 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 @@ -31,6 +32,21 @@ impl UnixDatagram {
Ok(UnixDatagram::new(sys))
}

/// 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; ; it is left up to the user to set it
/// in non-blocking mode.
pub fn from_std(datagram: net::UnixDatagram) -> UnixDatagram {
let sys = sys::UnixDatagram::from_std(datagram);
UnixDatagram {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Connects the socket to the specified address.
pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
self.sys.connect(path)
Expand Down
16 changes: 16 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 @@ -32,6 +33,21 @@ impl UnixListener {
Ok(UnixListener::new(sys))
}

/// 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; it is left up to the user to set it in
/// non-blocking mode.
pub fn from_std(listener: net::UnixListener) -> UnixListener {
let sys = sys::UnixListener::from_std(listener);
UnixListener {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Accepts a new incoming connection to this listener.
///
/// The call is responsible for ensuring that the listening socket is in
Expand Down
22 changes: 22 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 @@ -31,6 +32,27 @@ impl UnixStream {
Ok(UnixStream::new(sys))
}

/// 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; it is left up to the user to set it in
/// non-blocking mode.
///
/// # 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) -> UnixStream {
let sys = sys::UnixStream::from_std(stream);
UnixStream {
sys,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Creates an unnamed pair of connected sockets.
///
/// Returns two `UnixStream`s which are connected to each other.
Expand Down
4 changes: 4 additions & 0 deletions src/sys/unix/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl TcpListener {
})
}

pub fn from_std(inner: net::TcpListener) -> TcpListener {
TcpListener { inner }
}

pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.inner.local_addr()
}
Expand Down
4 changes: 4 additions & 0 deletions src/sys/unix/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl TcpStream {
})
}

pub fn from_std(inner: net::TcpStream) -> TcpStream {
TcpStream { inner }
}

pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.inner.peer_addr()
}
Expand Down
4 changes: 4 additions & 0 deletions src/sys/unix/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ impl UdpSocket {
})
}

pub fn from_std(io: net::UdpSocket) -> UdpSocket {
UdpSocket { io }
}

pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.io.local_addr()
}
Expand Down
4 changes: 4 additions & 0 deletions src/sys/unix/uds/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ impl UnixDatagram {
Ok(unsafe { UnixDatagram::from_raw_fd(socket) })
}

pub fn from_std(inner: net::UnixDatagram) -> UnixDatagram {
UnixDatagram { inner }
}

pub(crate) fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
self.inner.connect(path)
}
Expand Down
4 changes: 4 additions & 0 deletions src/sys/unix/uds/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ impl UnixListener {
.map(|_| unsafe { UnixListener::from_raw_fd(socket) })
}

pub fn from_std(inner: net::UnixListener) -> UnixListener {
UnixListener { inner }
}

pub(crate) fn try_clone(&self) -> io::Result<UnixListener> {
let inner = self.inner.try_clone()?;
Ok(UnixListener::new(inner))
Expand Down
4 changes: 4 additions & 0 deletions src/sys/unix/uds/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl UnixStream {
Ok(unsafe { UnixStream::from_raw_fd(socket) })
}

pub fn from_std(inner: net::UnixStream) -> UnixStream {
UnixStream { inner }
}

pub(crate) fn pair() -> io::Result<(UnixStream, UnixStream)> {
let fds = [-1; 2];
let flags = libc::SOCK_STREAM;
Expand Down
14 changes: 14 additions & 0 deletions src/sys/windows/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ impl TcpStream {
})
}

pub fn from_std(inner: net::TcpStream) -> TcpStream {
TcpStream {
internal: Box::new(Mutex::new(None)),
inner,
}
}

pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.inner.peer_addr()
}
Expand Down Expand Up @@ -316,6 +323,13 @@ impl TcpListener {
})
}

pub fn from_std(inner: net::TcpListener) -> TcpListener {
TcpListener {
internal: Box::new(Mutex::new(None)),
inner,
}
}

pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.inner.local_addr()
}
Expand Down
Loading