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;
7use crate::{io, net};
8
9/// Os-specific extensions for [`TcpStream`]
10///
11/// [`TcpStream`]: net::TcpStream
12#[unstable(feature = "tcp_quickack", issue = "96256")]
13pub trait TcpStreamExt: Sealed {
14    /// Enable or disable `TCP_QUICKACK`.
15    ///
16    /// This flag causes Linux to eagerly send ACKs rather than delaying them.
17    /// Linux may reset this flag after further operations on the socket.
18    ///
19    /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html) and
20    /// [TCP delayed acknowledgement](https://en.wikipedia.org/wiki/TCP_delayed_acknowledgment)
21    /// for more information.
22    ///
23    /// # Examples
24    ///
25    /// ```no_run
26    /// #![feature(tcp_quickack)]
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    #[unstable(feature = "tcp_quickack", issue = "96256")]
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    /// #![feature(tcp_quickack)]
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    #[unstable(feature = "tcp_quickack", issue = "96256")]
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 delay in seconds 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    ///
70    /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
71    ///
72    /// # Examples
73    ///
74    /// ```no run
75    /// #![feature(tcp_deferaccept)]
76    /// use std::net::TcpStream;
77    /// use std::os::linux::net::TcpStreamExt;
78    ///
79    /// let stream = TcpStream::connect("127.0.0.1:8080")
80    ///         .expect("Couldn't connect to the server...");
81    /// stream.set_deferaccept(1).expect("set_deferaccept call failed");
82    /// ```
83    #[unstable(feature = "tcp_deferaccept", issue = "119639")]
84    #[cfg(target_os = "linux")]
85    fn set_deferaccept(&self, accept: u32) -> io::Result<()>;
86
87    /// Gets the accept delay value (in seconds) of the `TCP_DEFER_ACCEPT` option.
88    ///
89    /// For more information about this option, see [`TcpStreamExt::set_deferaccept`].
90    ///
91    /// # Examples
92    ///
93    /// ```no_run
94    /// #![feature(tcp_deferaccept)]
95    /// use std::net::TcpStream;
96    /// use std::os::linux::net::TcpStreamExt;
97    ///
98    /// let stream = TcpStream::connect("127.0.0.1:8080")
99    ///         .expect("Couldn't connect to the server...");
100    /// stream.set_deferaccept(1).expect("set_deferaccept call failed");
101    /// assert_eq!(stream.deferaccept().unwrap_or(0), 1);
102    /// ```
103    #[unstable(feature = "tcp_deferaccept", issue = "119639")]
104    #[cfg(target_os = "linux")]
105    fn deferaccept(&self) -> io::Result<u32>;
106}
107
108#[unstable(feature = "tcp_quickack", issue = "96256")]
109impl Sealed for net::TcpStream {}
110
111#[unstable(feature = "tcp_quickack", issue = "96256")]
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: u32) -> io::Result<()> {
123        self.as_inner().as_inner().set_deferaccept(accept)
124    }
125
126    #[cfg(target_os = "linux")]
127    fn deferaccept(&self) -> io::Result<u32> {
128        self.as_inner().as_inner().deferaccept()
129    }
130}