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

Try recv timeout #284

Merged
merged 10 commits into from
Nov 18, 2021
27 changes: 23 additions & 4 deletions src/platform/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::ptr;
use std::slice;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::UNIX_EPOCH;
use std::time::{Duration, UNIX_EPOCH};
use std::thread;
use mio::unix::EventedFd;
use mio::{Poll, Token, Events, Ready, PollOpt};
Expand Down Expand Up @@ -148,6 +148,11 @@ impl OsIpcReceiver {
-> Result<(Vec<u8>, Vec<OsOpaqueIpcChannel>, Vec<OsIpcSharedMemory>),UnixError> {
recv(self.fd.get(), BlockingMode::Nonblocking)
}

pub fn try_recv_timeout(&self, duration: Duration)
-> Result<(Vec<u8>, Vec<OsOpaqueIpcChannel>, Vec<OsIpcSharedMemory>),UnixError> {
recv(self.fd.get(), BlockingMode::Timeout(duration))
}
}

#[derive(PartialEq, Debug)]
Expand Down Expand Up @@ -891,6 +896,7 @@ impl From<io::Error> for UnixError {
enum BlockingMode {
Blocking,
Nonblocking,
Timeout(Duration),
}

fn recv(fd: c_int, blocking_mode: BlockingMode)
Expand Down Expand Up @@ -1054,10 +1060,23 @@ impl UnixCmsg {

unsafe fn recv(&mut self, fd: c_int, blocking_mode: BlockingMode)
-> Result<usize, UnixError> {
if let BlockingMode::Nonblocking = blocking_mode {
if libc::fcntl(fd, libc::F_SETFL, libc::O_NONBLOCK) < 0 {
return Err(UnixError::last())
match blocking_mode {
BlockingMode::Nonblocking => {
if libc::fcntl(fd, libc::F_SETFL, libc::O_NONBLOCK) < 0 {
return Err(UnixError::last())
}
},
BlockingMode::Timeout(duration) => {
let events = libc::POLLIN | libc::POLLPRI | libc::POLLRDHUP;
let fd = &mut [libc::pollfd {fd, events, revents: 0}];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's write this as let mut fd = [...] instead. I find that easier to reason about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in da99fca

let result = libc::poll(fd.as_mut_ptr(), fd.len() as libc::c_ulong, duration.as_millis() as libc::c_int);
if result == 0 {
return Err(UnixError::Errno(EAGAIN));
} else if result < 0 {
return Err(UnixError::last());
};
}
BlockingMode::Blocking => {},
}

let result = recvmsg(fd, &mut self.msghdr, RECVMSG_FLAGS);
Expand Down