Skip to content

Commit 3ca57f3

Browse files
committed
Document unconnected TcpStream returned by TcpStream::connect
1 parent 47cf59c commit 3ca57f3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/net/tcp/stream.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ pub struct TcpStream {
4949
impl TcpStream {
5050
/// Create a new TCP stream and issue a non-blocking connect to the
5151
/// specified address.
52+
///
53+
/// # Notes
54+
///
55+
/// The returned `TcpStream` may not be connected (and thus usable), unlike
56+
/// the API found in `std::net::TcpStream`. Because Mio issues a
57+
/// *non-blocking* connect it will not block the thread and instead return
58+
/// an unconnected `TcpStream`.
59+
///
60+
/// Ensuring the returned stream is connected is surprisingly complex when
61+
/// considering cross-platform support. Doing this properly should follow
62+
/// the steps below, an example implementation can be found
63+
/// [here](https://github.com/Thomasdezeeuw/heph/blob/0c4f1ab3eaf08bea1d65776528bfd6114c9f8374/src/net/tcp/stream.rs#L560-L622).
64+
///
65+
/// 1. Call `TcpStream::connect`
66+
/// 2. Register the returned stream with at least [read interest].
67+
/// 3. Wait for a (readable) event.
68+
/// 4. Check `TcpStream::peer_addr`. If it returns `libc::EINPROGRESS` or
69+
/// `ErrorKind::NotConnected` it means the stream is not yet connected,
70+
/// go back to step 3. If it returns an address it means the stream is
71+
/// connected, go to step 5. If another error is returned something
72+
/// whent wrong.
73+
/// 5. Now the stream can be used.
74+
///
75+
/// [read interest]: Interest::READABLE
5276
pub fn connect(addr: SocketAddr) -> io::Result<TcpStream> {
5377
let socket = new_for_addr(addr)?;
5478
#[cfg(unix)]

0 commit comments

Comments
 (0)