Skip to main content

phantom_protocol/
config.rs

1use std::time::Duration;
2
3#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
4#[derive(Debug, Clone)]
5// New tunables must not break downstream construction. Build via `mobile()` /
6// `server()` / `iot()` / `default()` then mutate the public fields.
7#[non_exhaustive]
8pub struct PhantomConfig {
9    /// Interval between keep-alive pings
10    pub keepalive_interval: Duration,
11    /// Session inactivity timeout
12    pub session_timeout: Duration,
13    /// Maximum packet size (MTU)
14    pub max_packet_size: u32,
15    /// Send buffer size in packets
16    pub send_buffer_size: u32,
17    /// Receive buffer size in packets
18    pub recv_buffer_size: u32,
19    /// Maximum tickets in session cache
20    pub session_cache_capacity: u32,
21    /// Lifetime of a session ticket
22    pub session_ticket_lifetime: Duration,
23    /// Enable automatic transport fallback
24    pub auto_fallback: bool,
25    /// Packet loss percentage to trigger fallback
26    pub fallback_loss_threshold: u8,
27    /// Connection failures to trigger fallback
28    pub fallback_failure_threshold: u32,
29    /// Timeout for connection attempts
30    pub connect_timeout: Duration,
31    /// Delay before attempting to upgrade transport
32    pub upgrade_delay: Duration,
33}
34
35impl Default for PhantomConfig {
36    fn default() -> Self {
37        Self::mobile()
38    }
39}
40
41impl PhantomConfig {
42    /// Optimized for mobile devices (LTE/Wi-Fi transitions, power saving)
43    pub fn mobile() -> Self {
44        Self {
45            keepalive_interval: Duration::from_secs(30),
46            session_timeout: Duration::from_secs(3600),
47            max_packet_size: 1350,
48            send_buffer_size: 256,
49            recv_buffer_size: 1024,
50            session_cache_capacity: 32,
51            session_ticket_lifetime: Duration::from_secs(86400),
52            auto_fallback: true,
53            fallback_loss_threshold: 15,
54            fallback_failure_threshold: 3,
55            connect_timeout: Duration::from_secs(5),
56            upgrade_delay: Duration::from_secs(60),
57        }
58    }
59
60    /// Optimized for servers (high throughput, static IP)
61    pub fn server() -> Self {
62        Self {
63            keepalive_interval: Duration::from_secs(60),
64            session_timeout: Duration::from_secs(7200),
65            max_packet_size: 1440,
66            send_buffer_size: 2048,
67            recv_buffer_size: 8192,
68            session_cache_capacity: 1024,
69            session_ticket_lifetime: Duration::from_secs(604800),
70            auto_fallback: false,
71            fallback_loss_threshold: 20,
72            fallback_failure_threshold: 5,
73            connect_timeout: Duration::from_secs(2),
74            upgrade_delay: Duration::from_secs(300),
75        }
76    }
77
78    /// Optimized for IoT devices (extremely low memory, slow networks)
79    pub fn iot() -> Self {
80        Self {
81            keepalive_interval: Duration::from_secs(120),
82            session_timeout: Duration::from_secs(1800),
83            max_packet_size: 512,
84            send_buffer_size: 32,
85            recv_buffer_size: 64,
86            session_cache_capacity: 4,
87            session_ticket_lifetime: Duration::from_secs(3600),
88            auto_fallback: true,
89            fallback_loss_threshold: 25,
90            fallback_failure_threshold: 2,
91            connect_timeout: Duration::from_secs(10),
92            upgrade_delay: Duration::from_secs(600),
93        }
94    }
95}