From ec4abf421a13b7b6bd4ebf848e99a18e830b611e Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 18 Feb 2020 12:55:18 +0100 Subject: [PATCH] Disable NetSocket reuse address on Windows. It actually means reuse port -.- ... (cherry picked from commits cae0d8853d7a373ad8720289c12c7c2e7b5ef240 and 19ef28a61494d06a001c983a50e5a33bb0c7615b) --- drivers/unix/net_socket_posix.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/unix/net_socket_posix.cpp b/drivers/unix/net_socket_posix.cpp index cab5513e0a..545c9f26f5 100644 --- a/drivers/unix/net_socket_posix.cpp +++ b/drivers/unix/net_socket_posix.cpp @@ -673,22 +673,27 @@ void NetSocketPosix::set_tcp_no_delay_enabled(bool p_enabled) { void NetSocketPosix::set_reuse_address_enabled(bool p_enabled) { ERR_FAIL_COND(!is_open()); +// On Windows, enabling SO_REUSEADDR actually would also enable reuse port, very bad on TCP. Denying... +// Windows does not have this option, SO_REUSEADDR in this magical world means SO_REUSEPORT +#ifndef WINDOWS_ENABLED int par = p_enabled ? 1 : 0; if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, SOCK_CBUF(&par), sizeof(int)) < 0) { WARN_PRINT("Unable to set socket REUSEADDR option!"); } +#endif } void NetSocketPosix::set_reuse_port_enabled(bool p_enabled) { -// Windows does not have this option, as it is always ON when setting REUSEADDR. -#ifndef WINDOWS_ENABLED ERR_FAIL_COND(!is_open()); +// See comment above... +#ifdef WINDOWS_ENABLED +#define SO_REUSEPORT SO_REUSEADDR +#endif int par = p_enabled ? 1 : 0; if (setsockopt(_sock, SOL_SOCKET, SO_REUSEPORT, SOCK_CBUF(&par), sizeof(int)) < 0) { WARN_PRINT("Unable to set socket REUSEPORT option!"); } -#endif } bool NetSocketPosix::is_open() const {