Skip to main content

o_sfu_router/model/
ids.rs

1use std::fmt::{self, Display, Formatter};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4pub struct RouterId(pub u64);
5
6/// unique identifier for a user's transport connection within the server process
7///
8/// this separates the ephemeral transport lifecycle from the persistent logical
9/// user identity. a single user might create multiple connections over time due to
10/// network drops or handovers. this identifier ensures media operations only apply
11/// to the specific transport they were negotiated against
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct ConnectionId(u64);
14
15impl ConnectionId {
16    #[must_use]
17    pub fn allocate(next_connection_id: &mut u64) -> Self {
18        let connection_id = Self(*next_connection_id);
19        *next_connection_id = next_connection_id.saturating_add(1);
20        connection_id
21    }
22
23    #[must_use]
24    pub const fn from_raw(raw: u64) -> Self {
25        Self(raw)
26    }
27
28    #[must_use]
29    pub const fn as_u64(self) -> u64 {
30        self.0
31    }
32}
33
34impl Display for ConnectionId {
35    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
36        self.0.fmt(formatter)
37    }
38}
39
40/// runtime-local identifier for one rtc media worker
41///
42/// this is worker identity, not a worker count or vector capacity
43/// convert to raw `usize` only when indexing worker storage or projecting
44/// telemetry and diagnostics fields
45#[repr(transparent)]
46#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
47pub struct MediaWorkerId(usize);
48
49impl MediaWorkerId {
50    #[must_use]
51    pub const fn from_raw(raw: usize) -> Self {
52        Self(raw)
53    }
54
55    #[must_use]
56    pub const fn as_usize(self) -> usize {
57        self.0
58    }
59}