diff --git a/compio-net/src/opts.rs b/compio-net/src/opts.rs index 9c760a3b..db742bc1 100644 --- a/compio-net/src/opts.rs +++ b/compio-net/src/opts.rs @@ -1,36 +1,22 @@ +use std::time::Duration; + use crate::Socket; /// Options for configuring TCP sockets. /// By default, SO_REUSEADDR is enabled. -#[derive(Copy, Clone)] +#[derive(Default, Debug, Copy, Clone)] pub struct TcpOpts { recv_buffer_size: Option, send_buffer_size: Option, keepalive: bool, - linger: Option, - read_timeout: Option, - write_timeout: Option, + linger: Option, + read_timeout: Option, + write_timeout: Option, reuse_address: bool, reuse_port: bool, no_delay: bool, } -impl Default for TcpOpts { - fn default() -> Self { - Self { - recv_buffer_size: None, - send_buffer_size: None, - keepalive: false, - linger: None, - read_timeout: None, - write_timeout: None, - reuse_address: true, - reuse_port: false, - no_delay: false, - } - } -} - impl TcpOpts { /// Creates a new `TcpOpts` with default settings. pub fn new() -> Self { @@ -38,55 +24,55 @@ impl TcpOpts { } /// Sets the receive buffer size for the TCP socket. - pub fn set_recv_buffer_size(mut self, size: usize) -> Self { + pub fn recv_buffer_size(mut self, size: usize) -> Self { self.recv_buffer_size = Some(size); self } /// Sets the send buffer size for the TCP socket. - pub fn set_send_buffer_size(mut self, size: usize) -> Self { + pub fn send_buffer_size(mut self, size: usize) -> Self { self.send_buffer_size = Some(size); self } /// Enables or disables the TCP keepalive option. - pub fn set_keepalive(mut self, keepalive: bool) -> Self { + pub fn keepalive(mut self, keepalive: bool) -> Self { self.keepalive = keepalive; self } /// Sets the linger duration for the TCP socket. - pub fn set_linger(mut self, duration: std::time::Duration) -> Self { + pub fn linger(mut self, duration: Duration) -> Self { self.linger = Some(duration); self } /// Sets the read timeout for the TCP socket. - pub fn set_read_timeout(mut self, duration: std::time::Duration) -> Self { + pub fn read_timeout(mut self, duration: Duration) -> Self { self.read_timeout = Some(duration); self } /// Sets the write timeout for the TCP socket. - pub fn set_write_timeout(mut self, duration: std::time::Duration) -> Self { + pub fn write_timeout(mut self, duration: Duration) -> Self { self.write_timeout = Some(duration); self } /// Sets whether the TCP socket should reuse the address. - pub fn set_reuse_address(mut self, reuse: bool) -> Self { + pub fn reuse_address(mut self, reuse: bool) -> Self { self.reuse_address = reuse; self } /// Sets whether the TCP socket should reuse the port. - pub fn set_reuse_port(mut self, reuse: bool) -> Self { + pub fn reuse_port(mut self, reuse: bool) -> Self { self.reuse_port = reuse; self } /// Sets whether the TCP socket should disable Nagle's algorithm (no delay). - pub fn set_no_delay(mut self, no_delay: bool) -> Self { + pub fn no_delay(mut self, no_delay: bool) -> Self { self.no_delay = no_delay; self } diff --git a/compio-net/src/tcp.rs b/compio-net/src/tcp.rs index d2557a81..72e47643 100644 --- a/compio-net/src/tcp.rs +++ b/compio-net/src/tcp.rs @@ -56,7 +56,7 @@ impl TcpListener { /// Binding with a port number of 0 will request that the OS assigns a port /// to this listener. pub async fn bind(addr: impl ToSocketAddrsAsync) -> io::Result { - Self::bind_base(addr, None).await + Self::bind_with_options(addr, TcpOpts::default().reuse_address(true)).await } /// Creates a new `TcpListener`, which will be bound to the specified @@ -70,14 +70,6 @@ impl TcpListener { addr: impl ToSocketAddrsAsync, options: TcpOpts, ) -> io::Result { - Self::bind_base(addr, Some(options)).await - } - - async fn bind_base( - addr: impl ToSocketAddrsAsync, - options: Option, - ) -> io::Result { - let options = options.unwrap_or_default(); super::each_addr(addr, |addr| async move { let sa = SockAddr::from(addr); let socket = Socket::new(sa.domain(), Type::STREAM, Some(Protocol::TCP)).await?; @@ -191,7 +183,6 @@ impl TcpStream { ) -> io::Result { use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; - let options = options.unwrap_or_default(); super::each_addr(addr, |addr| async move { let addr2 = SockAddr::from(addr); let socket = if cfg!(windows) { @@ -209,7 +200,9 @@ impl TcpStream { } else { Socket::new(addr2.domain(), Type::STREAM, Some(Protocol::TCP)).await? }; - options.setup_socket(&socket)?; + if let Some(options) = &options { + options.setup_socket(&socket)?; + } socket.connect_async(&addr2).await?; Ok(Self { inner: socket }) })