|
1 | 1 | use std::io::{IoSlice, IoSliceMut, Read as _, Write as _};
|
2 | 2 | use std::net::SocketAddr;
|
3 | 3 | use std::pin::Pin;
|
| 4 | +use std::time::Duration; |
4 | 5 |
|
5 | 6 | use crate::future;
|
6 | 7 | use crate::io::{self, Read, Write};
|
@@ -96,6 +97,18 @@ impl TcpStream {
|
96 | 97 | }))
|
97 | 98 | }
|
98 | 99 |
|
| 100 | + /// Opens a TCP connection to a remote host with a timeout. |
| 101 | + /// |
| 102 | + /// Unlike `connect`, `connect_timeout` takes a single `SocketAddr` since |
| 103 | + /// timeout must be applied to individual addresses. |
| 104 | + /// |
| 105 | + /// It is an error to pass a zero `Duration` to this function. |
| 106 | + pub async fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> { |
| 107 | + let stream = io::timeout(timeout, async move { TcpStream::connect(addr).await }).await?; |
| 108 | + |
| 109 | + Ok(stream) |
| 110 | + } |
| 111 | + |
99 | 112 | /// Returns the local address that this stream is connected to.
|
100 | 113 | ///
|
101 | 114 | /// ## Examples
|
@@ -255,6 +268,29 @@ impl TcpStream {
|
255 | 268 | self.watcher.get_ref().set_nodelay(nodelay)
|
256 | 269 | }
|
257 | 270 |
|
| 271 | + /// Gets the value of the `SO_ERROR` option on this socket. |
| 272 | + /// |
| 273 | + /// This will retrieve the stored error in the underlying socket, clearing |
| 274 | + /// the field in the process. This can be useful for checking errors between |
| 275 | + /// calls. |
| 276 | + /// |
| 277 | + /// # Examples |
| 278 | + /// |
| 279 | + /// ```no_run |
| 280 | + /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 281 | + /// # |
| 282 | + /// use async_std::net::TcpStream; |
| 283 | + /// |
| 284 | + /// let stream = TcpStream::connect("127.0.0.1:8080").await |
| 285 | + /// .expect("Couldn't connect to the server..."); |
| 286 | + /// stream.take_error().expect("No error was expected..."); |
| 287 | + /// |
| 288 | + /// # Ok(()) }) } |
| 289 | + /// ``` |
| 290 | + pub fn take_error(&self) -> io::Result<Option<io::Error>> { |
| 291 | + self.watcher.get_ref().take_error() |
| 292 | + } |
| 293 | + |
258 | 294 | /// Shuts down the read, write, or both halves of this connection.
|
259 | 295 | ///
|
260 | 296 | /// This method will cause all pending and future I/O on the specified portions to return
|
|
0 commit comments