From b2422cef0de6c7fe8001bfb81eb4534757f3fdb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Wed, 2 Jul 2025 18:59:15 +0800 Subject: [PATCH 1/2] fix(net): don't call setup_socket if possible --- compio-net/src/opts.rs | 18 +----------------- compio-net/src/tcp.rs | 15 ++++----------- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/compio-net/src/opts.rs b/compio-net/src/opts.rs index 9c760a3b..5c40c82d 100644 --- a/compio-net/src/opts.rs +++ b/compio-net/src/opts.rs @@ -2,7 +2,7 @@ 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, @@ -15,22 +15,6 @@ pub struct TcpOpts { 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 { diff --git a/compio-net/src/tcp.rs b/compio-net/src/tcp.rs index d2557a81..cd301b8e 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().set_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 }) }) From 4cf08322925c312da10a2bb8c8cef6847a9d0b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Wed, 2 Jul 2025 19:01:29 +0800 Subject: [PATCH 2/2] feat(net): rename opts method names --- compio-net/src/opts.rs | 26 ++++++++++++++------------ compio-net/src/tcp.rs | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/compio-net/src/opts.rs b/compio-net/src/opts.rs index 5c40c82d..db742bc1 100644 --- a/compio-net/src/opts.rs +++ b/compio-net/src/opts.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use crate::Socket; /// Options for configuring TCP sockets. @@ -7,9 +9,9 @@ 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, @@ -22,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 cd301b8e..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_with_options(addr, TcpOpts::default().set_reuse_address(true)).await + Self::bind_with_options(addr, TcpOpts::default().reuse_address(true)).await } /// Creates a new `TcpListener`, which will be bound to the specified