Skip to content

Commit 1ea4091

Browse files
keithmattixThomasdezeeuw
authored andcommitted
Add TCP retries to Windows
1 parent 2446f48 commit 1ea4091

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@ pub struct TcpKeepalive {
441441
target_os = "openbsd",
442442
target_os = "redox",
443443
target_os = "solaris",
444-
target_os = "windows",
445444
target_os = "nto",
446445
target_os = "espidf",
447446
target_os = "vita",
@@ -470,7 +469,6 @@ impl TcpKeepalive {
470469
target_os = "openbsd",
471470
target_os = "redox",
472471
target_os = "solaris",
473-
target_os = "windows",
474472
target_os = "nto",
475473
target_os = "espidf",
476474
target_os = "vita",
@@ -548,6 +546,7 @@ impl TcpKeepalive {
548546
target_os = "tvos",
549547
target_os = "watchos",
550548
target_os = "cygwin",
549+
target_os = "windows",
551550
)
552551
))]
553552
pub const fn with_retries(self, retries: u32) -> Self {

src/socket.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,7 @@ impl Socket {
21162116
target_os = "tvos",
21172117
target_os = "watchos",
21182118
target_os = "cygwin",
2119+
target_os = "windows",
21192120
)
21202121
))]
21212122
pub fn keepalive_retries(&self) -> io::Result<u32> {

src/sys/windows.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ pub(crate) const SOCK_SEQPACKET: c_int =
5959
pub(crate) use windows_sys::Win32::Networking::WinSock::{
6060
IPPROTO_ICMP, IPPROTO_ICMPV6, IPPROTO_TCP, IPPROTO_UDP,
6161
};
62+
63+
pub(crate) use windows_sys::Win32::Networking::WinSock::TCP_KEEPCNT;
6264
// Used in `SockAddr`.
6365
pub(crate) use windows_sys::Win32::Networking::WinSock::{
6466
SOCKADDR as sockaddr, SOCKADDR_IN as sockaddr_in, SOCKADDR_IN6 as sockaddr_in6,
@@ -737,17 +739,18 @@ fn into_ms(duration: Option<Duration>) -> u32 {
737739
}
738740

739741
pub(crate) fn set_tcp_keepalive(socket: Socket, keepalive: &TcpKeepalive) -> io::Result<()> {
740-
let mut keepalive = tcp_keepalive {
742+
let mut tcp_keepalive = tcp_keepalive {
741743
onoff: 1,
742744
keepalivetime: into_ms(keepalive.time),
743745
keepaliveinterval: into_ms(keepalive.interval),
744746
};
747+
745748
let mut out = 0;
746749
syscall!(
747750
WSAIoctl(
748751
socket,
749752
SIO_KEEPALIVE_VALS,
750-
&mut keepalive as *mut _ as *mut _,
753+
&mut tcp_keepalive as *mut _ as *mut _,
751754
size_of::<tcp_keepalive>() as _,
752755
ptr::null_mut(),
753756
0,
@@ -757,8 +760,18 @@ pub(crate) fn set_tcp_keepalive(socket: Socket, keepalive: &TcpKeepalive) -> io:
757760
),
758761
PartialEq::eq,
759762
SOCKET_ERROR
760-
)
761-
.map(|_| ())
763+
)?;
764+
if let Some(retries) = keepalive.retries {
765+
unsafe {
766+
setsockopt(
767+
socket,
768+
WinSock::IPPROTO_TCP,
769+
WinSock::TCP_KEEPCNT,
770+
retries as c_int,
771+
)?
772+
}
773+
}
774+
Ok(())
762775
}
763776

764777
/// Caller must ensure `T` is the correct type for `level` and `optname`.

tests/socket.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ fn tcp_keepalive() {
900900
target_os = "netbsd",
901901
target_os = "tvos",
902902
target_os = "watchos",
903+
target_os = "windows"
903904
)
904905
))]
905906
let params = params.with_retries(10);
@@ -950,6 +951,7 @@ fn tcp_keepalive() {
950951
target_os = "netbsd",
951952
target_os = "tvos",
952953
target_os = "watchos",
954+
target_os = "windows",
953955
)
954956
))]
955957
assert_eq!(socket.keepalive_retries().unwrap(), 10);

0 commit comments

Comments
 (0)