aboutsummaryrefslogtreecommitdiff
path: root/net/src/transports
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-05-19 23:41:31 +0200
committerhozan23 <hozan23@karyontech.net>2024-05-19 23:41:31 +0200
commitf6f44784fff5488bb59d563ee7ff7b94c08a48c1 (patch)
tree63fa6fa0d620748a92d819f4773773ea9d53afc5 /net/src/transports
parenta6016c7eeb11fc8aeaa1a3b160b970b15362695d (diff)
use cargo features to enable/disable protocols for net crate
Diffstat (limited to 'net/src/transports')
-rw-r--r--net/src/transports/mod.rs5
-rw-r--r--net/src/transports/ws.rs23
2 files changed, 25 insertions, 3 deletions
diff --git a/net/src/transports/mod.rs b/net/src/transports/mod.rs
index 14ef6f3..c7d684b 100644
--- a/net/src/transports/mod.rs
+++ b/net/src/transports/mod.rs
@@ -1,5 +1,10 @@
+#[cfg(feature = "tcp")]
pub mod tcp;
+#[cfg(feature = "tls")]
pub mod tls;
+#[cfg(feature = "udp")]
pub mod udp;
+#[cfg(all(feature = "unix", target_family = "unix"))]
pub mod unix;
+#[cfg(feature = "ws")]
pub mod ws;
diff --git a/net/src/transports/ws.rs b/net/src/transports/ws.rs
index 17fe924..6107999 100644
--- a/net/src/transports/ws.rs
+++ b/net/src/transports/ws.rs
@@ -1,14 +1,18 @@
-use std::{net::SocketAddr, sync::Arc};
+use std::net::SocketAddr;
+
+#[cfg(feature = "tls")]
+use std::sync::Arc;
use async_trait::async_trait;
+#[cfg(feature = "tls")]
use rustls_pki_types as pki_types;
#[cfg(feature = "tokio")]
use async_tungstenite::tokio as async_tungstenite;
-#[cfg(feature = "smol")]
+#[cfg(all(feature = "smol", feature = "tls"))]
use futures_rustls::{rustls, TlsAcceptor, TlsConnector};
-#[cfg(feature = "tokio")]
+#[cfg(all(feature = "tokio", feature = "tls"))]
use tokio_rustls::{rustls, TlsAcceptor, TlsConnector};
use karyon_core::async_runtime::{
@@ -30,12 +34,14 @@ use super::tcp::TcpConfig;
/// WSS configuration
#[derive(Clone)]
pub struct ServerWssConfig {
+ #[cfg(feature = "tls")]
pub server_config: rustls::ServerConfig,
}
/// WSS configuration
#[derive(Clone)]
pub struct ClientWssConfig {
+ #[cfg(feature = "tls")]
pub client_config: rustls::ClientConfig,
pub dns_name: String,
}
@@ -104,6 +110,7 @@ pub struct WsListener<C> {
inner: TcpListener,
config: ServerWsConfig,
codec: C,
+ #[cfg(feature = "tls")]
tls_acceptor: Option<TlsAcceptor>,
}
@@ -125,6 +132,7 @@ where
socket.set_nodelay(self.config.tcp_config.nodelay)?;
match &self.config.wss_config {
+ #[cfg(feature = "tls")]
Some(_) => match &self.tls_acceptor {
Some(acceptor) => {
let peer_endpoint = socket.peer_addr().map(Endpoint::new_wss_addr)?;
@@ -152,6 +160,8 @@ where
local_endpoint,
)))
}
+ #[cfg(not(feature = "tls"))]
+ _ => unreachable!(),
}
}
}
@@ -166,6 +176,7 @@ where
socket.set_nodelay(config.tcp_config.nodelay)?;
match &config.wss_config {
+ #[cfg(feature = "tls")]
Some(conf) => {
let peer_endpoint = socket.peer_addr().map(Endpoint::new_wss_addr)?;
let local_endpoint = socket.local_addr().map(Endpoint::new_wss_addr)?;
@@ -193,6 +204,8 @@ where
local_endpoint,
))
}
+ #[cfg(not(feature = "tls"))]
+ _ => unreachable!(),
}
}
@@ -206,6 +219,7 @@ pub async fn listen<C>(
let listener = TcpListener::bind(addr).await?;
match &config.wss_config {
+ #[cfg(feature = "tls")]
Some(conf) => {
let acceptor = TlsAcceptor::from(Arc::new(conf.server_config.clone()));
Ok(WsListener {
@@ -219,8 +233,11 @@ pub async fn listen<C>(
inner: listener,
config,
codec,
+ #[cfg(feature = "tls")]
tls_acceptor: None,
}),
+ #[cfg(not(feature = "tls"))]
+ _ => unreachable!(),
}
}