Skip to content

Commit

Permalink
port old iocp routines back
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbai committed May 7, 2021
1 parent 772c692 commit 462c9bd
Show file tree
Hide file tree
Showing 25 changed files with 4,370 additions and 1,748 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pipe = ["os-ext"] # Replaced with "os-ext" feature.
os-util = ["os-ext"]# Replaced with "os-ext" feature.

[dependencies]
iovec = "0.1.2"
net2 = "0.2.33"
log = "0.4.8"

[target.'cfg(unix)'.dependencies]
Expand Down
18 changes: 2 additions & 16 deletions src/event/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ impl Event {
/// [alternate]: fmt::Formatter::alternate
impl fmt::Debug for Event {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let alternate = f.alternate();
let mut d = f.debug_struct("Event");
d.field("token", &self.token())
.field("readable", &self.is_readable())
Expand All @@ -202,20 +201,7 @@ impl fmt::Debug for Event {
.field("write_closed", &self.is_write_closed())
.field("priority", &self.is_priority())
.field("aio", &self.is_aio())
.field("lio", &self.is_lio());

if alternate {
struct EventDetails<'a>(&'a sys::Event);

impl<'a> fmt::Debug for EventDetails<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
sys::event::debug_details(f, self.0)
}
}

d.field("details", &EventDetails(&self.inner)).finish()
} else {
d.finish()
}
.field("lio", &self.is_lio())
.finish()
}
}
4 changes: 4 additions & 0 deletions src/interest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ impl Interest {
pub const fn is_lio(self) -> bool {
(self.0.get() & LIO) != 0
}

pub const fn as_u8(self) -> u8 {
self.0.get()
}
}

impl ops::BitOr for Interest {
Expand Down
48 changes: 0 additions & 48 deletions src/io_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,47 +66,6 @@ pub struct IoSource<T> {
selector_id: SelectorId,
}

impl<T> IoSource<T> {
/// Create a new `IoSource`.
pub fn new(io: T) -> IoSource<T> {
IoSource {
state: IoSourceState::new(),
inner: io,
#[cfg(debug_assertions)]
selector_id: SelectorId::new(),
}
}

/// Execute an I/O operations ensuring that the socket receives more events
/// if it hits a [`WouldBlock`] error.
///
/// # Notes
///
/// This method is required to be called for **all** I/O operations to
/// ensure the user will receive events once the socket is ready again after
/// returning a [`WouldBlock`] error.
///
/// [`WouldBlock`]: io::ErrorKind::WouldBlock
pub fn do_io<F, R>(&self, f: F) -> io::Result<R>
where
F: FnOnce(&T) -> io::Result<R>,
{
self.state.do_io(f, &self.inner)
}

/// Returns the I/O source, dropping the state.
///
/// # Notes
///
/// To ensure no more events are to be received for this I/O source first
/// [`deregister`] it.
///
/// [`deregister`]: Registry::deregister
pub fn into_inner(self) -> T {
self.inner
}
}

/// Be careful when using this method. All I/O operations that may block must go
/// through the [`do_io`] method.
///
Expand Down Expand Up @@ -220,13 +179,6 @@ impl SelectorId {
/// `sys::Selector`. Valid selector ids start at 1.
const UNASSOCIATED: usize = 0;

/// Create a new `SelectorId`.
const fn new() -> SelectorId {
SelectorId {
id: AtomicUsize::new(Self::UNASSOCIATED),
}
}

/// Associate an I/O source with `registry`, returning an error if its
/// already registered.
fn associate(&self, registry: &Registry) -> io::Result<()> {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![doc(html_root_url = "https://docs.rs/mio/0.7.11")]
#![deny(
missing_docs,
// missing_docs,
missing_debug_implementations,
rust_2018_idioms,
unused_imports,
Expand Down Expand Up @@ -45,9 +45,10 @@
#[macro_use]
mod macros;

#[macro_use]
mod sys;
mod interest;
mod poll;
mod sys;
mod token;
mod waker;

Expand Down
6 changes: 5 additions & 1 deletion src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
//! [portability guidelines]: ../struct.Poll.html#portability
mod tcp;
pub use self::tcp::{TcpListener, TcpSocket, TcpStream, TcpKeepalive};
pub use self::tcp::{TcpKeepalive, TcpListener, TcpSocket, TcpStream};

#[cfg(not(windows))]
mod udp;
#[cfg(not(windows))]
pub use self::udp::UdpSocket;
#[cfg(windows)]
pub use crate::sys::udp::UdpSocket;

#[cfg(unix)]
mod uds;
Expand Down
13 changes: 12 additions & 1 deletion src/net/tcp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
#[cfg(not(windows))]
mod listener;
#[cfg(not(windows))]
pub use self::listener::TcpListener;
#[cfg(windows)]
pub use crate::sys::TcpListener;

mod socket;
pub use self::socket::{TcpSocket, TcpKeepalive};
// #[cfg(not(windows))]
pub use self::socket::{TcpKeepalive, TcpSocket};
// #[cfg(windows)]
// pub use crate::sys::tcp::TcpSocket;

#[cfg(not(windows))]
mod stream;
#[cfg(not(windows))]
pub use self::stream::TcpStream;
#[cfg(windows)]
pub use crate::sys::TcpStream;
18 changes: 9 additions & 9 deletions src/net/tcp/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::sys;
/// The socket will be closed when the value is dropped.
#[derive(Debug)]
pub struct TcpSocket {
sys: sys::tcp::TcpSocket,
pub sys: sys::tcp::TcpSocket,
}

/// Configures a socket's TCP keepalive parameters.
Expand Down Expand Up @@ -60,14 +60,6 @@ impl TcpSocket {
sys::tcp::new_v6_socket().map(|sys| TcpSocket { sys })
}

pub(crate) fn new_for_addr(addr: SocketAddr) -> io::Result<TcpSocket> {
if addr.is_ipv4() {
TcpSocket::new_v4()
} else {
TcpSocket::new_v6()
}
}

/// Bind `addr` to the TCP socket.
pub fn bind(&self, addr: SocketAddr) -> io::Result<()> {
sys::tcp::bind(self.sys, addr)
Expand All @@ -78,6 +70,7 @@ impl TcpSocket {
/// This consumes the socket and performs the connect operation. Once the
/// connection completes, the socket is now a non-blocking `TcpStream` and
/// can be used as such.
#[cfg(not(windows))]
pub fn connect(self, addr: SocketAddr) -> io::Result<TcpStream> {
let stream = sys::tcp::connect(self.sys, addr)?;

Expand All @@ -86,6 +79,13 @@ impl TcpSocket {
Ok(TcpStream::from_std(stream))
}

#[cfg(windows)]
pub fn connect(self, addr: SocketAddr) -> io::Result<TcpStream> {
let handle = self.sys;
mem::forget(self);
sys::tcp::TcpStream::connect_mio(handle, addr)
}

/// Listen for inbound connections, converting the socket to a
/// `TcpListener`.
pub fn listen(self, backlog: u32) -> io::Result<TcpListener> {
Expand Down
37 changes: 0 additions & 37 deletions src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,6 @@
//! * `tcp` and `udp` modules: see the [`crate::net`] module.
//! * `Waker`: see [`crate::Waker`].
cfg_os_poll! {
macro_rules! debug_detail {
(
$type: ident ($event_type: ty), $test: path,
$($(#[$target: meta])* $libc: ident :: $flag: ident),+ $(,)*
) => {
struct $type($event_type);

impl fmt::Debug for $type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut written_one = false;
$(
$(#[$target])*
#[allow(clippy::bad_bit_mask)] // Apparently some flags are zero.
{
// Windows doesn't use `libc` but the `afd` module.
if $test(&self.0, &$libc :: $flag) {
if !written_one {
write!(f, "{}", stringify!($flag))?;
written_one = true;
} else {
write!(f, "|{}", stringify!($flag))?;
}
}
}
)+
if !written_one {
write!(f, "(empty)")
} else {
Ok(())
}
}
}
};
}
}

#[cfg(unix)]
cfg_os_poll! {
mod unix;
Expand Down
Loading

0 comments on commit 462c9bd

Please sign in to comment.