rtc_shared/
transport.rs

1use bytes::BytesMut;
2use std::net::{IpAddr, Ipv4Addr, SocketAddr};
3use std::time::Instant;
4
5/// Explicit congestion notification codepoint
6#[repr(u8)]
7#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
8pub enum EcnCodepoint {
9    #[doc(hidden)]
10    Ect0 = 0b10,
11    #[doc(hidden)]
12    Ect1 = 0b01,
13    #[doc(hidden)]
14    Ce = 0b11,
15}
16
17impl EcnCodepoint {
18    /// Create new object from the given bits
19    pub fn from_bits(x: u8) -> Option<Self> {
20        use self::EcnCodepoint::*;
21        Some(match x & 0b11 {
22            0b10 => Ect0,
23            0b01 => Ect1,
24            0b11 => Ce,
25            _ => {
26                return None;
27            }
28        })
29    }
30}
31
32/// Type of transport protocol, either UDP or TCP
33#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
34pub enum TransportProtocol {
35    /// UDP
36    #[default]
37    UDP,
38    /// TCP
39    TCP,
40}
41
42/// Transport Context with local address, peer address, ECN, protocol, etc.
43#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
44pub struct TransportContext {
45    /// Local socket address, either IPv4 or IPv6
46    pub local_addr: SocketAddr,
47    /// Peer socket address, either IPv4 or IPv6
48    pub peer_addr: SocketAddr,
49    /// Type of transport protocol, either UDP or TCP
50    pub transport_protocol: TransportProtocol,
51    /// Explicit congestion notification bits to set on the packet
52    pub ecn: Option<EcnCodepoint>,
53}
54
55impl Default for TransportContext {
56    fn default() -> Self {
57        Self {
58            local_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0),
59            peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 65535),
60            transport_protocol: TransportProtocol::UDP,
61            ecn: None,
62        }
63    }
64}
65
66/// A generic transmit with [TransportContext]
67pub struct TransportMessage<T> {
68    /// Received/Sent time
69    pub now: Instant,
70    /// A transport context with [local_addr](TransportContext::local_addr) and [peer_addr](TransportContext::peer_addr)
71    pub transport: TransportContext,
72    /// Message body with generic type
73    pub message: T,
74}
75
76/// BytesMut type transmit with [TransportContext]
77pub type TaggedBytesMut = TransportMessage<BytesMut>;
78
79/// String type transmit with [TransportContext]
80pub type TaggedString = TransportMessage<String>;
81
82/// Four Tuple consists of local address and peer address
83#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
84pub struct FourTuple {
85    /// Local socket address, either IPv4 or IPv6
86    pub local_addr: SocketAddr,
87    /// Peer socket address, either IPv4 or IPv6
88    pub peer_addr: SocketAddr,
89}
90
91impl From<&TransportContext> for FourTuple {
92    fn from(value: &TransportContext) -> Self {
93        Self {
94            local_addr: value.local_addr,
95            peer_addr: value.peer_addr,
96        }
97    }
98}
99
100impl From<TransportContext> for FourTuple {
101    fn from(value: TransportContext) -> Self {
102        Self {
103            local_addr: value.local_addr,
104            peer_addr: value.peer_addr,
105        }
106    }
107}
108
109/// Five Tuple consists of local address, peer address and protocol
110#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
111pub struct FiveTuple {
112    /// Local socket address, either IPv4 or IPv6
113    pub local_addr: SocketAddr,
114    /// Peer socket address, either IPv4 or IPv6
115    pub peer_addr: SocketAddr,
116    /// Type of protocol, either UDP or TCP
117    pub transport_protocol: TransportProtocol,
118}
119
120impl From<&TransportContext> for FiveTuple {
121    fn from(value: &TransportContext) -> Self {
122        Self {
123            local_addr: value.local_addr,
124            peer_addr: value.peer_addr,
125            transport_protocol: value.transport_protocol,
126        }
127    }
128}
129
130impl From<TransportContext> for FiveTuple {
131    fn from(value: TransportContext) -> Self {
132        Self {
133            local_addr: value.local_addr,
134            peer_addr: value.peer_addr,
135            transport_protocol: value.transport_protocol,
136        }
137    }
138}