returns true if timed out
(
sock: &Socket,
kind: SelectKind,
interval: Option<Duration>,
)
| 2764 | |
| 2765 | /// returns true if timed out |
| 2766 | pub(crate) fn sock_select( |
| 2767 | sock: &Socket, |
| 2768 | kind: SelectKind, |
| 2769 | interval: Option<Duration>, |
| 2770 | ) -> io::Result<bool> { |
| 2771 | #[cfg(unix)] |
| 2772 | { |
| 2773 | use nix::poll::*; |
| 2774 | use std::os::fd::AsFd; |
| 2775 | let events = match kind { |
| 2776 | SelectKind::Read => PollFlags::POLLIN, |
| 2777 | SelectKind::Write => PollFlags::POLLOUT, |
| 2778 | SelectKind::Connect => PollFlags::POLLOUT | PollFlags::POLLERR, |
| 2779 | }; |
| 2780 | let mut pollfd = [PollFd::new(sock.as_fd(), events)]; |
| 2781 | let timeout = match interval { |
| 2782 | Some(d) => d.try_into().unwrap_or(PollTimeout::MAX), |
| 2783 | None => PollTimeout::NONE, |
| 2784 | }; |
| 2785 | let ret = poll(&mut pollfd, timeout)?; |
| 2786 | Ok(ret == 0) |
| 2787 | } |
| 2788 | #[cfg(windows)] |
| 2789 | { |
| 2790 | use crate::select; |
| 2791 | |
| 2792 | let fd = sock_fileno(sock); |
| 2793 | |
| 2794 | let mut reads = select::FdSet::new(); |
| 2795 | let mut writes = select::FdSet::new(); |
| 2796 | let mut errs = select::FdSet::new(); |
| 2797 | |
| 2798 | let fd = fd as usize; |
| 2799 | match kind { |
| 2800 | SelectKind::Read => reads.insert(fd), |
| 2801 | SelectKind::Write => writes.insert(fd), |
| 2802 | SelectKind::Connect => { |
| 2803 | writes.insert(fd); |
| 2804 | errs.insert(fd); |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | let mut interval = interval.map(|dur| select::timeval { |
| 2809 | tv_sec: dur.as_secs() as _, |
| 2810 | tv_usec: dur.subsec_micros() as _, |
| 2811 | }); |
| 2812 | |
| 2813 | select::select( |
| 2814 | fd as i32 + 1, |
| 2815 | &mut reads, |
| 2816 | &mut writes, |
| 2817 | &mut errs, |
| 2818 | interval.as_mut(), |
| 2819 | ) |
| 2820 | .map(|ret| ret == 0) |
| 2821 | } |
| 2822 | } |
| 2823 |
no test coverage detected