diff options
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/net/tcp.rs | 17 | ||||
| -rw-r--r-- | src/libstd/rt/rtio.rs | 3 |
3 files changed, 20 insertions, 2 deletions
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index d8267e472bd..9c163523abe 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -430,6 +430,8 @@ pub enum IoErrorKind { IoUnavailable, /// A parameter was incorrect in a way that caused an I/O error not part of this list. InvalidInput, + /// The I/O operation's timeout expired, causing it to be canceled. + TimedOut, } /// A trait for objects which are byte-oriented streams. Readers are defined by diff --git a/src/libstd/io/net/tcp.rs b/src/libstd/io/net/tcp.rs index 49e6bcff8eb..4f1e6bd7418 100644 --- a/src/libstd/io/net/tcp.rs +++ b/src/libstd/io/net/tcp.rs @@ -22,6 +22,7 @@ use io::IoResult; use io::net::ip::SocketAddr; use io::{Reader, Writer, Listener, Acceptor}; use kinds::Send; +use option::{None, Some}; use rt::rtio::{IoFactory, LocalIo, RtioSocket, RtioTcpListener}; use rt::rtio::{RtioTcpAcceptor, RtioTcpStream}; @@ -57,7 +58,21 @@ impl TcpStream { /// If no error is encountered, then `Ok(stream)` is returned. pub fn connect(addr: SocketAddr) -> IoResult<TcpStream> { LocalIo::maybe_raise(|io| { - io.tcp_connect(addr).map(TcpStream::new) + io.tcp_connect(addr, None).map(TcpStream::new) + }) + } + + /// Creates a TCP connection to a remote socket address, timing out after + /// the specified number of milliseconds. + /// + /// This is the same as the `connect` method, except that if the timeout + /// specified (in milliseconds) elapses before a connection is made an error + /// will be returned. The error's kind will be `TimedOut`. + #[experimental = "the timeout argument may eventually change types"] + pub fn connect_timeout(addr: SocketAddr, + timeout_ms: u64) -> IoResult<TcpStream> { + LocalIo::maybe_raise(|io| { + io.tcp_connect(addr, Some(timeout_ms)).map(TcpStream::new) }) } diff --git a/src/libstd/rt/rtio.rs b/src/libstd/rt/rtio.rs index cc8356d2b9a..0f3fc9c21ce 100644 --- a/src/libstd/rt/rtio.rs +++ b/src/libstd/rt/rtio.rs @@ -146,7 +146,8 @@ impl<'a> LocalIo<'a> { pub trait IoFactory { // networking - fn tcp_connect(&mut self, addr: SocketAddr) -> IoResult<~RtioTcpStream:Send>; + fn tcp_connect(&mut self, addr: SocketAddr, + timeout: Option<u64>) -> IoResult<~RtioTcpStream:Send>; fn tcp_bind(&mut self, addr: SocketAddr) -> IoResult<~RtioTcpListener:Send>; fn udp_bind(&mut self, addr: SocketAddr) -> IoResult<~RtioUdpSocket:Send>; fn unix_bind(&mut self, path: &CString) |
