clean up socket2 networkwork code to do more on the socket2 before convertig it.

This commit is contained in:
Marcel Märtens 2022-02-05 13:05:44 +01:00
parent 2528afafdd
commit 97a58f0e14

View File

@ -95,19 +95,20 @@ impl Protocols {
c2s_protocol_s: mpsc::UnboundedSender<(Self, Cid)>,
) -> std::io::Result<()> {
use socket2::{Domain, Socket, Type};
use tokio::net::TcpSocket;
let domain = Domain::for_address(addr);
let socket2_socket = Socket::new(domain, Type::STREAM, None)?;
if domain == Domain::IPV6 {
socket2_socket.set_only_v6(true)?
}
socket2_socket.set_nonblocking(true)?; //needed by tokio
let socket = TcpSocket::from_std_stream(socket2_socket.into());
socket2_socket.set_nonblocking(true)?; // Needed by Tokio
// See https://docs.rs/tokio/latest/tokio/net/struct.TcpSocket.html
#[cfg(not(windows))]
socket.set_reuseaddr(true)?;
socket.bind(addr)?;
let listener = socket.listen(1024)?;
socket2_socket.set_reuse_address(true)?;
let socket2_addr = addr.into();
socket2_socket.bind(&socket2_addr)?;
socket2_socket.listen(1024)?;
let std_listener: std::net::TcpListener = socket2_socket.into();
let listener = tokio::net::TcpListener::from_std(std_listener)?;
trace!(?addr, "Tcp Listener bound");
let mut end_receiver = s2s_stop_listening_r.fuse();
tokio::spawn(async move {