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/src/client.rs | 19 ++++++++++--------- jsonrpc/src/codec.rs | 11 ++++++++++- jsonrpc/src/server.rs | 10 ++++++---- 3 files changed, 26 insertions(+), 14 deletions(-) (limited to 'jsonrpc/src') 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