pub mod tcp;
pub mod udp;
pub mod wrtc;
pub mod ws;
use super::*;
use std::io;
#[derive(Debug)]
pub(crate) enum ProtocolNetworkConnection {
RawTcp(tcp::RawTcpNetworkConnection),
WsAccepted(ws::WebSocketNetworkConnectionAccepted),
Ws(ws::WebsocketNetworkConnectionWS),
#[cfg(feature = "enable-protocol-wss")]
Wss(ws::WebsocketNetworkConnectionWSS),
}
impl ProtocolNetworkConnection {
pub async fn connect(
registry: VeilidComponentRegistry,
local_address: Option<SocketAddr>,
dial_info: &DialInfo,
timeout_ms: u32,
address_filter: &AddressFilter,
) -> io::Result<NetworkResult<ProtocolNetworkConnection>> {
if address_filter.is_ip_addr_punished(dial_info.address().ip_addr()) {
return Ok(NetworkResult::no_connection_other("punished"));
}
match dial_info.protocol_type() {
ProtocolType::UDP => {
panic!("Should not connect to UDP dialinfo");
}
ProtocolType::TCP => {
tcp::RawTcpProtocolHandler::connect(
registry,
local_address,
dial_info.to_socket_addr(),
timeout_ms,
)
.await
}
ProtocolType::WS => {
ws::WebsocketProtocolHandler::connect(
registry,
local_address,
dial_info,
timeout_ms,
)
.await
}
#[cfg(feature = "enable-protocol-wss")]
ProtocolType::WSS => {
ws::WebsocketProtocolHandler::connect(
registry,
local_address,
dial_info,
timeout_ms,
)
.await
}
}
}
pub fn flow(&self) -> Flow {
match self {
Self::RawTcp(t) => t.flow(),
Self::WsAccepted(w) => w.flow(),
Self::Ws(w) => w.flow(),
#[cfg(feature = "enable-protocol-wss")]
Self::Wss(w) => w.flow(),
}
}
pub async fn close(&self) -> io::Result<NetworkResult<()>> {
match self {
Self::RawTcp(t) => t.close(),
Self::WsAccepted(w) => w.close().await,
Self::Ws(w) => w.close().await,
#[cfg(feature = "enable-protocol-wss")]
Self::Wss(w) => w.close().await,
}
}
pub async fn send(&self, message: Bytes) -> io::Result<NetworkResult<()>> {
match self {
Self::RawTcp(t) => t.send(message).await,
Self::WsAccepted(w) => w.send(message).await,
Self::Ws(w) => w.send(message).await,
#[cfg(feature = "enable-protocol-wss")]
Self::Wss(w) => w.send(message).await,
}
}
pub async fn recv(&self) -> io::Result<NetworkResult<Bytes>> {
match self {
Self::RawTcp(t) => t.recv().await,
Self::WsAccepted(w) => w.recv().await,
Self::Ws(w) => w.recv().await,
#[cfg(feature = "enable-protocol-wss")]
Self::Wss(w) => w.recv().await,
}
}
}