Skip to content

Commit

Permalink
fixed windows TFO by using tokio try_write_io
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Jul 7, 2021
1 parent 0f942ea commit 250aa95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crates/shadowsocks-service/src/local/dns/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ impl DnsClient {
fn check_peekable<F: std::os::windows::io::AsRawSocket>(s: &mut F) -> bool {
use winapi::{
ctypes::{c_char, c_int},
um::winsock2::{recv, MSG_PEEK},
um::winsock2::{recv, MSG_PEEK, SOCKET},
};

let sock = s.as_raw_socket();
let sock = s.as_raw_socket() as SOCKET;

unsafe {
let mut peek_buf = [0u8; 1];
Expand Down
20 changes: 15 additions & 5 deletions crates/shadowsocks/src/net/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ impl AsyncWrite for TcpStream {
TcpStreamState::FastOpenConnecting(ref mut overlapped) => {
let stream = inner.get_mut();

let n = ready!(stream.poll_write_io(cx, || {
ready!(stream.poll_write_ready(cx))?;

let write_result = stream.try_write_io(|| {
unsafe {
let sock = stream.as_raw_socket() as SOCKET;

Expand Down Expand Up @@ -320,11 +322,19 @@ impl AsyncWrite for TcpStream {
Err(io::Error::from_raw_os_error(err))
}
}
}))?;
});

// Connect successfully with fast open
*state = TcpStreamState::Connected;
return Ok(n).into();
match write_result {
Ok(n) => {
// Connect successfully with fast open
*state = TcpStreamState::Connected;
return Ok(n).into();
}
Err(ref err) if err.kind() == ErrorKind::WouldBlock => {
// Wait again for writable event.
}
Err(err) => return Err(err).into(),
}
}
}
}
Expand Down

0 comments on commit 250aa95

Please sign in to comment.