diff options
author | hozan23 <hozan23@karyontech.net> | 2024-05-19 23:41:31 +0200 |
---|---|---|
committer | hozan23 <hozan23@karyontech.net> | 2024-05-19 23:41:31 +0200 |
commit | f6f44784fff5488bb59d563ee7ff7b94c08a48c1 (patch) | |
tree | 63fa6fa0d620748a92d819f4773773ea9d53afc5 | |
parent | a6016c7eeb11fc8aeaa1a3b160b970b15362695d (diff) |
use cargo features to enable/disable protocols for net crate
-rw-r--r-- | Cargo.lock | 29 | ||||
-rw-r--r-- | jsonrpc/Cargo.toml | 8 | ||||
-rw-r--r-- | jsonrpc/examples/tokio_server/Cargo.lock | 16 | ||||
-rw-r--r-- | net/Cargo.toml | 34 | ||||
-rw-r--r-- | net/examples/tcp_codec_tokio/Cargo.lock | 17 | ||||
-rw-r--r-- | net/examples/tcp_codec_tokio/Cargo.toml | 2 | ||||
-rw-r--r-- | net/src/codec/mod.rs | 3 | ||||
-rw-r--r-- | net/src/error.rs | 2 | ||||
-rw-r--r-- | net/src/lib.rs | 17 | ||||
-rw-r--r-- | net/src/stream/mod.rs | 2 | ||||
-rw-r--r-- | net/src/stream/websocket.rs | 11 | ||||
-rw-r--r-- | net/src/transports/mod.rs | 5 | ||||
-rw-r--r-- | net/src/transports/ws.rs | 23 | ||||
-rw-r--r-- | p2p/Cargo.toml | 10 |
14 files changed, 94 insertions, 85 deletions
@@ -359,19 +359,6 @@ dependencies = [ ] [[package]] -name = "asynchronous-codec" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a860072022177f903e59730004fb5dc13db9275b79bb2aef7ba8ce831956c233" -dependencies = [ - "bytes", - "futures-sink", - "futures-util", - "memchr", - "pin-project-lite", -] - -[[package]] name = "atomic-waker" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1026,17 +1013,6 @@ dependencies = [ ] [[package]] -name = "futures-macro" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.63", -] - -[[package]] name = "futures-rustls" version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1066,11 +1042,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-core", - "futures-io", - "futures-macro", "futures-sink", "futures-task", - "memchr", "pin-project-lite", "pin-utils", "slab", @@ -1288,7 +1261,6 @@ dependencies = [ "karyon_jsonrpc_macro", "karyon_net", "log", - "memchr", "rand", "rcgen 0.13.1", "rustls-pemfile", @@ -1317,7 +1289,6 @@ dependencies = [ "async-channel 2.3.0", "async-trait", "async-tungstenite", - "asynchronous-codec", "bincode", "futures-rustls", "futures-util", diff --git a/jsonrpc/Cargo.toml b/jsonrpc/Cargo.toml index cf49d6e..719543e 100644 --- a/jsonrpc/Cargo.toml +++ b/jsonrpc/Cargo.toml @@ -22,7 +22,12 @@ tokio = [ [dependencies] karyon_core = { workspace = true, default-features = false } -karyon_net = { workspace = true, default-features = false } +karyon_net = { workspace = true, default-features = false, features = [ + "tcp", + "unix", + "tls", + "ws", +] } karyon_jsonrpc_macro = { path = "jsonrpc_macro", default-features = false } @@ -32,7 +37,6 @@ async-tungstenite = { version = "0.25.0", default-features = false } serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.114" thiserror = "1.0.58" -memchr = "2.7.1" async-trait = "0.1.77" futures-rustls = { version = "0.25.1", optional = true } diff --git a/jsonrpc/examples/tokio_server/Cargo.lock b/jsonrpc/examples/tokio_server/Cargo.lock index 0fe9893..c42c2bf 100644 --- a/jsonrpc/examples/tokio_server/Cargo.lock +++ b/jsonrpc/examples/tokio_server/Cargo.lock @@ -128,19 +128,6 @@ dependencies = [ ] [[package]] -name = "asynchronous-codec" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a860072022177f903e59730004fb5dc13db9275b79bb2aef7ba8ce831956c233" -dependencies = [ - "bytes", - "futures-sink", - "futures-util", - "memchr", - "pin-project-lite", -] - -[[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -535,11 +522,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-core", - "futures-io", "futures-macro", "futures-sink", "futures-task", - "memchr", "pin-project-lite", "pin-utils", "slab", @@ -739,7 +724,6 @@ dependencies = [ "async-channel", "async-trait", "async-tungstenite", - "asynchronous-codec", "bincode", "futures-util", "karyon_core", diff --git a/net/Cargo.toml b/net/Cargo.toml index 304cbb2..717f603 100644 --- a/net/Cargo.toml +++ b/net/Cargo.toml @@ -6,35 +6,49 @@ edition.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["smol"] +default = ["smol", "all-protocols"] +all-protocols = ["tcp", "tls", "ws", "udp", "unix"] +stream = ["dep:pin-project-lite", "dep:futures-util"] +tcp = ["stream"] +tls = ["dep:rustls-pki-types", "tcp"] +ws = ["dep:async-tungstenite", "tcp"] +udp = [] +unix = ["stream"] smol = [ "karyon_core/smol", - "async-tungstenite/async-std-runtime", - "dep:futures-rustls", + "async-tungstenite?/async-std-runtime", + # TODO: use tls feature + "futures-rustls", ] tokio = [ "karyon_core/tokio", - "async-tungstenite/tokio-runtime", + "async-tungstenite?/tokio-runtime", "dep:tokio", - "dep:tokio-rustls", + # TODO: use tls feature + "tokio-rustls", ] [dependencies] karyon_core = { workspace = true, default-features = false } -pin-project-lite = "0.2.13" async-trait = "0.1.77" log = "0.4.21" bincode = { version = "2.0.0-rc.3", features = ["derive"] } thiserror = "1.0.58" url = "2.5.0" -async-tungstenite = { version = "0.25.0", default-features = false } -asynchronous-codec = "0.7.0" -futures-util = "0.3.30" async-channel = "2.3.0" -rustls-pki-types = "1.7.0" +futures-util = { version = "0.3.30", default-features = false, features = [ + "sink", +], optional = true } +pin-project-lite = { version = "0.2.13", optional = true } + +# websocket deps +async-tungstenite = { version = "0.25.0", default-features = false, optional = true } + +# tls deps +rustls-pki-types = { version = "1.7.0", optional = true } futures-rustls = { version = "0.25.1", optional = true } tokio-rustls = { version = "0.26.0", optional = true } tokio = { version = "1.37.0", features = ["io-util"], optional = true } diff --git a/net/examples/tcp_codec_tokio/Cargo.lock b/net/examples/tcp_codec_tokio/Cargo.lock index b9665ab..a9f015b 100644 --- a/net/examples/tcp_codec_tokio/Cargo.lock +++ b/net/examples/tcp_codec_tokio/Cargo.lock @@ -79,19 +79,6 @@ dependencies = [ ] [[package]] -name = "asynchronous-codec" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a860072022177f903e59730004fb5dc13db9275b79bb2aef7ba8ce831956c233" -dependencies = [ - "bytes", - "futures-sink", - "futures-util", - "memchr", - "pin-project-lite", -] - -[[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -457,11 +444,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-core", - "futures-io", "futures-macro", "futures-sink", "futures-task", - "memchr", "pin-project-lite", "pin-utils", "slab", @@ -621,13 +606,11 @@ dependencies = [ "async-channel", "async-trait", "async-tungstenite", - "asynchronous-codec", "bincode", "futures-util", "karyon_core", "log", "pin-project-lite", - "rustls-pki-types", "thiserror", "tokio", "tokio-rustls", diff --git a/net/examples/tcp_codec_tokio/Cargo.toml b/net/examples/tcp_codec_tokio/Cargo.toml index e21b2b7..c72be64 100644 --- a/net/examples/tcp_codec_tokio/Cargo.toml +++ b/net/examples/tcp_codec_tokio/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [workspace] [dependencies] -karyon_net = { path = "../../", default-features = false, features = ["tokio"] } +karyon_net = { path = "../../", default-features = false, features = ["tokio", "tcp"] } karyon_core = { path = "../../../core", default-features = false, features = ["tokio"] } tokio = { version = "1.37.0", features = ["full"] } diff --git a/net/src/codec/mod.rs b/net/src/codec/mod.rs index 565cb07..43a02f3 100644 --- a/net/src/codec/mod.rs +++ b/net/src/codec/mod.rs @@ -1,9 +1,12 @@ mod bytes_codec; mod length_codec; +#[cfg(feature = "ws")] mod websocket; pub use bytes_codec::BytesCodec; pub use length_codec::LengthCodec; + +#[cfg(feature = "ws")] pub use websocket::{WebSocketCodec, WebSocketDecoder, WebSocketEncoder}; use crate::Result; diff --git a/net/src/error.rs b/net/src/error.rs index ee93168..102a343 100644 --- a/net/src/error.rs +++ b/net/src/error.rs @@ -37,6 +37,7 @@ pub enum Error { #[error(transparent)] ChannelRecv(#[from] async_channel::RecvError), + #[cfg(feature = "ws")] #[error("Ws Error: {0}")] WsError(#[from] async_tungstenite::tungstenite::Error), @@ -48,6 +49,7 @@ pub enum Error { #[error("Tls Error: {0}")] Rustls(#[from] tokio_rustls::rustls::Error), + #[cfg(feature = "tls")] #[error("Invalid DNS Name: {0}")] InvalidDnsNameError(#[from] rustls_pki_types::InvalidDnsNameError), diff --git a/net/src/lib.rs b/net/src/lib.rs index ddb53cf..cd5fc8b 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -3,6 +3,7 @@ mod connection; mod endpoint; mod error; mod listener; +#[cfg(feature = "stream")] mod stream; mod transports; @@ -10,9 +11,23 @@ pub use { connection::{Conn, Connection, ToConn}, endpoint::{Addr, Endpoint, Port, ToEndpoint}, listener::{ConnListener, Listener, ToListener}, - transports::{tcp, tls, udp, unix, ws}, }; +#[cfg(feature = "tcp")] +pub use transports::tcp; + +#[cfg(feature = "tls")] +pub use transports::tls; + +#[cfg(feature = "ws")] +pub use transports::ws; + +#[cfg(feature = "udp")] +pub use transports::udp; + +#[cfg(all(feature = "unix", target_family = "unix"))] +pub use transports::unix; + /// Represents karyon's Net Error pub use error::Error; diff --git a/net/src/stream/mod.rs b/net/src/stream/mod.rs index b792292..ce48a77 100644 --- a/net/src/stream/mod.rs +++ b/net/src/stream/mod.rs @@ -1,6 +1,8 @@ mod buffer; +#[cfg(feature = "ws")] mod websocket; +#[cfg(feature = "ws")] pub use websocket::WsStream; use std::{ diff --git a/net/src/stream/websocket.rs b/net/src/stream/websocket.rs index 2552eaf..9d41626 100644 --- a/net/src/stream/websocket.rs +++ b/net/src/stream/websocket.rs @@ -6,9 +6,9 @@ use std::{ use async_tungstenite::tungstenite::Message; use futures_util::{Sink, SinkExt, Stream, StreamExt}; -#[cfg(feature = "smol")] +#[cfg(all(feature = "smol", feature = "tls"))] use futures_rustls::TlsStream; -#[cfg(feature = "tokio")] +#[cfg(all(feature = "tokio", feature = "tls"))] use tokio_rustls::TlsStream; use karyon_core::async_runtime::net::TcpStream; @@ -37,6 +37,7 @@ where } } + #[cfg(feature = "tls")] pub fn new_wss(conn: WebSocketStream<TlsStream<TcpStream>>, codec: C) -> Self { Self { inner: InnerWSConn::Tls(conn), @@ -59,6 +60,7 @@ where enum InnerWSConn { Plain(WebSocketStream<TcpStream>), + #[cfg(feature = "tls")] Tls(WebSocketStream<TlsStream<TcpStream>>), } @@ -68,6 +70,7 @@ impl Sink<Message> for InnerWSConn { fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> { match &mut *self { InnerWSConn::Plain(s) => Pin::new(s).poll_ready(cx).map_err(Error::from), + #[cfg(feature = "tls")] InnerWSConn::Tls(s) => Pin::new(s).poll_ready(cx).map_err(Error::from), } } @@ -75,6 +78,7 @@ impl Sink<Message> for InnerWSConn { fn start_send(mut self: Pin<&mut Self>, item: Message) -> Result<()> { match &mut *self { InnerWSConn::Plain(s) => Pin::new(s).start_send(item).map_err(Error::from), + #[cfg(feature = "tls")] InnerWSConn::Tls(s) => Pin::new(s).start_send(item).map_err(Error::from), } } @@ -82,6 +86,7 @@ impl Sink<Message> for InnerWSConn { fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> { match &mut *self { InnerWSConn::Plain(s) => Pin::new(s).poll_flush(cx).map_err(Error::from), + #[cfg(feature = "tls")] InnerWSConn::Tls(s) => Pin::new(s).poll_flush(cx).map_err(Error::from), } } @@ -89,6 +94,7 @@ impl Sink<Message> for InnerWSConn { fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> { match &mut *self { InnerWSConn::Plain(s) => Pin::new(s).poll_close(cx).map_err(Error::from), + #[cfg(feature = "tls")] InnerWSConn::Tls(s) => Pin::new(s).poll_close(cx).map_err(Error::from), } .map_err(Error::from) @@ -101,6 +107,7 @@ impl Stream for InnerWSConn { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { match &mut *self { InnerWSConn::Plain(s) => Pin::new(s).poll_next(cx).map_err(Error::from), + #[cfg(feature = "tls")] InnerWSConn::Tls(s) => Pin::new(s).poll_next(cx).map_err(Error::from), } } 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!(), } } diff --git a/p2p/Cargo.toml b/p2p/Cargo.toml index 3147d39..d9eea5f 100644 --- a/p2p/Cargo.toml +++ b/p2p/Cargo.toml @@ -14,12 +14,16 @@ tokio = ["karyon_core/tokio", "karyon_net/tokio", "dep:tokio-rustls"] karyon_core = { workspace = true, features = [ "crypto", ], default-features = false } -karyon_net = { workspace = true, default-features = false } +karyon_net = { workspace = true, default-features = false, features = [ + "tcp", + "tls", + "udp", +] } async-trait = "0.1.77" async-channel = "2.3.0" futures-util = { version = "0.3.5", features = [ - "std", + "alloc", ], default-features = false } log = "0.4.21" chrono = "0.4.35" @@ -46,5 +50,3 @@ ctrlc = "3.4.4" easy-parallel = "3.3.1" env_logger = "0.11.3" smol = "2.0.0" - - |