use std::io::Result;
use std::sync::Arc;
use futures::future::BoxFuture;
use crate::{Config, PubKey};
use tx5_core::deps::serde_json;
#[cfg(any(feature = "backend-go-pion", feature = "backend-libdatachannel"))]
mod be_tx5_connection;
mod mem;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BackendModule {
#[cfg(feature = "backend-libdatachannel")]
LibDataChannel,
#[cfg(feature = "backend-go-pion")]
GoPion,
#[cfg(feature = "backend-webrtc-rs")]
WebrtcRs,
Mem,
}
impl Default for BackendModule {
#[allow(unreachable_code)]
fn default() -> Self {
#[cfg(feature = "backend-libdatachannel")]
return Self::LibDataChannel;
#[cfg(feature = "backend-go-pion")]
return Self::GoPion;
#[cfg(feature = "backend-webrtc-rs")]
return Self::WebrtcRs;
Self::Mem
}
}
impl BackendModule {
pub fn default_config(&self) -> serde_json::Value {
match self {
#[cfg(feature = "backend-libdatachannel")]
Self::LibDataChannel => be_tx5_connection::default_config(),
#[cfg(feature = "backend-go-pion")]
Self::GoPion => be_tx5_connection::default_config(),
#[cfg(feature = "backend-webrtc-rs")]
Self::WebrtcRs => todo!(),
Self::Mem => mem::default_config(),
}
}
pub async fn connect(
&self,
url: &str,
listener: bool,
config: &Arc<Config>,
) -> Result<(DynBackEp, DynBackEpRecvCon)> {
match self {
#[cfg(feature = "backend-libdatachannel")]
Self::LibDataChannel => {
be_tx5_connection::connect(config, url, listener).await
}
#[cfg(feature = "backend-go-pion")]
Self::GoPion => {
be_tx5_connection::connect(config, url, listener).await
}
#[cfg(feature = "backend-webrtc-rs")]
Self::WebrtcRs => todo!(),
Self::Mem => mem::connect(config, url, listener).await,
}
}
}
pub trait BackCon: 'static + Send + Sync {
fn send(&self, data: Vec<u8>) -> BoxFuture<'_, Result<()>>;
fn pub_key(&self) -> &PubKey;
fn is_using_webrtc(&self) -> bool;
fn get_stats(&self) -> tx5_connection::ConnStats;
}
pub type DynBackCon = Arc<dyn BackCon + 'static + Send + Sync>;
pub trait BackConRecvData: 'static + Send {
fn recv(&mut self) -> BoxFuture<'_, Option<Vec<u8>>>;
}
pub type DynBackConRecvData = Box<dyn BackConRecvData + 'static + Send>;
pub trait BackWaitCon: 'static + Send {
fn wait(
&mut self,
timeout: std::time::Duration,
recv_limit: Arc<tokio::sync::Semaphore>,
) -> BoxFuture<'static, Result<(DynBackCon, DynBackConRecvData)>>;
fn pub_key(&self) -> &PubKey;
}
pub type DynBackWaitCon = Box<dyn BackWaitCon + 'static + Send>;
pub trait BackEp: 'static + Send + Sync {
fn connect(&self, pub_key: PubKey)
-> BoxFuture<'_, Result<DynBackWaitCon>>;
fn pub_key(&self) -> &PubKey;
}
pub type DynBackEp = Arc<dyn BackEp + 'static + Send + Sync>;
pub trait BackEpRecvCon: 'static + Send {
fn recv(&mut self) -> BoxFuture<'_, Option<DynBackWaitCon>>;
}
pub type DynBackEpRecvCon = Box<dyn BackEpRecvCon + 'static + Send>;