mirror of https://github.com/stjepang/smol
Call take_error inside the wait_connect loop
This commit is contained in:
parent
702e1f8097
commit
7ac8969f45
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue