reuseaddr in listen_any

This commit is contained in:
rustdesk 2023-06-27 15:20:32 +08:00
parent 22558b5775
commit 56eac7294c
2 changed files with 11 additions and 7 deletions

View File

@ -72,8 +72,8 @@ fn new_socket(addr: std::net::SocketAddr, reuse: bool) -> Result<TcpSocket, std:
// almost equals to unix's reuse_port + reuse_address, // almost equals to unix's reuse_port + reuse_address,
// though may introduce nondeterministic behavior // though may introduce nondeterministic behavior
#[cfg(unix)] #[cfg(unix)]
socket.set_reuseport(true)?; socket.set_reuseport(true).ok();
socket.set_reuseaddr(true)?; socket.set_reuseaddr(true).ok();
} }
socket.bind(addr)?; socket.bind(addr)?;
Ok(socket) Ok(socket)
@ -260,6 +260,8 @@ pub async fn listen_any(port: u16) -> ResultType<TcpListener> {
if let Ok(mut socket) = TcpSocket::new_v6() { if let Ok(mut socket) = TcpSocket::new_v6() {
#[cfg(unix)] #[cfg(unix)]
{ {
socket.set_reuseport(true).ok();
socket.set_reuseaddr(true).ok();
use std::os::unix::io::{FromRawFd, IntoRawFd}; use std::os::unix::io::{FromRawFd, IntoRawFd};
let raw_fd = socket.into_raw_fd(); let raw_fd = socket.into_raw_fd();
let sock2 = unsafe { socket2::Socket::from_raw_fd(raw_fd) }; let sock2 = unsafe { socket2::Socket::from_raw_fd(raw_fd) };
@ -283,9 +285,11 @@ pub async fn listen_any(port: u16) -> ResultType<TcpListener> {
} }
} }
} }
let s = TcpSocket::new_v4()?; Ok(new_socket(
s.bind(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), port))?; SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), port),
Ok(s.listen(DEFAULT_BACKLOG)?) true,
)?
.listen(DEFAULT_BACKLOG)?)
} }
impl Unpin for DynTcpStream {} impl Unpin for DynTcpStream {}

View File

@ -24,8 +24,8 @@ fn new_socket(addr: SocketAddr, reuse: bool, buf_size: usize) -> Result<Socket,
// almost equals to unix's reuse_port + reuse_address, // almost equals to unix's reuse_port + reuse_address,
// though may introduce nondeterministic behavior // though may introduce nondeterministic behavior
#[cfg(unix)] #[cfg(unix)]
socket.set_reuse_port(true)?; socket.set_reuse_port(true).ok();
socket.set_reuse_address(true)?; socket.set_reuse_address(true).ok();
} }
// only nonblocking work with tokio, https://stackoverflow.com/questions/64649405/receiver-on-tokiompscchannel-only-receives-messages-when-buffer-is-full // only nonblocking work with tokio, https://stackoverflow.com/questions/64649405/receiver-on-tokiompscchannel-only-receives-messages-when-buffer-is-full
socket.set_nonblocking(true)?; socket.set_nonblocking(true)?;