I have a stream that supports a list of sockets, and I would like to iterate over the list, see if there is anything to read, if so - act on it, if not - go to the next. The problem is that as soon as I run into the first node, all execution stops until something comes to a read.
I am using std::io::Read::read(&mut self, buf: &mut [u8]) -> Result<usize>
From doc
This function does not give any guarantees as to whether it blocks the data waiting for data, but if the object should block reading but cannot, it usually signals this via the return value Err.
Delving into the source, implementation of TcpStream Read
impl Read for TcpStream { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) } }
What causes
pub fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { let fd = self.fd(); let dolock = || self.lock_nonblocking(); let doread = |nb| unsafe { let flags = if nb {c::MSG_DONTWAIT} else {0}; libc::recv(fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len() as wrlen, flags) as libc::c_int }; read(fd, self.read_deadline, dolock, doread) }
And finally, it calls read<T, L, R>(fd: sock_t, deadline: u64, mut lock: L, mut read: R)
Where can I see loops over non-blocking reads until the data is restored or an error occurs.
Is there a way to force non-blocking reads using TcpStream ?
sockets tcp rust
nathansizemore
source share