Skip to main content

phantom_protocol/
config.rs

1use std::time::Duration;
2
3/// Tunable parameters for a Phantom session / listener, exported across the
4/// UniFFI boundary as a plain record.
5///
6/// NOTE: this is a stable FFI config surface, but the core does not yet read
7/// most of these fields on the live data path — `PhantomConfig` is currently
8/// re-exported and FFI-exported only. The `auto_fallback` / `fallback_*` /
9/// `upgrade_delay` fields in particular describe the legacy multi-leg
10/// fallback model; transport-leg fallback / aggregation was deliberately
11/// dropped in favour of single-path connection migration, so those knobs are
12/// presently inert. Treat the presets below as documented intent, not as
13/// behaviour the core enforces today.
14#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
15#[derive(Debug, Clone)]
16// New tunables must not break downstream construction. Build via `mobile()` /
17// `server()` / `iot()` / `default()` then mutate the public fields.
18#[non_exhaustive]
19pub struct PhantomConfig {
20    /// Interval between keep-alive pings
21    pub keepalive_interval: Duration,
22    /// Session inactivity timeout
23    pub session_timeout: Duration,
24    /// Maximum packet size (MTU)
25    pub max_packet_size: u32,
26    /// Send buffer size in packets
27    pub send_buffer_size: u32,
28    /// Receive buffer size in packets
29    pub recv_buffer_size: u32,
30    /// Maximum tickets in session cache
31    pub session_cache_capacity: u32,
32    /// Lifetime of a session ticket
33    pub session_ticket_lifetime: Duration,
34    /// Enable automatic transport fallback (legacy multi-leg model; inert —
35    /// see the struct-level note).
36    pub auto_fallback: bool,
37    /// Packet loss percentage to trigger fallback (legacy multi-leg model; inert).
38    pub fallback_loss_threshold: u8,
39    /// Connection failures to trigger fallback (legacy multi-leg model; inert).
40    pub fallback_failure_threshold: u32,
41    /// Timeout for connection attempts
42    pub connect_timeout: Duration,
43    /// Delay before attempting to upgrade transport (legacy multi-leg model; inert).
44    pub upgrade_delay: Duration,
45}
46
47impl Default for PhantomConfig {
48    fn default() -> Self {
49        Self::mobile()
50    }
51}
52
53impl PhantomConfig {
54    /// Optimized for mobile devices (LTE/Wi-Fi transitions, power saving)
55    pub fn mobile() -> Self {
56        Self {
57            keepalive_interval: Duration::from_secs(30),
58            session_timeout: Duration::from_secs(3600),
59            max_packet_size: 1350,
60            send_buffer_size: 256,
61            recv_buffer_size: 1024,
62            session_cache_capacity: 32,
63            session_ticket_lifetime: Duration::from_secs(86400),
64            auto_fallback: true,
65            fallback_loss_threshold: 15,
66            fallback_failure_threshold: 3,
67            connect_timeout: Duration::from_secs(5),
68            upgrade_delay: Duration::from_secs(60),
69        }
70    }
71
72    /// Optimized for servers (high throughput, static IP)
73    pub fn server() -> Self {
74        Self {
75            keepalive_interval: Duration::from_secs(60),
76            session_timeout: Duration::from_secs(7200),
77            max_packet_size: 1440,
78            send_buffer_size: 2048,
79            recv_buffer_size: 8192,
80            session_cache_capacity: 1024,
81            session_ticket_lifetime: Duration::from_secs(604800),
82            auto_fallback: false,
83            fallback_loss_threshold: 20,
84            fallback_failure_threshold: 5,
85            connect_timeout: Duration::from_secs(2),
86            upgrade_delay: Duration::from_secs(300),
87        }
88    }
89
90    /// Optimized for IoT devices (extremely low memory, slow networks)
91    pub fn iot() -> Self {
92        Self {
93            keepalive_interval: Duration::from_secs(120),
94            session_timeout: Duration::from_secs(1800),
95            max_packet_size: 512,
96            send_buffer_size: 32,
97            recv_buffer_size: 64,
98            session_cache_capacity: 4,
99            session_ticket_lifetime: Duration::from_secs(3600),
100            auto_fallback: true,
101            fallback_loss_threshold: 25,
102            fallback_failure_threshold: 2,
103            connect_timeout: Duration::from_secs(10),
104            upgrade_delay: Duration::from_secs(600),
105        }
106    }
107}