diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-04-18 13:23:56 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-04-19 00:47:14 -0700 |
| commit | 3915e17cd70e2d584726364851d368badb8bf15b (patch) | |
| tree | 4b3b57fa402272ff03274a93251393c8d3a6b2fc /src/libstd/io | |
| parent | 9d5082e88a11f1daf66f062eb061efcee54718a0 (diff) | |
| download | rust-3915e17cd70e2d584726364851d368badb8bf15b.tar.gz rust-3915e17cd70e2d584726364851d368badb8bf15b.zip | |
std: Add an experimental connect_timeout function
This adds a `TcpStream::connect_timeout` function in order to assist opening connections with a timeout (cc #13523). There isn't really much design space for this specific operation (unlike timing out normal blocking reads/writes), so I am fairly confident that this is the correct interface for this function. The function is marked #[experimental] because it takes a u64 timeout argument, and the u64 type is likely to change in the future.
Diffstat (limited to 'src/libstd/io')
| -rw-r--r-- | src/libstd/io/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/net/tcp.rs | 17 |
2 files changed, 18 insertions, 1 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) }) } |
