Skip to main content

oxpulse_sfu_kit/
net.rs

1//! UDP datagram wrappers over `str0m::net::{Protocol, Transmit}`.
2
3use std::net::SocketAddr;
4use std::time::Instant;
5
6/// Transport protocol for an SFU datagram.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum SfuProtocol {
10    /// Plain UDP.
11    Udp,
12    /// UDP over TCP (ICE-TCP).
13    Tcp,
14    /// SSL over TCP (TURN).
15    SslTcp,
16    /// TLS over TCP (TURN).
17    Tls,
18}
19
20impl SfuProtocol {
21    #[allow(dead_code)]
22    pub(crate) fn from_str0m(p: str0m::net::Protocol) -> Self {
23        use str0m::net::Protocol;
24        match p {
25            Protocol::Udp => Self::Udp,
26            Protocol::Tcp => Self::Tcp,
27            Protocol::SslTcp => Self::SslTcp,
28            Protocol::Tls => Self::Tls,
29        }
30    }
31    #[allow(dead_code)]
32    pub(crate) fn to_str0m(self) -> str0m::net::Protocol {
33        use str0m::net::Protocol;
34        match self {
35            Self::Udp => Protocol::Udp,
36            Self::Tcp => Protocol::Tcp,
37            Self::SslTcp => Protocol::SslTcp,
38            Self::Tls => Protocol::Tls,
39        }
40    }
41}
42
43/// A datagram received from the network, ready to feed into an `SfuRtc` via
44/// [`Client::handle_input`][crate::Client::handle_input] (migration lands in Task 7).
45#[derive(Debug)]
46pub struct IncomingDatagram {
47    /// Wall-clock time the datagram was received.
48    pub received_at: Instant,
49    /// Transport protocol.
50    pub proto: SfuProtocol,
51    /// Remote address.
52    pub source: SocketAddr,
53    /// Local bound address.
54    pub destination: SocketAddr,
55    /// Raw wire bytes.
56    pub contents: Vec<u8>,
57}
58
59/// A datagram the SFU wants to send.
60#[derive(Debug)]
61pub struct OutgoingDatagram {
62    /// Transport protocol.
63    pub proto: SfuProtocol,
64    /// Source (local) address.
65    pub source: SocketAddr,
66    /// Destination (remote) address.
67    pub destination: SocketAddr,
68    /// Wire bytes.
69    pub contents: Vec<u8>,
70}
71
72impl OutgoingDatagram {
73    #[allow(dead_code)]
74    pub(crate) fn from_transmit(t: str0m::net::Transmit) -> Self {
75        Self {
76            proto: SfuProtocol::from_str0m(t.proto),
77            source: t.source,
78            destination: t.destination,
79            contents: t.contents.into(),
80        }
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn protocol_roundtrip() {
90        use str0m::net::Protocol;
91        for p in [
92            Protocol::Udp,
93            Protocol::Tcp,
94            Protocol::SslTcp,
95            Protocol::Tls,
96        ] {
97            let wrapped = SfuProtocol::from_str0m(p);
98            assert_eq!(wrapped.to_str0m(), p);
99        }
100    }
101}