Skip to main content

naia_shared/connection/
connection_config.rs

1use std::{default::Default, time::Duration};
2
3use crate::connection::bandwidth::BandwidthConfig;
4
5/// Contains Config properties which will be used by a Server or Client
6#[derive(Clone, Debug)]
7pub struct ConnectionConfig {
8    /// The duration to wait for communication from a remote host before
9    /// initiating a disconnect
10    pub disconnection_timeout_duration: Duration,
11    /// The duration to wait before sending a heartbeat message to a remote
12    /// host, if the host has not already sent another message within that time
13    pub heartbeat_interval: Duration,
14    /// The duration over which to measure bandwidth. Set to None to avoid
15    /// measure bandwidth at all. This is a telemetry/averaging window — NOT
16    /// the outbound cap; for that, see `bandwidth`.
17    pub bandwidth_measure_duration: Option<Duration>,
18    /// Outbound bandwidth budget (token-bucket cap) applied by the unified
19    /// priority-sort send loop. Distinct from `bandwidth_measure_duration`.
20    pub bandwidth: BandwidthConfig,
21}
22
23impl ConnectionConfig {
24    /// Creates a new ConnectionConfig, used to initialize a Connection.
25    /// Uses default `BandwidthConfig`; set the `.bandwidth` field directly
26    /// after construction if a non-default budget is required.
27    pub fn new(
28        disconnection_timeout_duration: Duration,
29        heartbeat_interval: Duration,
30        bandwidth_measure_duration: Option<Duration>,
31    ) -> Self {
32        Self {
33            disconnection_timeout_duration,
34            heartbeat_interval,
35            bandwidth_measure_duration,
36            bandwidth: BandwidthConfig::default(),
37        }
38    }
39}
40
41impl Default for ConnectionConfig {
42    fn default() -> Self {
43        Self {
44            // 30 s: generous enough to survive a 20–25 s mobile network handoff
45            // or a brief server GC pause, but short enough that a dead peer is
46            // detected before the 60 s TCP keepalive would fire on the auth socket.
47            disconnection_timeout_duration: Duration::from_secs(30),
48            // 4 s: keeps NAT mappings alive (most home routers timeout UDP at
49            // ~30 s; 4 s gives plenty of headroom even at 1 heartbeat / interval).
50            heartbeat_interval: Duration::from_secs(4),
51            // None: bandwidth measurement is opt-in telemetry; disabled by default
52            // to avoid the per-packet accounting cost in production builds.
53            bandwidth_measure_duration: None,
54            bandwidth: BandwidthConfig::default(),
55        }
56    }
57}