std/os/net/linux_ext/
tcp.rs

1//! Linux and Android-specific tcp extensions to primitives in the [`std::net`] module.
2//!
3//! [`std::net`]: crate::net
4
5use crate::sealed::Sealed;
6use crate::sys_common::AsInner;
7#[cfg(target_os = "linux")]
8use crate::time::Duration;
9use crate::{io, net};
10
11/// Os-specific extensions for [`TcpStream`]
12///
13/// [`TcpStream`]: net::TcpStream
14#[stable(feature = "tcp_quickack", since = "1.89.0")]
15pub trait TcpStreamExt: Sealed {
16    /// Enable or disable `TCP_QUICKACK`.
17    ///
18    /// This flag causes Linux to eagerly send ACKs rather than delaying them.
19    /// Linux may reset this flag after further operations on the socket.
20    ///
21    /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html) and
22    /// [TCP delayed acknowledgement](https://en.wikipedia.org/wiki/TCP_delayed_acknowledgment)
23    /// for more information.
24    ///
25    /// # Examples
26    ///
27    /// ```no_run
28    /// use std::net::TcpStream;
29    /// #[cfg(target_os = "linux")]
30    /// use std::os::linux::net::TcpStreamExt;
31    /// #[cfg(target_os = "android")]
32    /// use std::os::android::net::TcpStreamExt;
33    ///
34    /// let stream = TcpStream::connect("127.0.0.1:8080")
35    ///         .expect("Couldn't connect to the server...");
36    /// stream.set_quickack(true).expect("set_quickack call failed");
37    /// ```
38    #[stable(feature = "tcp_quickack", since = "1.89.0")]
39    fn set_quickack(&self, quickack: bool) -> io::Result<()>;
40
41    /// Gets the value of the `TCP_QUICKACK` option on this socket.
42    ///
43    /// For more information about this option, see [`TcpStreamExt::set_quickack`].
44    ///
45    /// # Examples
46    ///
47    /// ```no_run
48    /// use std::net::TcpStream;
49    /// #[cfg(target_os = "linux")]
50    /// use std::os::linux::net::TcpStreamExt;
51    /// #[cfg(target_os = "android")]
52    /// use std::os::android::net::TcpStreamExt;
53    ///
54    /// let stream = TcpStream::connect("127.0.0.1:8080")
55    ///         .expect("Couldn't connect to the server...");
56    /// stream.set_quickack(true).expect("set_quickack call failed");
57    /// assert_eq!(stream.quickack().unwrap_or(false), true);
58    /// ```
59    #[stable(feature = "tcp_quickack", since = "1.89.0")]
60    fn quickack(&self) -> io::Result<bool>;
61
62    /// A socket listener will be awakened solely when data arrives.
63    ///
64    /// The `accept` argument set the maximum delay until the
65    /// data is available to read, reducing the number of short lived
66    /// connections without data to process.
67    /// Contrary to other platforms `SO_ACCEPTFILTER` feature equivalent, there is
68    /// no necessity to set it after the `listen` call.
69    /// Note that the delay is expressed as Duration from user's perspective
70    /// the call rounds it down to the nearest second expressible as a `c_int`.
71    ///
72    /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
73    ///
74    /// # Examples
75    ///
76    /// ```no run
77    /// #![feature(tcp_deferaccept)]
78    /// use std::net::TcpStream;
79    /// use std::os::linux::net::TcpStreamExt;
80    /// use std::time::Duration;
81    ///
82    /// let stream = TcpStream::connect("127.0.0.1:8080")
83    ///         .expect("Couldn't connect to the server...");
84    /// stream.set_deferaccept(Duration::from_secs(1u64)).expect("set_deferaccept call failed");
85    /// ```
86    #[unstable(feature = "tcp_deferaccept", issue = "119639")]
87    #[cfg(target_os = "linux")]
88    fn set_deferaccept(&self, accept: Duration) -> io::Result<()>;
89
90    /// Gets the accept delay value of the `TCP_DEFER_ACCEPT` option.
91    ///
92    /// For more information about this option, see [`TcpStreamExt::set_deferaccept`].
93    ///
94    /// # Examples
95    ///
96    /// ```no_run
97    /// #![feature(tcp_deferaccept)]
98    /// use std::net::TcpStream;
99    /// use std::os::linux::net::TcpStreamExt;
100    /// use std::time::Duration;
101    ///
102    /// let stream = TcpStream::connect("127.0.0.1:8080")
103    ///         .expect("Couldn't connect to the server...");
104    /// stream.set_deferaccept(Duration::from_secs(1u64)).expect("set_deferaccept call failed");
105    /// assert_eq!(stream.deferaccept().unwrap(), Duration::from_secs(1u64));
106    /// ```
107    #[unstable(feature = "tcp_deferaccept", issue = "119639")]
108    #[cfg(target_os = "linux")]
109    fn deferaccept(&self) -> io::Result<Duration>;
110}
111
112#[stable(feature = "tcp_quickack", since = "1.89.0")]
113impl Sealed for net::TcpStream {}
114
115#[stable(feature = "tcp_quickack", since = "1.89.0")]
116impl TcpStreamExt for net::TcpStream {
117    fn set_quickack(&self, quickack: bool) -> io::Result<()> {
118        self.as_inner().as_inner().set_quickack(quickack)
119    }
120
121    fn quickack(&self) -> io::Result<bool> {
122        self.as_inner().as_inner().quickack()
123    }
124
125    #[cfg(target_os = "linux")]
126    fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
127        self.as_inner().as_inner().set_deferaccept(accept)
128    }
129
130    #[cfg(target_os = "linux")]
131    fn deferaccept(&self) -> io::Result<Duration> {
132        self.as_inner().as_inner().deferaccept()
133    }
134}