monoio/net/
listener_config.rs

1/// Custom listener options
2#[derive(Debug, Clone, Copy)]
3#[non_exhaustive]
4pub struct ListenerOpts {
5    /// Whether to enable reuse_port.
6    pub reuse_port: bool,
7    /// Whether to enable reuse_addr.
8    pub reuse_addr: bool,
9    /// Backlog size.
10    pub backlog: i32,
11    /// Send buffer size or None to use default.
12    pub send_buf_size: Option<usize>,
13    /// Recv buffer size or None to use default.
14    pub recv_buf_size: Option<usize>,
15    /// TCP fast open.
16    pub tcp_fast_open: bool,
17}
18
19impl Default for ListenerOpts {
20    #[inline]
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl ListenerOpts {
27    /// Create a default ListenerOpts.
28    #[inline]
29    pub const fn new() -> Self {
30        Self {
31            reuse_port: true,
32            reuse_addr: true,
33            backlog: 1024,
34            send_buf_size: None,
35            recv_buf_size: None,
36            tcp_fast_open: false,
37        }
38    }
39
40    /// Enable SO_REUSEPORT
41    #[must_use]
42    #[inline]
43    pub fn reuse_port(mut self, reuse_port: bool) -> Self {
44        self.reuse_port = reuse_port;
45        self
46    }
47
48    /// Enable SO_REUSEADDR
49    #[must_use]
50    #[inline]
51    pub fn reuse_addr(mut self, reuse_addr: bool) -> Self {
52        self.reuse_addr = reuse_addr;
53        self
54    }
55
56    /// Specify backlog
57    #[must_use]
58    #[inline]
59    pub fn backlog(mut self, backlog: i32) -> Self {
60        self.backlog = backlog;
61        self
62    }
63
64    /// Specify SO_SNDBUF
65    #[must_use]
66    #[inline]
67    pub fn send_buf_size(mut self, send_buf_size: usize) -> Self {
68        self.send_buf_size = Some(send_buf_size);
69        self
70    }
71
72    /// Specify SO_RCVBUF
73    #[must_use]
74    #[inline]
75    pub fn recv_buf_size(mut self, recv_buf_size: usize) -> Self {
76        self.recv_buf_size = Some(recv_buf_size);
77        self
78    }
79
80    /// Specify FastOpen.
81    /// Note: if it is enabled, the connection will be
82    /// established on first peer data sent, which means
83    /// data cannot be sent immediately after connection
84    /// accepted if client does not send something.
85    #[must_use]
86    #[inline]
87    pub fn tcp_fast_open(mut self, fast_open: bool) -> Self {
88        self.tcp_fast_open = fast_open;
89        self
90    }
91}