1use crate::util::{AssociationIdGenerator, RandomAssociationIdGenerator};
2
3use crate::TimerConfig;
4use std::fmt;
5use std::sync::Arc;
6
7pub(crate) const RECEIVE_MTU: usize = 8192;
9pub(crate) const INITIAL_MTU: u32 = 1228;
11pub(crate) const INITIAL_RECV_BUF_SIZE: u32 = 1024 * 1024;
12pub(crate) const COMMON_HEADER_SIZE: u32 = 12;
13pub(crate) const DATA_CHUNK_HEADER_SIZE: u32 = 16;
14pub(crate) const DEFAULT_MAX_MESSAGE_SIZE: u32 = 262144;
15
16#[derive(Debug, Clone)]
19pub struct TransportConfig {
20 sctp_port: u16,
21 max_receive_buffer_size: u32,
22 max_message_size: u32,
23 max_num_outbound_streams: u16,
24 max_num_inbound_streams: u16,
25 timer_config: TimerConfig,
26}
27
28impl Default for TransportConfig {
29 fn default() -> Self {
30 TransportConfig {
31 sctp_port: 5000,
32 max_receive_buffer_size: INITIAL_RECV_BUF_SIZE,
33 max_message_size: DEFAULT_MAX_MESSAGE_SIZE,
34 max_num_outbound_streams: u16::MAX,
35 max_num_inbound_streams: u16::MAX,
36 timer_config: TimerConfig::default(),
37 }
38 }
39}
40
41impl TransportConfig {
42 pub fn with_sctp_port(mut self, value: u16) -> Self {
44 self.sctp_port = value;
45 self
46 }
47
48 pub fn with_max_receive_buffer_size(mut self, value: u32) -> Self {
51 self.max_receive_buffer_size = value;
52 self
53 }
54
55 pub fn with_max_message_size(mut self, value: u32) -> Self {
57 self.max_message_size = value;
58 self
59 }
60
61 pub fn with_max_num_outbound_streams(mut self, value: u16) -> Self {
63 self.max_num_outbound_streams = value;
64 self
65 }
66
67 pub fn with_max_num_inbound_streams(mut self, value: u16) -> Self {
69 self.max_num_inbound_streams = value;
70 self
71 }
72
73 pub fn with_timer_config(mut self, value: TimerConfig) -> Self {
75 self.timer_config = value;
76 self
77 }
78
79 pub fn sctp_port(&self) -> u16 {
81 self.sctp_port
82 }
83
84 pub fn max_receive_buffer_size(&self) -> u32 {
86 self.max_receive_buffer_size
87 }
88
89 pub fn max_message_size(&self) -> u32 {
91 self.max_message_size
92 }
93
94 pub fn max_num_outbound_streams(&self) -> u16 {
96 self.max_num_outbound_streams
97 }
98
99 pub fn max_num_inbound_streams(&self) -> u16 {
101 self.max_num_inbound_streams
102 }
103
104 pub fn timer_config(&self) -> TimerConfig {
106 self.timer_config
107 }
108}
109
110#[derive(Clone)]
114pub struct EndpointConfig {
115 pub(crate) max_payload_size: u32,
116
117 pub(crate) aid_generator_factory:
121 Arc<dyn (Fn() -> Box<dyn AssociationIdGenerator + Send>) + Send + Sync>,
122}
123
124impl Default for EndpointConfig {
125 fn default() -> Self {
126 Self::new()
127 }
128}
129
130impl EndpointConfig {
131 pub fn new() -> Self {
133 let aid_factory: fn() -> Box<dyn AssociationIdGenerator + Send> =
134 || Box::<RandomAssociationIdGenerator>::default();
135 Self {
136 max_payload_size: INITIAL_MTU - (COMMON_HEADER_SIZE + DATA_CHUNK_HEADER_SIZE),
137 aid_generator_factory: Arc::new(aid_factory),
138 }
139 }
140
141 pub fn aid_generator<
152 F: Fn() -> Box<dyn AssociationIdGenerator + Send> + Send + Sync + 'static,
153 >(
154 &mut self,
155 factory: F,
156 ) -> &mut Self {
157 self.aid_generator_factory = Arc::new(factory);
158 self
159 }
160
161 pub fn max_payload_size(&mut self, value: u32) -> &mut Self {
166 self.max_payload_size = value;
167 self
168 }
169
170 pub fn get_max_payload_size(&self) -> u32 {
179 self.max_payload_size
180 }
181}
182
183impl fmt::Debug for EndpointConfig {
184 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
185 fmt.debug_struct("EndpointConfig")
186 .field("max_payload_size", &self.max_payload_size)
187 .field("aid_generator_factory", &"[ elided ]")
188 .finish()
189 }
190}
191
192#[derive(Debug, Clone)]
196pub struct ServerConfig {
197 pub transport: Arc<TransportConfig>,
199
200 pub(crate) concurrent_associations: u32,
202}
203
204impl Default for ServerConfig {
205 fn default() -> Self {
206 ServerConfig {
207 transport: Arc::new(TransportConfig::default()),
208 concurrent_associations: 100_000,
209 }
210 }
211}
212
213impl ServerConfig {
214 pub fn new(transport: TransportConfig) -> Self {
216 ServerConfig {
217 transport: Arc::new(transport),
218 concurrent_associations: 100_000,
219 }
220 }
221}
222
223#[derive(Debug, Clone)]
227pub struct ClientConfig {
228 pub transport: Arc<TransportConfig>,
230}
231
232impl Default for ClientConfig {
233 fn default() -> Self {
234 ClientConfig {
235 transport: Arc::new(TransportConfig::default()),
236 }
237 }
238}
239
240impl ClientConfig {
241 pub fn new(transport: TransportConfig) -> Self {
243 ClientConfig {
244 transport: Arc::new(transport),
245 }
246 }
247}