use crate::util::{AssociationIdGenerator, RandomAssociationIdGenerator};
use crate::TimerConfig;
use std::fmt;
use std::sync::Arc;
pub(crate) const RECEIVE_MTU: usize = 8192;
pub(crate) const INITIAL_MTU: u32 = 1228;
pub(crate) const INITIAL_RECV_BUF_SIZE: u32 = 1024 * 1024;
pub(crate) const COMMON_HEADER_SIZE: u32 = 12;
pub(crate) const DATA_CHUNK_HEADER_SIZE: u32 = 16;
pub(crate) const DEFAULT_MAX_MESSAGE_SIZE: u32 = 262144;
#[derive(Debug, Clone)]
pub struct TransportConfig {
sctp_port: u16,
max_receive_buffer_size: u32,
max_message_size: u32,
max_num_outbound_streams: u16,
max_num_inbound_streams: u16,
timer_config: TimerConfig,
}
impl Default for TransportConfig {
fn default() -> Self {
TransportConfig {
sctp_port: 5000,
max_receive_buffer_size: INITIAL_RECV_BUF_SIZE,
max_message_size: DEFAULT_MAX_MESSAGE_SIZE,
max_num_outbound_streams: u16::MAX,
max_num_inbound_streams: u16::MAX,
timer_config: TimerConfig::default(),
}
}
}
impl TransportConfig {
pub fn with_sctp_port(mut self, value: u16) -> Self {
self.sctp_port = value;
self
}
pub fn with_max_receive_buffer_size(mut self, value: u32) -> Self {
self.max_receive_buffer_size = value;
self
}
pub fn with_max_message_size(mut self, value: u32) -> Self {
self.max_message_size = value;
self
}
pub fn with_max_num_outbound_streams(mut self, value: u16) -> Self {
self.max_num_outbound_streams = value;
self
}
pub fn with_max_num_inbound_streams(mut self, value: u16) -> Self {
self.max_num_inbound_streams = value;
self
}
pub fn with_timer_config(mut self, value: TimerConfig) -> Self {
self.timer_config = value;
self
}
pub fn sctp_port(&self) -> u16 {
self.sctp_port
}
pub fn max_receive_buffer_size(&self) -> u32 {
self.max_receive_buffer_size
}
pub fn max_message_size(&self) -> u32 {
self.max_message_size
}
pub fn max_num_outbound_streams(&self) -> u16 {
self.max_num_outbound_streams
}
pub fn max_num_inbound_streams(&self) -> u16 {
self.max_num_inbound_streams
}
pub fn timer_config(&self) -> TimerConfig {
self.timer_config
}
}
#[derive(Clone)]
pub struct EndpointConfig {
pub(crate) max_payload_size: u32,
pub(crate) aid_generator_factory:
Arc<dyn (Fn() -> Box<dyn AssociationIdGenerator + Send>) + Send + Sync>,
}
impl Default for EndpointConfig {
fn default() -> Self {
Self::new()
}
}
impl EndpointConfig {
pub fn new() -> Self {
let aid_factory: fn() -> Box<dyn AssociationIdGenerator + Send> =
|| Box::<RandomAssociationIdGenerator>::default();
Self {
max_payload_size: INITIAL_MTU - (COMMON_HEADER_SIZE + DATA_CHUNK_HEADER_SIZE),
aid_generator_factory: Arc::new(aid_factory),
}
}
pub fn aid_generator<
F: Fn() -> Box<dyn AssociationIdGenerator + Send> + Send + Sync + 'static,
>(
&mut self,
factory: F,
) -> &mut Self {
self.aid_generator_factory = Arc::new(factory);
self
}
pub fn max_payload_size(&mut self, value: u32) -> &mut Self {
self.max_payload_size = value;
self
}
pub fn get_max_payload_size(&self) -> u32 {
self.max_payload_size
}
}
impl fmt::Debug for EndpointConfig {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("EndpointConfig")
.field("max_payload_size", &self.max_payload_size)
.field("aid_generator_factory", &"[ elided ]")
.finish()
}
}
#[derive(Debug, Clone)]
pub struct ServerConfig {
pub transport: Arc<TransportConfig>,
pub(crate) concurrent_associations: u32,
}
impl Default for ServerConfig {
fn default() -> Self {
ServerConfig {
transport: Arc::new(TransportConfig::default()),
concurrent_associations: 100_000,
}
}
}
impl ServerConfig {
pub fn new(transport: TransportConfig) -> Self {
ServerConfig {
transport: Arc::new(transport),
concurrent_associations: 100_000,
}
}
}
#[derive(Debug, Clone)]
pub struct ClientConfig {
pub transport: Arc<TransportConfig>,
}
impl Default for ClientConfig {
fn default() -> Self {
ClientConfig {
transport: Arc::new(TransportConfig::default()),
}
}
}
impl ClientConfig {
pub fn new(transport: TransportConfig) -> Self {
ClientConfig {
transport: Arc::new(transport),
}
}
}