diff options
Diffstat (limited to 'libs/hbb_common/src/tcp.rs')
-rw-r--r-- | libs/hbb_common/src/tcp.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/libs/hbb_common/src/tcp.rs b/libs/hbb_common/src/tcp.rs index f574e83..046d901 100644 --- a/libs/hbb_common/src/tcp.rs +++ b/libs/hbb_common/src/tcp.rs @@ -260,8 +260,16 @@ pub async fn new_listener<T: ToSocketAddrs>(addr: T, reuse: bool) -> ResultType< } } -pub async fn listen_any(port: u16) -> ResultType<TcpListener> { +pub async fn listen_any(port: u16, reuse: bool) -> ResultType<TcpListener> { if let Ok(mut socket) = TcpSocket::new_v6() { + if reuse { + // windows has no reuse_port, but it's reuse_address + // almost equals to unix's reuse_port + reuse_address, + // though may introduce nondeterministic behavior + #[cfg(unix)] + socket.set_reuseport(true).ok(); + socket.set_reuseaddr(true).ok(); + } #[cfg(unix)] { use std::os::unix::io::{FromRawFd, IntoRawFd}; |