From 00c096d5364ef2e8cec918dab44e28a7d8c439f4 Mon Sep 17 00:00:00 2001 From: Marcin Anforowicz Date: Fri, 7 Jun 2024 09:29:47 -0700 Subject: [PATCH] Changed socket usage to support windows --- gday_hole_punch/src/hole_puncher.rs | 8 +++++--- gday_hole_punch/src/server_connector.rs | 5 ++++- gday_server/src/main.rs | 3 +-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/gday_hole_punch/src/hole_puncher.rs b/gday_hole_punch/src/hole_puncher.rs index be56e91..5eacc64 100644 --- a/gday_hole_punch/src/hole_puncher.rs +++ b/gday_hole_punch/src/hole_puncher.rs @@ -228,12 +228,14 @@ fn get_local_socket(local_addr: SocketAddr) -> std::io::Result { let sock = SockRef::from(&socket); let _ = sock.set_reuse_address(true); + + // socket2 only supports this method on these systems + #[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))] let _ = sock.set_reuse_port(true); let keepalive = TcpKeepalive::new() - .with_time(Duration::from_secs(5)) - .with_interval(Duration::from_secs(2)) - .with_retries(5); + .with_time(Duration::from_secs(60)) + .with_interval(Duration::from_secs(10)); let _ = sock.set_tcp_keepalive(&keepalive); socket.bind(local_addr)?; diff --git a/gday_hole_punch/src/server_connector.rs b/gday_hole_punch/src/server_connector.rs index 4c4b90c..30f2e0f 100644 --- a/gday_hole_punch/src/server_connector.rs +++ b/gday_hole_punch/src/server_connector.rs @@ -85,13 +85,16 @@ impl ServerStream { /// So that this socket can be reused for /// hole-punching. fn enable_reuse(&self) { - let stream = match self { + let stream: &TcpStream = match self { Self::TCP(stream) => stream, Self::TLS(stream) => stream.get_ref(), }; let sock = SockRef::from(stream); let _ = sock.set_reuse_address(true); + + // socket2 only supports this method on these systems + #[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))] let _ = sock.set_reuse_port(true); } } diff --git a/gday_server/src/main.rs b/gday_server/src/main.rs index aafaef2..22f217e 100644 --- a/gday_server/src/main.rs +++ b/gday_server/src/main.rs @@ -139,8 +139,7 @@ async fn get_tcp_listener(addr: impl ToSocketAddrs + Display) -> TcpListener { // sets the keepalive to 10 minutes let tcp_keepalive = TcpKeepalive::new() .with_time(Duration::from_secs(600)) - .with_interval(Duration::from_secs(10)) - .with_retries(3); + .with_interval(Duration::from_secs(10)); let socket = SockRef::from(&listener); socket .set_tcp_keepalive(&tcp_keepalive)