use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
use std::sync::Arc;
use rustix::io::{read, write, Errno};
use tracing::warn;
use super::PingError;
use crate::{
generic::Generic, EventSource, Interest, Mode, Poll, PostAction, Readiness, Token, TokenFactory,
};
#[cfg(target_os = "macos")]
#[inline]
fn make_ends() -> std::io::Result<(OwnedFd, OwnedFd)> {
use rustix::fs::{fcntl_getfl, fcntl_setfl, OFlags};
use rustix::pipe::pipe;
let (read, write) = pipe()?;
let set_flags = |fd| fcntl_setfl(fd, fcntl_getfl(fd)? | OFlags::CLOEXEC | OFlags::NONBLOCK);
set_flags(&read)?;
set_flags(&write)?;
Ok((read, write))
}
#[cfg(not(target_os = "macos"))]
#[inline]
fn make_ends() -> std::io::Result<(OwnedFd, OwnedFd)> {
use rustix::pipe::{pipe_with, PipeFlags};
Ok(pipe_with(PipeFlags::CLOEXEC | PipeFlags::NONBLOCK)?)
}
#[inline]
pub fn make_ping() -> std::io::Result<(Ping, PingSource)> {
let (read, write) = make_ends()?;
let source = PingSource {
pipe: Generic::new(read, Interest::READ, Mode::Level),
};
let ping = Ping {
pipe: Arc::new(write),
};
Ok((ping, source))
}
#[inline]
fn send_ping(fd: BorrowedFd<'_>) -> std::io::Result<()> {
write(fd, &[0u8])?;
Ok(())
}
#[derive(Debug)]
pub struct PingSource {
pipe: Generic<OwnedFd>,
}
impl EventSource for PingSource {
type Event = ();
type Metadata = ();
type Ret = ();
type Error = PingError;
fn process_events<C>(
&mut self,
readiness: Readiness,
token: Token,
mut callback: C,
) -> Result<PostAction, Self::Error>
where
C: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
{
self.pipe
.process_events(readiness, token, |_, fd| {
let mut buf = [0u8; 32];
let mut read_something = false;
let mut action = PostAction::Continue;
loop {
match read(&fd, &mut buf) {
Ok(0) => {
action = PostAction::Remove;
break;
}
Ok(_) => read_something = true,
Err(Errno::AGAIN) => break,
Err(e) => return Err(e.into()),
}
}
if read_something {
callback((), &mut ());
}
Ok(action)
})
.map_err(|e| PingError(e.into()))
}
fn register(&mut self, poll: &mut Poll, token_factory: &mut TokenFactory) -> crate::Result<()> {
self.pipe.register(poll, token_factory)
}
fn reregister(
&mut self,
poll: &mut Poll,
token_factory: &mut TokenFactory,
) -> crate::Result<()> {
self.pipe.reregister(poll, token_factory)
}
fn unregister(&mut self, poll: &mut Poll) -> crate::Result<()> {
self.pipe.unregister(poll)
}
}
#[derive(Clone, Debug)]
pub struct Ping {
pipe: Arc<OwnedFd>,
}
impl Ping {
pub fn ping(&self) {
if let Err(e) = send_ping(self.pipe.as_fd()) {
warn!("Failed to write a ping: {e:?}");
}
}
}