use std::fmt;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PeerId(String);
impl PeerId {
pub(crate) fn new(id: String) -> Self {
Self(id)
}
}
impl fmt::Display for PeerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl AsRef<str> for PeerId {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct HostConfig {
pub event_delay: Duration,
pub password: Option<String>,
pub max_players: Option<u32>,
}
impl HostConfig {
pub fn new() -> Self {
Self::default()
}
pub fn event_delay(mut self, delay: Duration) -> Self {
self.event_delay = delay;
self
}
pub fn password(mut self, password: Option<String>) -> Self {
self.password = password;
self
}
pub fn max_players(mut self, max_players: Option<u32>) -> Self {
self.max_players = max_players;
self
}
}
impl Default for HostConfig {
fn default() -> Self {
Self {
event_delay: Duration::ZERO,
password: None,
max_players: None,
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct JoinConfig {
pub event_delay: Duration,
pub password: Option<String>,
pub max_retries: Option<u32>,
pub initial_retries: u32,
pub base_backoff: Duration,
pub max_backoff: Duration,
}
impl JoinConfig {
pub fn new() -> Self {
Self::default()
}
pub fn event_delay(mut self, delay: Duration) -> Self {
self.event_delay = delay;
self
}
pub fn password(mut self, password: Option<String>) -> Self {
self.password = password;
self
}
pub fn max_retries(mut self, max_retries: Option<u32>) -> Self {
self.max_retries = max_retries;
self
}
}
impl Default for JoinConfig {
fn default() -> Self {
Self {
event_delay: Duration::ZERO,
password: None,
max_retries: None,
initial_retries: 3,
base_backoff: Duration::from_millis(500),
max_backoff: Duration::from_secs(30),
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum TunnelEvent {
PlayerJoined { id: PeerId },
PlayerLeft { id: PeerId, reason: String },
Connected,
Disconnected { reason: String },
PathChanged {
remote_id: PeerId,
is_relay: bool,
rtt_ms: u64,
},
Reconnecting { attempt: u32 },
Reconnected,
AuthFailed { id: PeerId },
PlayerRejected { id: PeerId, reason: String },
Error { message: String },
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ConnectionSnapshot {
pub remote_id: PeerId,
pub is_relay: bool,
pub rtt_ms: u64,
pub tx_bytes: u64,
pub rx_bytes: u64,
pub alive: bool,
pub elapsed: Duration,
}