Skip to content

Commit

Permalink
Add cross platform from_std to net resources
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Leimkuhler <kleimkuhler@icloud.com>
  • Loading branch information
kleimkuhler committed Nov 6, 2019
1 parent ecdc34b commit ae6a33f
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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};
Expand Down Expand Up @@ -41,6 +42,20 @@ pub struct TcpListener {
}

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.
pub fn from_std(listener: net::TcpListener) -> Self {
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
19 changes: 19 additions & 0 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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};
Expand Down Expand Up @@ -55,6 +56,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
/// 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
15 changes: 15 additions & 0 deletions src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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};
Expand Down Expand Up @@ -92,6 +93,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
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 {
let raw_fd = inner.into_raw_fd();
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
9 changes: 9 additions & 0 deletions src/sys/windows/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ macro_rules! wouldblock {
}

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

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

0 comments on commit ae6a33f

Please sign in to comment.