Skip to main content

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::macros::rustdoc_skip_body;
6use crate::sys::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 impl(self) trait TcpStreamExt {
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    #[cfg_attr(
28        any(target_os = "linux", target_os = "android", target_os = "cygwin"),
29        doc = "```no_run"
30    )]
31    #[cfg_attr(
32        not(any(target_os = "linux", target_os = "android", target_os = "cygwin")),
33        doc = "```ignore (needs linux)"
34    )]
35    /// use std::net::TcpStream;
36    /// #[cfg(target_os = "linux")]
37    /// use std::os::linux::net::TcpStreamExt;
38    /// #[cfg(target_os = "android")]
39    /// use std::os::android::net::TcpStreamExt;
40    ///
41    /// let stream = TcpStream::connect("127.0.0.1:8080")
42    ///         .expect("Couldn't connect to the server...");
43    /// stream.set_quickack(true).expect("set_quickack call failed");
44    /// ```
45    #[stable(feature = "tcp_quickack", since = "1.89.0")]
46    fn set_quickack(&self, quickack: bool) -> io::Result<()>;
47
48    /// Gets the value of the `TCP_QUICKACK` option on this socket.
49    ///
50    /// For more information about this option, see [`TcpStreamExt::set_quickack`].
51    ///
52    /// # Examples
53    ///
54    #[cfg_attr(
55        any(target_os = "linux", target_os = "android", target_os = "cygwin"),
56        doc = "```no_run"
57    )]
58    #[cfg_attr(
59        not(any(target_os = "linux", target_os = "android", target_os = "cygwin")),
60        doc = "```ignore (needs linux)"
61    )]
62    /// use std::net::TcpStream;
63    /// #[cfg(target_os = "linux")]
64    /// use std::os::linux::net::TcpStreamExt;
65    /// #[cfg(target_os = "android")]
66    /// use std::os::android::net::TcpStreamExt;
67    ///
68    /// let stream = TcpStream::connect("127.0.0.1:8080")
69    ///         .expect("Couldn't connect to the server...");
70    /// stream.set_quickack(true).expect("set_quickack call failed");
71    /// assert_eq!(stream.quickack().unwrap_or(false), true);
72    /// ```
73    #[stable(feature = "tcp_quickack", since = "1.89.0")]
74    fn quickack(&self) -> io::Result<bool>;
75
76    /// A socket listener will be awakened solely when data arrives.
77    ///
78    /// The `accept` argument set the maximum delay until the
79    /// data is available to read, reducing the number of short lived
80    /// connections without data to process.
81    /// Contrary to other platforms `SO_ACCEPTFILTER` feature equivalent, there is
82    /// no necessity to set it after the `listen` call.
83    /// Note that the delay is expressed as Duration from user's perspective
84    /// the call rounds it down to the nearest second expressible as a `c_int`.
85    ///
86    /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
87    ///
88    /// # Examples
89    ///
90    /// ```no run
91    /// #![feature(tcp_deferaccept)]
92    /// use std::net::TcpStream;
93    /// use std::os::linux::net::TcpStreamExt;
94    /// use std::time::Duration;
95    ///
96    /// let stream = TcpStream::connect("127.0.0.1:8080")
97    ///         .expect("Couldn't connect to the server...");
98    /// stream.set_deferaccept(Duration::from_secs(1u64)).expect("set_deferaccept call failed");
99    /// ```
100    #[unstable(feature = "tcp_deferaccept", issue = "119639")]
101    #[cfg(target_os = "linux")]
102    fn set_deferaccept(&self, accept: Duration) -> io::Result<()>;
103
104    /// Gets the accept delay value of the `TCP_DEFER_ACCEPT` option.
105    ///
106    /// For more information about this option, see [`TcpStreamExt::set_deferaccept`].
107    ///
108    /// # Examples
109    ///
110    #[cfg_attr(
111        any(target_os = "linux", target_os = "android", target_os = "cygwin"),
112        doc = "```no_run"
113    )]
114    #[cfg_attr(
115        not(any(target_os = "linux", target_os = "android", target_os = "cygwin")),
116        doc = "```ignore (needs linux)"
117    )]
118    /// #![feature(tcp_deferaccept)]
119    /// use std::net::TcpStream;
120    /// use std::os::linux::net::TcpStreamExt;
121    /// use std::time::Duration;
122    ///
123    /// let stream = TcpStream::connect("127.0.0.1:8080")
124    ///         .expect("Couldn't connect to the server...");
125    /// stream.set_deferaccept(Duration::from_secs(1u64)).expect("set_deferaccept call failed");
126    /// assert_eq!(stream.deferaccept().unwrap(), Duration::from_secs(1u64));
127    /// ```
128    #[unstable(feature = "tcp_deferaccept", issue = "119639")]
129    #[cfg(target_os = "linux")]
130    fn deferaccept(&self) -> io::Result<Duration>;
131}
132
133#[stable(feature = "tcp_quickack", since = "1.89.0")]
134impl TcpStreamExt for net::TcpStream {
135    #[rustdoc_skip_body]
136    fn set_quickack(&self, quickack: bool) -> io::Result<()> {
137        self.as_inner().as_inner().set_quickack(quickack)
138    }
139
140    #[rustdoc_skip_body]
141    fn quickack(&self) -> io::Result<bool> {
142        self.as_inner().as_inner().quickack()
143    }
144
145    #[cfg(target_os = "linux")]
146    fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
147        self.as_inner().as_inner().set_deferaccept(accept)
148    }
149
150    #[cfg(target_os = "linux")]
151    fn deferaccept(&self) -> io::Result<Duration> {
152        self.as_inner().as_inner().deferaccept()
153    }
154}