sansio_codec/transport/
mod.rs

1#![allow(dead_code)]
2
3//! Transport abstraction for TCP and UDP
4pub use async_transport::EcnCodepoint;
5use bytes::BytesMut;
6use std::net::SocketAddr;
7use std::str::FromStr;
8use std::time::Instant;
9
10/// Type of transport protocol, either UDP or TCP
11#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
12pub enum TransportProtocol {
13    /// UDP
14    #[default]
15    UDP,
16    /// TCP
17    TCP,
18}
19
20/// Transport Context with local address, peer address, ECN, protocol, etc.
21#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
22pub struct TransportContext {
23    /// Local socket address, either IPv4 or IPv6
24    pub local_addr: SocketAddr,
25    /// Peer socket address, either IPv4 or IPv6
26    pub peer_addr: SocketAddr,
27    /// Type of transport protocol, either UDP or TCP
28    pub transport_protocol: TransportProtocol,
29    /// Explicit congestion notification bits to set on the packet
30    pub ecn: Option<EcnCodepoint>,
31}
32
33impl Default for TransportContext {
34    fn default() -> Self {
35        Self {
36            local_addr: SocketAddr::from_str("0.0.0.0:0").unwrap(),
37            peer_addr: SocketAddr::from_str("0.0.0.0:0").unwrap(),
38            transport_protocol: TransportProtocol::UDP,
39            ecn: None,
40        }
41    }
42}
43
44/// A generic transmit with [TransportContext]
45pub struct Transmit<T> {
46    /// Received/Sent time
47    pub now: Instant,
48    /// A transport context with [local_addr](TransportContext::local_addr) and [peer_addr](TransportContext::peer_addr)
49    pub transport: TransportContext,
50    /// Message body with generic type
51    pub message: T,
52}
53
54/// BytesMut type transmit with [TransportContext]
55pub type TaggedBytesMut = Transmit<BytesMut>;
56
57/// String type transmit with [TransportContext]
58pub type TaggedString = Transmit<String>;
59
60/// Four Tuple consists of local address and peer address
61#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
62pub struct FourTuple {
63    /// Local socket address, either IPv4 or IPv6
64    pub local_addr: SocketAddr,
65    /// Peer socket address, either IPv4 or IPv6
66    pub peer_addr: SocketAddr,
67}
68
69impl From<&TransportContext> for FourTuple {
70    fn from(value: &TransportContext) -> Self {
71        Self {
72            local_addr: value.local_addr,
73            peer_addr: value.peer_addr,
74        }
75    }
76}
77
78impl From<TransportContext> for FourTuple {
79    fn from(value: TransportContext) -> Self {
80        Self {
81            local_addr: value.local_addr,
82            peer_addr: value.peer_addr,
83        }
84    }
85}
86
87/// Five Tuple consists of local address, peer address and protocol
88#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
89pub struct FiveTuple {
90    /// Local socket address, either IPv4 or IPv6
91    pub local_addr: SocketAddr,
92    /// Peer socket address, either IPv4 or IPv6
93    pub peer_addr: SocketAddr,
94    /// Type of protocol, either UDP or TCP
95    pub transport_protocol: TransportProtocol,
96}
97
98impl From<&TransportContext> for FiveTuple {
99    fn from(value: &TransportContext) -> Self {
100        Self {
101            local_addr: value.local_addr,
102            peer_addr: value.peer_addr,
103            transport_protocol: value.transport_protocol,
104        }
105    }
106}
107
108impl From<TransportContext> for FiveTuple {
109    fn from(value: TransportContext) -> Self {
110        Self {
111            local_addr: value.local_addr,
112            peer_addr: value.peer_addr,
113            transport_protocol: value.transport_protocol,
114        }
115    }
116}