node_device_session/webrtc/
carrier.rs1use super::portmap::{NoSessionPortMapper, SessionPortMapper};
4use super::WebRtcHandle;
5use crate::messages::OpenOk;
6use async_trait::async_trait;
7use std::net::SocketAddr;
8use std::sync::Arc;
9use std::time::Instant;
10
11#[async_trait]
16pub trait WebRtcCarrier: Send + Sync {
17 fn now(&self) -> Instant {
19 Instant::now()
20 }
21
22 async fn stun_servers(&self) -> Vec<SocketAddr>;
24
25 fn bind_addr(&self) -> Option<SocketAddr> {
27 None
28 }
29
30 fn port_mapper(&self) -> Arc<dyn SessionPortMapper> {
32 Arc::new(NoSessionPortMapper)
33 }
34
35 fn mapping_description(&self) -> &str {
37 "webrtc"
38 }
39
40 async fn bind_session(
45 &self,
46 _identity: &OpenOk,
47 _peer: [u8; 32],
48 _handle: &WebRtcHandle,
49 ) -> bool {
50 true
51 }
52}
53
54#[derive(Debug, Default, Clone, Copy)]
56pub struct NullWebRtcCarrier;
57
58#[async_trait]
59impl WebRtcCarrier for NullWebRtcCarrier {
60 async fn stun_servers(&self) -> Vec<SocketAddr> {
61 Vec::new()
62 }
63}
64
65#[derive(Clone)]
67pub struct StaticWebRtcCarrier {
68 pub stun: Vec<SocketAddr>,
70 pub bind: Option<SocketAddr>,
72 pub mapper: Arc<dyn SessionPortMapper>,
74 pub mapping_description: &'static str,
76}
77
78impl Default for StaticWebRtcCarrier {
79 fn default() -> Self {
80 Self {
81 stun: Vec::new(),
82 bind: None,
83 mapper: Arc::new(NoSessionPortMapper),
84 mapping_description: "webrtc",
85 }
86 }
87}
88
89#[async_trait]
90impl WebRtcCarrier for StaticWebRtcCarrier {
91 async fn stun_servers(&self) -> Vec<SocketAddr> {
92 self.stun.clone()
93 }
94
95 fn bind_addr(&self) -> Option<SocketAddr> {
96 self.bind
97 }
98
99 fn port_mapper(&self) -> Arc<dyn SessionPortMapper> {
100 self.mapper.clone()
101 }
102
103 fn mapping_description(&self) -> &str {
104 self.mapping_description
105 }
106}