Allow setting nonblock on sockets

This commit is contained in:
Jeremy Soller 2016-11-23 14:22:39 -07:00
parent 4a0bc71bb7
commit 6733074c84
3 changed files with 16 additions and 11 deletions

View file

@ -48,6 +48,16 @@ impl FileDesc {
cvt(libc::write(self.fd, buf))
}
pub fn duplicate(&self) -> io::Result<FileDesc> {
let new_fd = cvt(libc::dup(self.fd, &[]))?;
Ok(FileDesc::new(new_fd))
}
pub fn nonblocking(&self) -> io::Result<bool> {
let flags = cvt(libc::fcntl(self.fd, libc::F_GETFL, 0))?;
Ok(flags & libc::O_NONBLOCK == libc::O_NONBLOCK)
}
pub fn set_cloexec(&self) -> io::Result<()> {
let mut flags = cvt(libc::fcntl(self.fd, libc::F_GETFL, 0))?;
flags |= libc::O_CLOEXEC;
@ -63,11 +73,6 @@ impl FileDesc {
}
cvt(libc::fcntl(self.fd, libc::F_SETFL, flags)).and(Ok(()))
}
pub fn duplicate(&self) -> io::Result<FileDesc> {
let new_fd = cvt(libc::dup(self.fd, &[]))?;
Ok(FileDesc::new(new_fd))
}
}
impl<'a> Read for &'a FileDesc {

View file

@ -58,7 +58,7 @@ impl TcpStream {
}
pub fn nonblocking(&self) -> Result<bool> {
Err(Error::new(ErrorKind::Other, "TcpStream::nonblocking not implemented"))
self.0.fd().nonblocking()
}
pub fn only_v6(&self) -> Result<bool> {
@ -81,8 +81,8 @@ impl TcpStream {
Err(Error::new(ErrorKind::Other, "TcpStream::set_nodelay not implemented"))
}
pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> {
Err(Error::new(ErrorKind::Other, "TcpStream::set_nonblocking not implemented"))
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> {
self.0.fd().set_nonblocking(nonblocking)
}
pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {

View file

@ -90,7 +90,7 @@ impl UdpSocket {
}
pub fn nonblocking(&self) -> Result<bool> {
Err(Error::new(ErrorKind::Other, "UdpSocket::nonblocking not implemented"))
self.0.fd().nonblocking()
}
pub fn only_v6(&self) -> Result<bool> {
@ -125,8 +125,8 @@ impl UdpSocket {
Err(Error::new(ErrorKind::Other, "UdpSocket::set_multicast_ttl_v4 not implemented"))
}
pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> {
Err(Error::new(ErrorKind::Other, "UdpSocket::set_nonblocking not implemented"))
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> {
self.0.fd().set_nonblocking(nonblocking)
}
pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {