use std::{borrow::Cow, time::Duration};
use engineioxide_core::TransportType;
#[derive(Debug, Clone)]
pub struct EngineIoConfig {
pub req_path: Cow<'static, str>,
pub ping_interval: Duration,
pub ping_timeout: Duration,
pub upgrade_timeout: Duration,
pub max_buffer_size: usize,
pub max_payload: u64,
pub ws_read_buffer_size: usize,
pub transports: u8,
}
impl Default for EngineIoConfig {
fn default() -> Self {
Self {
req_path: "/engine.io".into(),
ping_interval: Duration::from_millis(25000),
ping_timeout: Duration::from_millis(20000),
upgrade_timeout: Duration::from_millis(10000),
max_buffer_size: 128,
max_payload: 1e5 as u64, ws_read_buffer_size: 4096,
transports: TransportType::Polling as u8 | TransportType::Websocket as u8,
}
}
}
impl EngineIoConfig {
pub fn builder() -> EngineIoConfigBuilder {
EngineIoConfigBuilder::new()
}
#[inline(always)]
pub fn allowed_transport(&self, transport: TransportType) -> bool {
self.transports & transport as u8 == transport as u8
}
}
pub struct EngineIoConfigBuilder {
config: EngineIoConfig,
}
impl EngineIoConfigBuilder {
pub fn new() -> Self {
Self {
config: EngineIoConfig::default(),
}
}
pub fn req_path(mut self, req_path: impl Into<Cow<'static, str>>) -> Self {
self.config.req_path = req_path.into();
self
}
pub fn ping_interval(mut self, ping_interval: Duration) -> Self {
self.config.ping_interval = ping_interval;
self
}
pub fn ping_timeout(mut self, ping_timeout: Duration) -> Self {
self.config.ping_timeout = ping_timeout;
self
}
pub fn upgrade_timeout(mut self, upgrade_timeout: Duration) -> Self {
self.config.upgrade_timeout = upgrade_timeout;
self
}
pub fn max_buffer_size(mut self, max_buffer_size: usize) -> Self {
self.config.max_buffer_size = max_buffer_size;
self
}
pub fn max_payload(mut self, max_payload: u64) -> Self {
self.config.max_payload = max_payload;
self
}
pub fn ws_read_buffer_size(mut self, ws_read_buffer_size: usize) -> Self {
self.config.ws_read_buffer_size = ws_read_buffer_size;
self
}
pub fn transports<const N: usize>(mut self, transports: [TransportType; N]) -> Self {
assert!(N > 0 && N <= 2);
self.config.transports = 0;
for transport in transports {
self.config.transports |= transport as u8;
}
self
}
pub fn build(self) -> EngineIoConfig {
self.config
}
}
impl Default for EngineIoConfigBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn config_transports() {
let conf = EngineIoConfig::builder()
.transports([TransportType::Polling])
.build();
assert!(conf.allowed_transport(TransportType::Polling));
assert!(!conf.allowed_transport(TransportType::Websocket));
let conf = EngineIoConfig::builder()
.transports([TransportType::Websocket])
.build();
assert!(conf.allowed_transport(TransportType::Websocket));
assert!(!conf.allowed_transport(TransportType::Polling));
let conf = EngineIoConfig::builder()
.transports([TransportType::Polling, TransportType::Websocket])
.build();
assert!(conf.allowed_transport(TransportType::Polling));
assert!(conf.allowed_transport(TransportType::Websocket));
}
}