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)]
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 {
43 self.sctp_port = value;
44 self
45 }
46
47 pub fn with_max_receive_buffer_size(mut self, value: u32) -> Self {
48 self.max_receive_buffer_size = value;
49 self
50 }
51
52 pub fn with_max_message_size(mut self, value: u32) -> Self {
53 self.max_message_size = value;
54 self
55 }
56
57 pub fn with_max_num_outbound_streams(mut self, value: u16) -> Self {
58 self.max_num_outbound_streams = value;
59 self
60 }
61
62 pub fn with_max_num_inbound_streams(mut self, value: u16) -> Self {
63 self.max_num_inbound_streams = value;
64 self
65 }
66
67 pub fn with_timer_config(mut self, value: TimerConfig) -> Self {
68 self.timer_config = value;
69 self
70 }
71
72 pub fn sctp_port(&self) -> u16 {
73 self.sctp_port
74 }
75
76 pub fn max_receive_buffer_size(&self) -> u32 {
77 self.max_receive_buffer_size
78 }
79
80 pub fn max_message_size(&self) -> u32 {
81 self.max_message_size
82 }
83
84 pub fn max_num_outbound_streams(&self) -> u16 {
85 self.max_num_outbound_streams
86 }
87
88 pub fn max_num_inbound_streams(&self) -> u16 {
89 self.max_num_inbound_streams
90 }
91
92 pub fn timer_config(&self) -> TimerConfig {
93 self.timer_config
94 }
95}
96
97#[derive(Clone)]
101pub struct EndpointConfig {
102 pub(crate) max_payload_size: u32,
103
104 pub(crate) aid_generator_factory:
108 Arc<dyn (Fn() -> Box<dyn AssociationIdGenerator + Send>) + Send + Sync>,
109}
110
111impl Default for EndpointConfig {
112 fn default() -> Self {
113 Self::new()
114 }
115}
116
117impl EndpointConfig {
118 pub fn new() -> Self {
120 let aid_factory: fn() -> Box<dyn AssociationIdGenerator + Send> =
121 || Box::<RandomAssociationIdGenerator>::default();
122 Self {
123 max_payload_size: INITIAL_MTU - (COMMON_HEADER_SIZE + DATA_CHUNK_HEADER_SIZE),
124 aid_generator_factory: Arc::new(aid_factory),
125 }
126 }
127
128 pub fn aid_generator<
139 F: Fn() -> Box<dyn AssociationIdGenerator + Send> + Send + Sync + 'static,
140 >(
141 &mut self,
142 factory: F,
143 ) -> &mut Self {
144 self.aid_generator_factory = Arc::new(factory);
145 self
146 }
147
148 pub fn max_payload_size(&mut self, value: u32) -> &mut Self {
153 self.max_payload_size = value;
154 self
155 }
156
157 pub fn get_max_payload_size(&self) -> u32 {
166 self.max_payload_size
167 }
168}
169
170impl fmt::Debug for EndpointConfig {
171 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
172 fmt.debug_struct("EndpointConfig")
173 .field("max_payload_size", &self.max_payload_size)
174 .field("aid_generator_factory", &"[ elided ]")
175 .finish()
176 }
177}
178
179#[derive(Debug, Clone)]
183pub struct ServerConfig {
184 pub transport: Arc<TransportConfig>,
186
187 pub(crate) concurrent_associations: u32,
189}
190
191impl Default for ServerConfig {
192 fn default() -> Self {
193 ServerConfig {
194 transport: Arc::new(TransportConfig::default()),
195 concurrent_associations: 100_000,
196 }
197 }
198}
199
200impl ServerConfig {
201 pub fn new() -> Self {
203 ServerConfig::default()
204 }
205}
206
207#[derive(Debug, Clone)]
211pub struct ClientConfig {
212 pub transport: Arc<TransportConfig>,
214}
215
216impl Default for ClientConfig {
217 fn default() -> Self {
218 ClientConfig {
219 transport: Arc::new(TransportConfig::default()),
220 }
221 }
222}
223
224impl ClientConfig {
225 pub fn new() -> Self {
227 ClientConfig::default()
228 }
229}