1use crate::util::{AssociationIdGenerator, RandomAssociationIdGenerator};
2
3use std::fmt;
4use std::sync::Arc;
5
6pub(crate) const RECEIVE_MTU: usize = 8192;
8pub(crate) const INITIAL_MTU: u32 = 1228;
10pub(crate) const INITIAL_RECV_BUF_SIZE: u32 = 1024 * 1024;
11pub(crate) const COMMON_HEADER_SIZE: u32 = 12;
12pub(crate) const DATA_CHUNK_HEADER_SIZE: u32 = 16;
13pub(crate) const DEFAULT_MAX_MESSAGE_SIZE: u32 = 65536;
14
15#[derive(Debug)]
18pub struct TransportConfig {
19 max_receive_buffer_size: u32,
20 max_message_size: u32,
21 max_num_outbound_streams: u16,
22 max_num_inbound_streams: u16,
23}
24
25impl Default for TransportConfig {
26 fn default() -> Self {
27 TransportConfig {
28 max_receive_buffer_size: INITIAL_RECV_BUF_SIZE,
29 max_message_size: DEFAULT_MAX_MESSAGE_SIZE,
30 max_num_outbound_streams: u16::MAX,
31 max_num_inbound_streams: u16::MAX,
32 }
33 }
34}
35
36impl TransportConfig {
37 pub fn with_max_receive_buffer_size(mut self, value: u32) -> Self {
38 self.max_receive_buffer_size = value;
39 self
40 }
41
42 pub fn with_max_message_size(mut self, value: u32) -> Self {
43 self.max_message_size = value;
44 self
45 }
46
47 pub fn with_max_num_outbound_streams(mut self, value: u16) -> Self {
48 self.max_num_outbound_streams = value;
49 self
50 }
51
52 pub fn with_max_num_inbound_streams(mut self, value: u16) -> Self {
53 self.max_num_inbound_streams = value;
54 self
55 }
56
57 pub(crate) fn max_receive_buffer_size(&self) -> u32 {
58 self.max_receive_buffer_size
59 }
60
61 pub(crate) fn max_message_size(&self) -> u32 {
62 self.max_message_size
63 }
64
65 pub(crate) fn max_num_outbound_streams(&self) -> u16 {
66 self.max_num_outbound_streams
67 }
68
69 pub(crate) fn max_num_inbound_streams(&self) -> u16 {
70 self.max_num_inbound_streams
71 }
72}
73
74#[derive(Clone)]
78pub struct EndpointConfig {
79 pub(crate) max_payload_size: u32,
80
81 pub(crate) aid_generator_factory:
85 Arc<dyn Fn() -> Box<dyn AssociationIdGenerator> + Send + Sync>,
86}
87
88impl Default for EndpointConfig {
89 fn default() -> Self {
90 Self::new()
91 }
92}
93
94impl EndpointConfig {
95 pub fn new() -> Self {
97 let aid_factory: fn() -> Box<dyn AssociationIdGenerator> =
98 || Box::<RandomAssociationIdGenerator>::default();
99 Self {
100 max_payload_size: INITIAL_MTU - (COMMON_HEADER_SIZE + DATA_CHUNK_HEADER_SIZE),
101 aid_generator_factory: Arc::new(aid_factory),
102 }
103 }
104
105 pub fn aid_generator<F: Fn() -> Box<dyn AssociationIdGenerator> + Send + Sync + 'static>(
116 &mut self,
117 factory: F,
118 ) -> &mut Self {
119 self.aid_generator_factory = Arc::new(factory);
120 self
121 }
122
123 pub fn max_payload_size(&mut self, value: u32) -> &mut Self {
128 self.max_payload_size = value;
129 self
130 }
131
132 #[doc(hidden)]
141 pub fn get_max_payload_size(&self) -> u32 {
142 self.max_payload_size
143 }
144}
145
146impl fmt::Debug for EndpointConfig {
147 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
148 fmt.debug_struct("EndpointConfig")
149 .field("max_payload_size", &self.max_payload_size)
150 .field("aid_generator_factory", &"[ elided ]")
151 .finish()
152 }
153}
154
155#[derive(Debug, Clone)]
159pub struct ServerConfig {
160 pub transport: Arc<TransportConfig>,
162
163 pub(crate) concurrent_associations: u32,
165}
166
167impl Default for ServerConfig {
168 fn default() -> Self {
169 ServerConfig {
170 transport: Arc::new(TransportConfig::default()),
171 concurrent_associations: 100_000,
172 }
173 }
174}
175
176impl ServerConfig {
177 pub fn new() -> Self {
179 ServerConfig::default()
180 }
181}
182
183#[derive(Debug, Clone)]
187pub struct ClientConfig {
188 pub transport: Arc<TransportConfig>,
190}
191
192impl Default for ClientConfig {
193 fn default() -> Self {
194 ClientConfig {
195 transport: Arc::new(TransportConfig::default()),
196 }
197 }
198}
199
200impl ClientConfig {
201 pub fn new() -> Self {
203 ClientConfig::default()
204 }
205}