use super::portmap::{NoSessionPortMapper, SessionPortMapper};
use super::WebRtcHandle;
use crate::messages::OpenOk;
use async_trait::async_trait;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Instant;
#[async_trait]
pub trait WebRtcCarrier: Send + Sync {
fn now(&self) -> Instant {
Instant::now()
}
async fn stun_servers(&self) -> Vec<SocketAddr>;
fn bind_addr(&self) -> Option<SocketAddr> {
None
}
fn port_mapper(&self) -> Arc<dyn SessionPortMapper> {
Arc::new(NoSessionPortMapper)
}
fn mapping_description(&self) -> &str {
"webrtc"
}
async fn bind_session(
&self,
_identity: &OpenOk,
_peer: [u8; 32],
_handle: &WebRtcHandle,
) -> bool {
true
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct NullWebRtcCarrier;
#[async_trait]
impl WebRtcCarrier for NullWebRtcCarrier {
async fn stun_servers(&self) -> Vec<SocketAddr> {
Vec::new()
}
}
#[derive(Clone)]
pub struct StaticWebRtcCarrier {
pub stun: Vec<SocketAddr>,
pub bind: Option<SocketAddr>,
pub mapper: Arc<dyn SessionPortMapper>,
pub mapping_description: &'static str,
}
impl Default for StaticWebRtcCarrier {
fn default() -> Self {
Self {
stun: Vec::new(),
bind: None,
mapper: Arc::new(NoSessionPortMapper),
mapping_description: "webrtc",
}
}
}
#[async_trait]
impl WebRtcCarrier for StaticWebRtcCarrier {
async fn stun_servers(&self) -> Vec<SocketAddr> {
self.stun.clone()
}
fn bind_addr(&self) -> Option<SocketAddr> {
self.bind
}
fn port_mapper(&self) -> Arc<dyn SessionPortMapper> {
self.mapper.clone()
}
fn mapping_description(&self) -> &str {
self.mapping_description
}
}