From 059eed6ffa3a0ade449dfc5f5a7a97699e24394e Mon Sep 17 00:00:00 2001 From: hozan23 Date: Mon, 20 May 2024 00:14:15 +0200 Subject: jsonrpc: add ws cargo features --- jsonrpc/Cargo.toml | 15 ++++++++------- jsonrpc/examples/tokio_server/Cargo.lock | 13 ------------- jsonrpc/src/client.rs | 19 ++++++++++--------- jsonrpc/src/codec.rs | 11 ++++++++++- jsonrpc/src/server.rs | 10 ++++++---- 5 files changed, 34 insertions(+), 34 deletions(-) (limited to 'jsonrpc') diff --git a/jsonrpc/Cargo.toml b/jsonrpc/Cargo.toml index 719543e..ffab8bf 100644 --- a/jsonrpc/Cargo.toml +++ b/jsonrpc/Cargo.toml @@ -5,40 +5,41 @@ edition.workspace = true [features] default = ["smol"] +ws = ["karyon_net/ws", "async-tungstenite"] +unix = ["karyon_net/ws"] smol = [ "karyon_core/smol", "karyon_net/smol", "karyon_jsonrpc_macro/smol", - "dep:futures-rustls", - "async-tungstenite/async-std-runtime", + "futures-rustls", + "async-tungstenite?/async-std-runtime", ] tokio = [ "karyon_core/tokio", "karyon_net/tokio", "karyon_jsonrpc_macro/tokio", - "async-tungstenite/tokio-runtime", - "dep:tokio-rustls", + "tokio-rustls", + "async-tungstenite?/tokio-runtime", ] [dependencies] karyon_core = { 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 } log = "0.4.21" rand = "0.8.5" -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" async-trait = "0.1.77" +async-tungstenite = { version = "0.25.0", default-features = false, optional = true } + futures-rustls = { version = "0.25.1", optional = true } tokio-rustls = { version = "0.26.0", optional = true } diff --git a/jsonrpc/examples/tokio_server/Cargo.lock b/jsonrpc/examples/tokio_server/Cargo.lock index c42c2bf..a7fdb0b 100644 --- a/jsonrpc/examples/tokio_server/Cargo.lock +++ b/jsonrpc/examples/tokio_server/Cargo.lock @@ -492,17 +492,6 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" -[[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.65", -] - [[package]] name = "futures-sink" version = "0.3.30" @@ -522,7 +511,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-core", - "futures-macro", "futures-sink", "futures-task", "pin-project-lite", @@ -699,7 +687,6 @@ dependencies = [ "karyon_jsonrpc_macro", "karyon_net", "log", - "memchr", "rand", "serde", "serde_json", diff --git a/jsonrpc/src/client.rs b/jsonrpc/src/client.rs index 50d772b..b55943e 100644 --- a/jsonrpc/src/client.rs +++ b/jsonrpc/src/client.rs @@ -9,16 +9,15 @@ use futures_rustls::rustls; use tokio_rustls::rustls; use karyon_core::{async_util::timeout, util::random_32}; -use karyon_net::{ - tls::ClientTlsConfig, - ws::{ClientWsConfig, ClientWssConfig}, - Conn, Endpoint, ToEndpoint, -}; +use karyon_net::{tls::ClientTlsConfig, Conn, Endpoint, ToEndpoint}; -use crate::{ - codec::{JsonCodec, WsJsonCodec}, - message, Error, Result, -}; +#[cfg(feature = "ws")] +use karyon_net::ws::{ClientWsConfig, ClientWssConfig}; + +#[cfg(feature = "ws")] +use crate::codec::WsJsonCodec; + +use crate::{codec::JsonCodec, message, Error, Result}; /// Represents an RPC client pub struct Client { @@ -114,6 +113,7 @@ impl ClientBuilder { karyon_net::tcp::dial(&self.endpoint, Default::default(), JsonCodec {}).await?, ), }, + #[cfg(feature = "ws")] Endpoint::Ws(..) | Endpoint::Wss(..) => match self.tls_config { Some((conf, dns_name)) => Box::new( karyon_net::ws::dial( @@ -134,6 +134,7 @@ impl ClientBuilder { .await?, ), }, + #[cfg(all(feature = "unix", target_family = "unix"))] Endpoint::Unix(..) => Box::new( karyon_net::unix::dial(&self.endpoint, Default::default(), JsonCodec {}).await?, ), diff --git a/jsonrpc/src/codec.rs b/jsonrpc/src/codec.rs index 74415c7..29c6f13 100644 --- a/jsonrpc/src/codec.rs +++ b/jsonrpc/src/codec.rs @@ -1,10 +1,14 @@ +#[cfg(feature = "ws")] use async_tungstenite::tungstenite::Message; use karyon_net::{ - codec::{Codec, Decoder, Encoder, WebSocketCodec, WebSocketDecoder, WebSocketEncoder}, + codec::{Codec, Decoder, Encoder}, Error, Result, }; +#[cfg(feature = "ws")] +use karyon_net::codec::{WebSocketCodec, WebSocketDecoder, WebSocketEncoder}; + #[derive(Clone)] pub struct JsonCodec {} @@ -42,12 +46,16 @@ impl Decoder for JsonCodec { } } +#[cfg(feature = "ws")] #[derive(Clone)] pub struct WsJsonCodec {} + +#[cfg(feature = "ws")] impl WebSocketCodec for WsJsonCodec { type Item = serde_json::Value; } +#[cfg(feature = "ws")] impl WebSocketEncoder for WsJsonCodec { type EnItem = serde_json::Value; fn encode(&self, src: &Self::EnItem) -> Result { @@ -59,6 +67,7 @@ impl WebSocketEncoder for WsJsonCodec { } } +#[cfg(feature = "ws")] impl WebSocketDecoder for WsJsonCodec { type DeItem = serde_json::Value; fn decode(&self, src: &Message) -> Result { diff --git a/jsonrpc/src/server.rs b/jsonrpc/src/server.rs index 1cc7e1f..2155295 100644 --- a/jsonrpc/src/server.rs +++ b/jsonrpc/src/server.rs @@ -12,10 +12,10 @@ use karyon_core::async_util::{TaskGroup, TaskResult}; use karyon_net::{Conn, Endpoint, Listener, ToEndpoint}; -use crate::{ - codec::{JsonCodec, WsJsonCodec}, - message, Error, RPCService, Result, -}; +#[cfg(feature = "ws")] +use crate::codec::WsJsonCodec; + +use crate::{codec::JsonCodec, message, Error, RPCService, Result}; pub const INVALID_REQUEST_ERROR_MSG: &str = "Invalid request"; pub const FAILED_TO_PARSE_ERROR_MSG: &str = "Failed to parse"; @@ -230,6 +230,7 @@ impl ServerBuilder { .await?, ), }, + #[cfg(feature = "ws")] Endpoint::Ws(..) | Endpoint::Wss(..) => match &self.tls_config { Some(conf) => Box::new( karyon_net::ws::listen( @@ -249,6 +250,7 @@ impl ServerBuilder { .await?, ), }, + #[cfg(all(feature = "unix", target_family = "unix"))] Endpoint::Unix(..) => Box::new(karyon_net::unix::listen( &self.endpoint, Default::default(), -- cgit v1.2.3