kcp/
config.rs

1// ! This file is referenced from https://github.com/Matrix-Zhang/tokio_kcp
2
3use ::bytes::Bytes;
4use ::std::time::Duration;
5
6pub(crate) const LISTENER_TASK_LOOP: usize = 1024;
7pub(crate) const LISTENER_CONV_TIMEOUT: Duration = Duration::from_secs(120);
8
9#[derive(Debug, Clone, Copy)]
10pub struct KcpNoDelayConfig {
11    /// Enable nodelay
12    pub nodelay: bool,
13    /// Internal update interval (ms)
14    pub interval: u32,
15    /// ACK number to enable fast resend
16    pub resend: u32,
17    /// Disable congetion control
18    pub nc: bool,
19}
20
21impl Default for KcpNoDelayConfig {
22    fn default() -> KcpNoDelayConfig {
23        KcpNoDelayConfig {
24            nodelay: false,
25            interval: 100,
26            resend: 0,
27            nc: false,
28        }
29    }
30}
31
32impl KcpNoDelayConfig {
33    /// Get a fastest configuration
34    ///
35    /// 1. Enable NoDelay
36    /// 2. Set ticking interval to be 10ms
37    /// 3. Set fast resend to be 2
38    /// 4. Disable congestion control
39    pub fn fastest() -> KcpNoDelayConfig {
40        KcpNoDelayConfig {
41            nodelay: true,
42            interval: 10,
43            resend: 2,
44            nc: true,
45        }
46    }
47
48    /// Get a normal configuration
49    ///
50    /// 1. Disable NoDelay
51    /// 2. Set ticking interval to be 40ms
52    /// 3. Disable fast resend
53    /// 4. Enable congestion control
54    pub fn normal() -> KcpNoDelayConfig {
55        KcpNoDelayConfig {
56            nodelay: false,
57            interval: 40,
58            resend: 0,
59            nc: false,
60        }
61    }
62}
63
64/// Kcp Config
65#[derive(Debug, Clone)]
66pub struct KcpConfig {
67    /// Max Transmission Unit
68    pub mtu: u32,
69    /// nodelay
70    pub nodelay: KcpNoDelayConfig,
71    /// send window size
72    pub snd_wnd: u32,
73    /// recv window size
74    pub rcv_wnd: u32,
75    /// Stream mode
76    pub stream: bool,
77    /// Session key
78    pub session_key: Bytes,
79    /// Length of session ID
80    pub session_id_len: usize,
81    /// Session expire duration, default is 90 seconds
82    pub session_expire: Duration,
83    /// Connect timeout, default is 15 seconds
84    pub connect_timeout: Duration,
85    /// Shutdown timeout, default is 10 seconds
86    pub shutdown_timeout: Duration,
87    /// Half-close timeout. Default is 5 seconds; 0 to disable.
88    pub half_close_timeout: Duration,
89}
90
91impl Default for KcpConfig {
92    fn default() -> KcpConfig {
93        KcpConfig {
94            mtu: 1400,
95            nodelay: KcpNoDelayConfig::normal(),
96            snd_wnd: 32,
97            rcv_wnd: 256,
98            stream: true,
99            session_key: Bytes::new(),
100            session_id_len: 16,
101            session_expire: Duration::from_secs(90),
102            connect_timeout: Duration::from_secs(15),
103            shutdown_timeout: Duration::from_secs(10),
104            half_close_timeout: Duration::from_secs(5),
105        }
106    }
107}
108
109impl KcpConfig {
110    pub fn random_session_id(&self) -> Vec<u8> {
111        std::iter::repeat_with(rand::random::<u8>)
112            .take(self.session_id_len)
113            .collect()
114    }
115}