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