1use std::net::SocketAddr;
4use std::time::Duration;
5
6use crate::protocol::constants::*;
7
8#[derive(Debug, Clone)]
10pub struct ServerConfig {
11 pub bind_addr: SocketAddr,
13
14 pub max_connections: usize,
16
17 pub chunk_size: u32,
19
20 pub window_ack_size: u32,
22
23 pub peer_bandwidth: u32,
25
26 pub connection_timeout: Duration,
28
29 pub idle_timeout: Duration,
31
32 pub tcp_nodelay: bool,
34
35 pub tcp_recv_buffer: usize,
37
38 pub tcp_send_buffer: usize,
40
41 pub read_buffer_size: usize,
43
44 pub write_buffer_size: usize,
46
47 pub gop_buffer_enabled: bool,
49
50 pub gop_buffer_max_size: usize,
52
53 pub stats_interval: Duration,
55}
56
57impl Default for ServerConfig {
58 fn default() -> Self {
59 Self {
60 bind_addr: "0.0.0.0:1935".parse().unwrap(),
61 max_connections: 0, chunk_size: RECOMMENDED_CHUNK_SIZE,
63 window_ack_size: DEFAULT_WINDOW_ACK_SIZE,
64 peer_bandwidth: DEFAULT_PEER_BANDWIDTH,
65 connection_timeout: Duration::from_secs(10),
66 idle_timeout: Duration::from_secs(60),
67 tcp_nodelay: true, tcp_recv_buffer: 0,
69 tcp_send_buffer: 0,
70 read_buffer_size: 64 * 1024, write_buffer_size: 64 * 1024,
72 gop_buffer_enabled: true,
73 gop_buffer_max_size: 4 * 1024 * 1024, stats_interval: Duration::from_secs(5),
75 }
76 }
77}
78
79impl ServerConfig {
80 pub fn with_addr(addr: SocketAddr) -> Self {
82 Self {
83 bind_addr: addr,
84 ..Default::default()
85 }
86 }
87
88 pub fn bind(mut self, addr: SocketAddr) -> Self {
90 self.bind_addr = addr;
91 self
92 }
93
94 pub fn max_connections(mut self, max: usize) -> Self {
96 self.max_connections = max;
97 self
98 }
99
100 pub fn chunk_size(mut self, size: u32) -> Self {
102 self.chunk_size = size.min(MAX_CHUNK_SIZE);
103 self
104 }
105
106 pub fn disable_gop_buffer(mut self) -> Self {
108 self.gop_buffer_enabled = false;
109 self
110 }
111
112 pub fn connection_timeout(mut self, timeout: Duration) -> Self {
114 self.connection_timeout = timeout;
115 self
116 }
117
118 pub fn idle_timeout(mut self, timeout: Duration) -> Self {
120 self.idle_timeout = timeout;
121 self
122 }
123}