fuchsia_async/net/fuchsia/
mod.rs#![allow(missing_docs)]
mod tcp;
pub use self::tcp::*;
mod udp;
pub use self::udp::*;
use futures::io::{self, AsyncRead, AsyncWrite};
use futures::ready;
use futures::task::{AtomicWaker, Context};
use zx::{self as zx, AsHandleRef};
use std::io::{Read, Write};
use std::os::unix::io::{AsRawFd, RawFd};
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::task::Poll;
use std::{fmt, mem};
use crate::runtime::{EHandle, PacketReceiver, ReceiverRegistration};
const READABLE: usize = libc::EPOLLIN as usize;
const WRITABLE: usize = libc::EPOLLOUT as usize;
const ERROR: usize = libc::EPOLLERR as usize;
const HUP: usize = libc::EPOLLHUP as usize;
pub(crate) struct EventedFdPacketReceiver {
fdio: *const syscall::fdio_t,
signals: AtomicUsize,
read_task: AtomicWaker,
write_task: AtomicWaker,
}
unsafe impl Send for EventedFdPacketReceiver {}
unsafe impl Sync for EventedFdPacketReceiver {}
impl PacketReceiver for EventedFdPacketReceiver {
fn receive_packet(&self, packet: zx::Packet) {
let observed_signals = if let zx::PacketContents::SignalOne(p) = packet.contents() {
p.observed()
} else {
return;
};
let mut events: u32 = 0;
unsafe {
syscall::fdio_unsafe_wait_end(self.fdio, observed_signals.bits(), &mut events);
}
let events = events as usize;
let old = self.signals.fetch_or(events, Ordering::SeqCst);
let became_readable = ((events & READABLE) != 0) && ((old & READABLE) == 0);
let became_writable = ((events & WRITABLE) != 0) && ((old & WRITABLE) == 0);
let err_occurred = (events & (ERROR | HUP)) != 0;
if became_readable || err_occurred {
self.read_task.wake();
}
if became_writable || err_occurred {
self.write_task.wake();
}
}
}
pub struct EventedFd<T> {
inner: T,
fdio: *const syscall::fdio_t,
signal_receiver: mem::ManuallyDrop<ReceiverRegistration<EventedFdPacketReceiver>>,
}
unsafe impl<T> Send for EventedFd<T> where T: Send {}
unsafe impl<T> Sync for EventedFd<T> where T: Sync {}
impl<T> Unpin for EventedFd<T> {}
impl<T> Drop for EventedFd<T> {
fn drop(&mut self) {
unsafe {
mem::ManuallyDrop::drop(&mut self.signal_receiver);
syscall::fdio_unsafe_release(self.fdio);
}
}
}
impl<T: fmt::Debug> fmt::Debug for EventedFd<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EventedFd").field("inner", &self.inner).finish()
}
}
impl<T> EventedFd<T>
where
T: AsRawFd,
{
pub unsafe fn new(inner: T) -> io::Result<Self> {
let fdio = syscall::fdio_unsafe_fd_to_io(inner.as_raw_fd());
let signal_receiver =
EHandle::local().register_receiver(Arc::new(EventedFdPacketReceiver {
fdio,
signals: AtomicUsize::new(READABLE | WRITABLE),
read_task: AtomicWaker::new(),
write_task: AtomicWaker::new(),
}));
let evented_fd =
EventedFd { inner, fdio, signal_receiver: mem::ManuallyDrop::new(signal_receiver) };
evented_fd.schedule_packet(ERROR | HUP);
evented_fd.schedule_packet(READABLE);
evented_fd.schedule_packet(WRITABLE);
Ok(evented_fd)
}
pub fn poll_readable(&self, cx: &mut Context<'_>) -> Poll<Result<(), zx::Status>> {
let receiver = self.signal_receiver.receiver();
if (receiver.signals.load(Ordering::SeqCst) & (READABLE | ERROR | HUP)) != 0 {
Poll::Ready(Ok(()))
} else {
self.need_read(cx);
Poll::Pending
}
}
pub fn poll_writable(&self, cx: &mut Context<'_>) -> Poll<Result<(), zx::Status>> {
let receiver = self.signal_receiver.receiver();
if (receiver.signals.load(Ordering::SeqCst) & (WRITABLE | ERROR | HUP)) != 0 {
Poll::Ready(Ok(()))
} else {
self.need_write(cx);
Poll::Pending
}
}
pub fn as_ref(&self) -> &T {
&self.inner
}
pub fn as_mut(&mut self) -> &mut T {
&mut self.inner
}
pub fn need_read(&self, cx: &mut Context<'_>) {
let receiver = self.signal_receiver.receiver();
receiver.read_task.register(cx.waker());
let old = receiver.signals.fetch_and(!READABLE, Ordering::SeqCst);
if (old & READABLE) != 0 {
self.schedule_packet(READABLE);
}
}
pub fn need_write(&self, cx: &mut Context<'_>) {
let receiver = self.signal_receiver.receiver();
receiver.write_task.register(cx.waker());
let old = receiver.signals.fetch_and(!WRITABLE, Ordering::SeqCst);
if (old & WRITABLE) != 0 {
self.schedule_packet(WRITABLE);
}
}
fn schedule_packet(&self, signals: usize) {
unsafe {
let (mut raw_handle, mut raw_signals) = (0, 0);
syscall::fdio_unsafe_wait_begin(
self.fdio,
signals as u32,
&mut raw_handle,
&mut raw_signals,
);
let handle = zx::Handle::from_raw(raw_handle);
let signals = zx::Signals::from_bits_truncate(raw_signals);
let res = handle.wait_async_handle(
self.signal_receiver.port(),
self.signal_receiver.key(),
signals,
zx::WaitAsyncOpts::empty(),
);
mem::forget(handle);
res.expect("Error scheduling EventedFd notification");
}
}
pub fn clear(&self) {
self.signal_receiver.receiver().signals.store(0, Ordering::SeqCst);
}
}
impl<T: AsRawFd> AsRawFd for EventedFd<T> {
fn as_raw_fd(&self) -> RawFd {
self.as_ref().as_raw_fd()
}
}
impl<T: AsRawFd + Read> AsyncRead for EventedFd<T> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, io::Error>> {
ready!(EventedFd::poll_readable(&*self, cx))?;
let res = (&mut *self).as_mut().read(buf);
if let Err(e) = &res {
if e.kind() == io::ErrorKind::WouldBlock {
self.need_read(cx);
return Poll::Pending;
}
}
Poll::Ready(res.map_err(Into::into))
}
}
impl<T: AsRawFd + Write> AsyncWrite for EventedFd<T> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
ready!(EventedFd::poll_writable(&*self, cx))?;
let res = (&mut *self).as_mut().write(buf);
if let Err(e) = &res {
if e.kind() == io::ErrorKind::WouldBlock {
self.need_write(cx);
return Poll::Pending;
}
}
Poll::Ready(res.map_err(Into::into))
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}
}
impl<'a, T> AsyncRead for &'a EventedFd<T>
where
T: AsRawFd,
for<'b> &'b T: Read,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, io::Error>> {
ready!(EventedFd::poll_readable(&*self, cx))?;
let res = (&*self).as_ref().read(buf);
if let Err(e) = &res {
if e.kind() == io::ErrorKind::WouldBlock {
self.need_read(cx);
return Poll::Pending;
}
}
Poll::Ready(res.map_err(Into::into))
}
}
impl<'a, T> AsyncWrite for &'a EventedFd<T>
where
T: AsRawFd,
for<'b> &'b T: Write,
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
ready!(EventedFd::poll_writable(&*self, cx))?;
let res = (&*self).as_ref().write(buf);
if let Err(e) = &res {
if e.kind() == io::ErrorKind::WouldBlock {
self.need_write(cx);
return Poll::Pending;
}
}
Poll::Ready(res.map_err(Into::into))
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}
}
mod syscall {
#![allow(non_camel_case_types, improper_ctypes)]
use std::os::unix::io::RawFd;
pub use zx::sys::{zx_handle_t, zx_signals_t};
pub type fdio_t = ();
extern "C" {
pub fn fdio_unsafe_fd_to_io(fd: RawFd) -> *const fdio_t;
pub fn fdio_unsafe_release(io: *const fdio_t);
pub fn fdio_unsafe_wait_begin(
io: *const fdio_t,
events: u32,
handle_out: &mut zx_handle_t,
signals_out: &mut zx_signals_t,
);
pub fn fdio_unsafe_wait_end(io: *const fdio_t, signals: zx_signals_t, events_out: &mut u32);
}
}