Call take_error inside the wait_connect loop

This commit is contained in:
Stjepan Glavina 2020-05-22 20:22:41 +02:00
parent 702e1f8097
commit 7ac8969f45
1 changed files with 14 additions and 34 deletions

View File

@ -693,34 +693,17 @@ impl Async<TcpStream> {
// Waits for connect to complete.
let wait_connect = |mut stream: &TcpStream| match stream.write(&[]) {
Err(err) if err.kind() == io::ErrorKind::NotConnected => {
// On other systems, if a non-blocking connect call fails, a
// sensible error code is returned by the send (write) call.
//
// But not on Windows. If a non-blocking connect call fails, the
// send call always returns NotConnected. We have to use
// take_error (i.e., getsockopt SO_ERROR) to find out whether
// the connect call has failed, and retrieve the error code if
// it has.
#[cfg(windows)]
{
if let Some(e) = stream.take_error()? {
return Err(e);
}
}
Err(io::ErrorKind::WouldBlock.into())
}
Err(err) if err.kind() == io::ErrorKind::NotConnected => match stream.take_error()? {
Some(err) => Err(err),
None => Err(io::ErrorKind::WouldBlock.into()),
},
res => res.map(|_| ()),
};
// The stream becomes writable when connected.
match stream.write_with(|io| wait_connect(io)).await {
Ok(()) => Ok(stream),
Err(err) => match stream.get_ref().take_error() {
Ok(Some(err)) => Err(err),
Ok(None) | Err(_) => Err(err),
},
}
stream.write_with(|io| wait_connect(io)).await?;
Ok(stream)
}
/// Reads data from the stream without removing it from the buffer.
@ -1029,20 +1012,17 @@ impl Async<UnixStream> {
// Waits for connect to complete.
let wait_connect = |mut stream: &UnixStream| match stream.write(&[]) {
Err(err) if err.kind() == io::ErrorKind::NotConnected => {
Err(io::ErrorKind::WouldBlock.into())
}
Err(err) if err.kind() == io::ErrorKind::NotConnected => match stream.take_error()? {
Some(err) => Err(err),
None => Err(io::ErrorKind::WouldBlock.into()),
},
res => res.map(|_| ()),
};
// The stream becomes writable when connected.
match stream.write_with(|io| wait_connect(io)).await {
Ok(()) => Ok(stream),
Err(err) => match stream.get_ref().take_error() {
Ok(Some(err)) => Err(err),
Ok(None) | Err(_) => Err(err),
},
}
stream.write_with(|io| wait_connect(io)).await?;
Ok(stream)
}
/// Creates an unnamed pair of connected UDS stream sockets.