diff options
| author | Steven Fackler <sfackler@gmail.com> | 2015-05-26 23:47:03 -0700 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2015-05-28 20:03:20 -0700 |
| commit | 69a0e1af9553ad50ee2d9c9176470ddeef70717c (patch) | |
| tree | 96d0888e821b7787c5387b1c1c55a162c07f9be7 /src/libstd/net | |
| parent | 1a3cffbddfa21aac6fabd2f07f86703fbf1f26a5 (diff) | |
| download | rust-69a0e1af9553ad50ee2d9c9176470ddeef70717c.tar.gz rust-69a0e1af9553ad50ee2d9c9176470ddeef70717c.zip | |
Implement RFC 1047 - socket timeouts
Closes #25619
Diffstat (limited to 'src/libstd/net')
| -rw-r--r-- | src/libstd/net/tcp.rs | 111 | ||||
| -rw-r--r-- | src/libstd/net/udp.rs | 99 |
2 files changed, 210 insertions, 0 deletions
diff --git a/src/libstd/net/tcp.rs b/src/libstd/net/tcp.rs index 28063c1edb3..a0169ae000b 100644 --- a/src/libstd/net/tcp.rs +++ b/src/libstd/net/tcp.rs @@ -19,6 +19,7 @@ use io; use net::{ToSocketAddrs, SocketAddr, Shutdown}; use sys_common::net as net_imp; use sys_common::{AsInner, FromInner}; +use time::Duration; /// A structure which represents a TCP stream between a local socket and a /// remote socket. @@ -139,6 +140,50 @@ impl TcpStream { pub fn set_keepalive(&self, seconds: Option<u32>) -> io::Result<()> { self.0.set_keepalive(seconds) } + + /// Sets the read timeout to the timeout specified. + /// + /// If the value specified is `None`, then `read` calls will block + /// indefinitely. It is an error to pass the zero `Duration` to this + /// method. + #[unstable(feature = "socket_timeout", reason = "RFC 1047 - recently added")] + pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> { + self.0.set_read_timeout(dur) + } + + /// Sets the write timeout to the timeout specified. + /// + /// If the value specified is `None`, then `write` calls will block + /// indefinitely. It is an error to pass the zero `Duration` to this + /// method. + #[unstable(feature = "socket_timeout", reason = "RFC 1047 - recently added")] + pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> { + self.0.set_write_timeout(dur) + } + + /// Returns the read timeout of this socket. + /// + /// If the timeout is `None`, then `read` calls will block indefinitely. + /// + /// # Note + /// + /// Some platforms do not provide access to the current timeout. + #[unstable(feature = "socket_timeout", reason = "RFC 1047 - recently added")] + pub fn read_timeout(&self) -> io::Result<Option<Duration>> { + self.0.read_timeout() + } + + /// Returns the write timeout of this socket. + /// + /// If the timeout is `None`, then `write` calls will block indefinitely. + /// + /// # Note + /// + /// Some platforms do not provide access to the current timeout. + #[unstable(feature = "socket_timeout", reason = "RFC 1047 - recently added")] + pub fn write_timeout(&self) -> io::Result<Option<Duration>> { + self.0.write_timeout() + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -262,6 +307,7 @@ mod tests { use net::test::{next_test_ip4, next_test_ip6}; use sync::mpsc::channel; use sys_common::AsInner; + use time::Duration; use thread; fn each_ip(f: &mut FnMut(SocketAddr)) { @@ -855,4 +901,69 @@ mod tests { stream_inner); assert_eq!(format!("{:?}", stream), compare); } + + #[test] + fn timeouts() { + let addr = next_test_ip4(); + let listener = t!(TcpListener::bind(&addr)); + + let stream = t!(TcpStream::connect(&("localhost", addr.port()))); + let dur = Duration::new(15410, 0); + + assert_eq!(None, t!(stream.read_timeout())); + + t!(stream.set_read_timeout(Some(dur))); + assert_eq!(Some(dur), t!(stream.read_timeout())); + + assert_eq!(None, t!(stream.write_timeout())); + + t!(stream.set_write_timeout(Some(dur))); + assert_eq!(Some(dur), t!(stream.write_timeout())); + + t!(stream.set_read_timeout(None)); + assert_eq!(None, t!(stream.read_timeout())); + + t!(stream.set_write_timeout(None)); + assert_eq!(None, t!(stream.write_timeout())); + } + + #[test] + fn test_read_timeout() { + let addr = next_test_ip4(); + let listener = t!(TcpListener::bind(&addr)); + + let mut stream = t!(TcpStream::connect(&("localhost", addr.port()))); + t!(stream.set_read_timeout(Some(Duration::from_millis(10)))); + + let mut buf = [0; 10]; + let wait = Duration::span(|| { + let kind = stream.read(&mut buf).err().expect("expected error").kind(); + assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut); + }); + assert!(wait > Duration::from_millis(5)); + assert!(wait < Duration::from_millis(15)); + } + + #[test] + fn test_read_with_timeout() { + let addr = next_test_ip4(); + let listener = t!(TcpListener::bind(&addr)); + + let mut stream = t!(TcpStream::connect(&("localhost", addr.port()))); + t!(stream.set_read_timeout(Some(Duration::from_millis(10)))); + + let mut other_end = t!(listener.accept()).0; + t!(other_end.write_all(b"hello world")); + + let mut buf = [0; 11]; + t!(stream.read(&mut buf)); + assert_eq!(b"hello world", &buf[..]); + + let wait = Duration::span(|| { + let kind = stream.read(&mut buf).err().expect("expected error").kind(); + assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut); + }); + assert!(wait > Duration::from_millis(5)); + assert!(wait < Duration::from_millis(15)); + } } diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index 67c7096904d..e078f4cd44f 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -18,6 +18,7 @@ use io::{self, Error, ErrorKind}; use net::{ToSocketAddrs, SocketAddr, IpAddr}; use sys_common::net as net_imp; use sys_common::{AsInner, FromInner}; +use time::Duration; /// A User Datagram Protocol socket. /// @@ -127,6 +128,42 @@ impl UdpSocket { pub fn set_time_to_live(&self, ttl: i32) -> io::Result<()> { self.0.time_to_live(ttl) } + + /// Sets the read timeout to the timeout specified. + /// + /// If the value specified is `None`, then `read` calls will block + /// indefinitely. It is an error to pass the zero `Duration` to this + /// method. + #[unstable(feature = "socket_timeout", reason = "RFC 1047 - recently added")] + pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> { + self.0.set_read_timeout(dur) + } + + /// Sets the write timeout to the timeout specified. + /// + /// If the value specified is `None`, then `write` calls will block + /// indefinitely. It is an error to pass the zero `Duration` to this + /// method. + #[unstable(feature = "socket_timeout", reason = "RFC 1047 - recently added")] + pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> { + self.0.set_write_timeout(dur) + } + + /// Returns the read timeout of this socket. + /// + /// If the timeout is `None`, then `read` calls will block indefinitely. + #[unstable(feature = "socket_timeout", reason = "RFC 1047 - recently added")] + pub fn read_timeout(&self) -> io::Result<Option<Duration>> { + self.0.read_timeout() + } + + /// Returns the write timeout of this socket. + /// + /// If the timeout is `None`, then `write` calls will block indefinitely. + #[unstable(feature = "socket_timeout", reason = "RFC 1047 - recently added")] + pub fn write_timeout(&self) -> io::Result<Option<Duration>> { + self.0.write_timeout() + } } impl AsInner<net_imp::UdpSocket> for UdpSocket { @@ -152,6 +189,7 @@ mod tests { use net::test::{next_test_ip4, next_test_ip6}; use sync::mpsc::channel; use sys_common::AsInner; + use time::Duration; use thread; fn each_ip(f: &mut FnMut(SocketAddr, SocketAddr)) { @@ -321,4 +359,65 @@ mod tests { socket_addr, name, udpsock_inner); assert_eq!(format!("{:?}", udpsock), compare); } + + #[test] + fn timeouts() { + let addr = next_test_ip4(); + + let stream = t!(UdpSocket::bind(&addr)); + let dur = Duration::new(15410, 0); + + assert_eq!(None, t!(stream.read_timeout())); + + t!(stream.set_read_timeout(Some(dur))); + assert_eq!(Some(dur), t!(stream.read_timeout())); + + assert_eq!(None, t!(stream.write_timeout())); + + t!(stream.set_write_timeout(Some(dur))); + assert_eq!(Some(dur), t!(stream.write_timeout())); + + t!(stream.set_read_timeout(None)); + assert_eq!(None, t!(stream.read_timeout())); + + t!(stream.set_write_timeout(None)); + assert_eq!(None, t!(stream.write_timeout())); + } + + #[test] + fn test_read_timeout() { + let addr = next_test_ip4(); + + let mut stream = t!(UdpSocket::bind(&addr)); + t!(stream.set_read_timeout(Some(Duration::from_millis(10)))); + + let mut buf = [0; 10]; + let wait = Duration::span(|| { + let kind = stream.recv_from(&mut buf).err().expect("expected error").kind(); + assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut); + }); + assert!(wait > Duration::from_millis(5)); + assert!(wait < Duration::from_millis(15)); + } + + #[test] + fn test_read_with_timeout() { + let addr = next_test_ip4(); + + let mut stream = t!(UdpSocket::bind(&addr)); + t!(stream.set_read_timeout(Some(Duration::from_millis(10)))); + + t!(stream.send_to(b"hello world", &addr)); + + let mut buf = [0; 11]; + t!(stream.recv_from(&mut buf)); + assert_eq!(b"hello world", &buf[..]); + + let wait = Duration::span(|| { + let kind = stream.recv_from(&mut buf).err().expect("expected error").kind(); + assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut); + }); + assert!(wait > Duration::from_millis(5)); + assert!(wait < Duration::from_millis(15)); + } } |
