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 42b55f1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 15 additions & 22 deletions crates/shadowsocks-service/src/local/dns/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg(unix)]
use std::path::Path;
use std::{
cmp::Ordering,
io::{self, ErrorKind},
net::SocketAddr,
sync::Arc,
Expand Down Expand Up @@ -170,20 +171,16 @@ impl DnsClient {
libc::MSG_PEEK | libc::MSG_DONTWAIT,
);

if ret == 0 {
match ret.cmp(&0) {
// EOF, connection lost
false
} else if ret > 0 {
Ordering::Equal => false,
// Data in buffer
true
} else {
let err = io::Error::last_os_error();
if err.kind() == ErrorKind::WouldBlock {
Ordering::Greater => true,
Ordering::Less => {
let err = io::Error::last_os_error();
// EAGAIN, EWOULDBLOCK
// Still connected.
true
} else {
false
err.kind() == ErrorKind::WouldBlock
}
}
}
Expand All @@ -193,10 +190,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 All @@ -208,20 +205,16 @@ impl DnsClient {
MSG_PEEK,
);

if ret == 0 {
match ret.cmp(&0) {
// EOF, connection lost
false
} else if ret > 0 {
Ordering::Equal => false,
// Data in buffer
true
} else {
let err = io::Error::last_os_error();
if err.kind() == ErrorKind::WouldBlock {
Ordering::Greater => true,
Ordering::Less => {
let err = io::Error::last_os_error();
// I have to trust the `s` have already set to non-blocking mode
// Becuase windows doesn't have MSG_DONTWAIT
true
} else {
false
err.kind() == ErrorKind::WouldBlock
}
}
}
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 42b55f1

Please sign in to comment.