1use ::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 pub nodelay: bool,
13 pub interval: u32,
15 pub resend: u32,
17 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 pub fn fastest() -> KcpNoDelayConfig {
40 KcpNoDelayConfig {
41 nodelay: true,
42 interval: 10,
43 resend: 2,
44 nc: true,
45 }
46 }
47
48 pub fn normal() -> KcpNoDelayConfig {
55 KcpNoDelayConfig {
56 nodelay: false,
57 interval: 40,
58 resend: 0,
59 nc: false,
60 }
61 }
62}
63
64#[derive(Debug, Clone)]
66pub struct KcpConfig {
67 pub mtu: u32,
69 pub nodelay: KcpNoDelayConfig,
71 pub snd_wnd: u32,
73 pub rcv_wnd: u32,
75 pub stream: bool,
77 pub session_key: Bytes,
79 pub session_id_len: usize,
81 pub session_expire: Duration,
83 pub connect_timeout: Duration,
85 pub shutdown_timeout: Duration,
87 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}