use std::net::SocketAddr;
use socket2::{Domain, Protocol, Socket, Type};
pub fn reuseport_udp(addr: SocketAddr) -> std::io::Result<std::net::UdpSocket> {
let sock = Socket::new(Domain::for_address(addr), Type::DGRAM, Some(Protocol::UDP))?;
sock.set_reuse_address(true)?;
#[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
sock.set_reuse_port(true)?;
sock.set_nonblocking(true)?;
sock.bind(&addr.into())?;
Ok(sock.into())
}
pub fn reuseport_tcp(addr: SocketAddr, backlog: i32) -> std::io::Result<std::net::TcpListener> {
let sock = Socket::new(Domain::for_address(addr), Type::STREAM, Some(Protocol::TCP))?;
sock.set_reuse_address(true)?;
#[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
sock.set_reuse_port(true)?;
sock.set_nonblocking(true)?;
sock.bind(&addr.into())?;
sock.listen(backlog)?;
Ok(sock.into())
}