From 0a2c0dbc6c1afd56e9db0d93eef1ae05fe81a30b Mon Sep 17 00:00:00 2001 From: hozan23 Date: Sat, 22 Jun 2024 05:38:24 +0200 Subject: jsonrpc: add tcp, tls and ws features for compilation --- jsonrpc/src/client/builder.rs | 66 +++++++++++++++++++++++++++---------------- jsonrpc/src/error.rs | 5 +++- jsonrpc/src/lib.rs | 5 +++- jsonrpc/src/server/builder.rs | 59 +++++++++++++++++++++++--------------- 4 files changed, 87 insertions(+), 48 deletions(-) (limited to 'jsonrpc/src') diff --git a/jsonrpc/src/client/builder.rs b/jsonrpc/src/client/builder.rs index c34d2da..5a7936c 100644 --- a/jsonrpc/src/client/builder.rs +++ b/jsonrpc/src/client/builder.rs @@ -1,20 +1,24 @@ use std::sync::{atomic::AtomicBool, Arc}; -#[cfg(feature = "smol")] -use futures_rustls::rustls; -#[cfg(feature = "tokio")] -use tokio_rustls::rustls; - use karyon_core::async_util::TaskGroup; -use karyon_net::{tls::ClientTlsConfig, Conn, Endpoint, ToEndpoint}; +use karyon_net::{Conn, Endpoint, ToEndpoint}; + +#[cfg(feature = "tls")] +use karyon_net::{async_rustls::rustls, tls::ClientTlsConfig}; #[cfg(feature = "ws")] -use karyon_net::ws::{ClientWsConfig, ClientWssConfig}; +use karyon_net::ws::ClientWsConfig; + +#[cfg(all(feature = "ws", feature = "tls"))] +use karyon_net::ws::ClientWssConfig; #[cfg(feature = "ws")] use crate::codec::WsJsonCodec; -use crate::{codec::JsonCodec, Error, Result, TcpConfig}; +#[cfg(feature = "tcp")] +use crate::TcpConfig; + +use crate::{codec::JsonCodec, Error, Result}; use super::{Client, MessageDispatcher, Subscriptions}; @@ -42,8 +46,10 @@ impl Client { Ok(ClientBuilder { endpoint, timeout: Some(DEFAULT_TIMEOUT), - tls_config: None, + #[cfg(feature = "tcp")] tcp_config: Default::default(), + #[cfg(feature = "tls")] + tls_config: None, subscription_buffer_size: DEFAULT_MAX_SUBSCRIPTION_BUFFER_SIZE, }) } @@ -52,8 +58,10 @@ impl Client { /// Builder for constructing an RPC [`Client`]. pub struct ClientBuilder { endpoint: Endpoint, - tls_config: Option<(rustls::ClientConfig, String)>, + #[cfg(feature = "tcp")] tcp_config: TcpConfig, + #[cfg(feature = "tls")] + tls_config: Option<(rustls::ClientConfig, String)>, timeout: Option, subscription_buffer_size: usize, } @@ -118,6 +126,7 @@ impl ClientBuilder { /// ``` /// /// This function will return an error if the endpoint does not support TCP protocols. + #[cfg(feature = "tcp")] pub fn tcp_config(mut self, config: TcpConfig) -> Result { match self.endpoint { Endpoint::Tcp(..) | Endpoint::Tls(..) | Endpoint::Ws(..) | Endpoint::Wss(..) => { @@ -146,13 +155,17 @@ impl ClientBuilder { /// ``` /// /// This function will return an error if the endpoint does not support TLS protocols. + #[cfg(feature = "tls")] pub fn tls_config(mut self, config: rustls::ClientConfig, dns_name: &str) -> Result { match self.endpoint { - Endpoint::Tcp(..) | Endpoint::Tls(..) | Endpoint::Ws(..) | Endpoint::Wss(..) => { + Endpoint::Tls(..) | Endpoint::Wss(..) => { self.tls_config = Some((config, dns_name.to_string())); Ok(self) } - _ => Err(Error::UnsupportedProtocol(self.endpoint.to_string())), + _ => Err(Error::UnsupportedProtocol(format!( + "Invalid tls config for endpoint: {}", + self.endpoint + ))), } } @@ -177,7 +190,12 @@ impl ClientBuilder { /// ``` pub async fn build(self) -> Result> { let conn: Conn = match self.endpoint { - Endpoint::Tcp(..) | Endpoint::Tls(..) => match self.tls_config { + #[cfg(feature = "tcp")] + Endpoint::Tcp(..) => Box::new( + karyon_net::tcp::dial(&self.endpoint, self.tcp_config, JsonCodec {}).await?, + ), + #[cfg(feature = "tls")] + Endpoint::Tls(..) => match self.tls_config { Some((conf, dns_name)) => Box::new( karyon_net::tls::dial( &self.endpoint, @@ -190,12 +208,18 @@ impl ClientBuilder { ) .await?, ), - None => Box::new( - karyon_net::tcp::dial(&self.endpoint, self.tcp_config, JsonCodec {}).await?, - ), + None => return Err(Error::TLSConfigRequired), }, #[cfg(feature = "ws")] - Endpoint::Ws(..) | Endpoint::Wss(..) => match self.tls_config { + Endpoint::Ws(..) => { + let config = ClientWsConfig { + tcp_config: self.tcp_config, + wss_config: None, + }; + Box::new(karyon_net::ws::dial(&self.endpoint, config, WsJsonCodec {}).await?) + } + #[cfg(all(feature = "ws", feature = "tls"))] + Endpoint::Wss(..) => match self.tls_config { Some((conf, dns_name)) => Box::new( karyon_net::ws::dial( &self.endpoint, @@ -210,13 +234,7 @@ impl ClientBuilder { ) .await?, ), - None => { - let config = ClientWsConfig { - tcp_config: self.tcp_config, - wss_config: None, - }; - Box::new(karyon_net::ws::dial(&self.endpoint, config, WsJsonCodec {}).await?) - } + None => return Err(Error::TLSConfigRequired), }, #[cfg(all(feature = "unix", target_family = "unix"))] Endpoint::Unix(..) => Box::new( diff --git a/jsonrpc/src/error.rs b/jsonrpc/src/error.rs index f409c8d..7083e8a 100644 --- a/jsonrpc/src/error.rs +++ b/jsonrpc/src/error.rs @@ -20,9 +20,12 @@ pub enum Error { #[error(transparent)] ParseJSON(#[from] serde_json::Error), - #[error("Unsupported protocol: {0}")] + #[error("Unsupported Protocol: {0}")] UnsupportedProtocol(String), + #[error("Tls config is required")] + TLSConfigRequired, + #[error("Receive close message from connection: {0}")] CloseConnection(String), diff --git a/jsonrpc/src/lib.rs b/jsonrpc/src/lib.rs index c72f067..970f563 100644 --- a/jsonrpc/src/lib.rs +++ b/jsonrpc/src/lib.rs @@ -19,4 +19,7 @@ pub use server::{ pub use karyon_jsonrpc_macro::{rpc_impl, rpc_pubsub_impl}; -pub use karyon_net::{tcp::TcpConfig, Endpoint}; +pub use karyon_net::Endpoint; + +#[cfg(feature = "tcp")] +pub use karyon_net::tcp::TcpConfig; diff --git a/jsonrpc/src/server/builder.rs b/jsonrpc/src/server/builder.rs index ca6d1a7..cd79ac3 100644 --- a/jsonrpc/src/server/builder.rs +++ b/jsonrpc/src/server/builder.rs @@ -1,27 +1,30 @@ use std::{collections::HashMap, sync::Arc}; -#[cfg(feature = "smol")] -use futures_rustls::rustls; -#[cfg(feature = "tokio")] -use tokio_rustls::rustls; - use karyon_core::{async_runtime::Executor, async_util::TaskGroup}; use karyon_net::{Endpoint, Listener, ToEndpoint}; -#[cfg(feature = "ws")] -use crate::codec::WsJsonCodec; +#[cfg(feature = "tls")] +use karyon_net::async_rustls::rustls; #[cfg(feature = "ws")] use karyon_net::ws::ServerWsConfig; -use crate::{codec::JsonCodec, Error, PubSubRPCService, RPCService, Result, TcpConfig}; +#[cfg(feature = "ws")] +use crate::codec::WsJsonCodec; + +#[cfg(feature = "tcp")] +use crate::TcpConfig; + +use crate::{codec::JsonCodec, Error, PubSubRPCService, RPCService, Result}; use super::Server; /// Builder for constructing an RPC [`Server`]. pub struct ServerBuilder { endpoint: Endpoint, + #[cfg(feature = "tcp")] tcp_config: TcpConfig, + #[cfg(feature = "tls")] tls_config: Option, services: HashMap>, pubsub_services: HashMap>, @@ -135,6 +138,7 @@ impl ServerBuilder { /// ``` /// /// This function will return an error if the endpoint does not support TCP protocols. + #[cfg(feature = "tcp")] pub fn tcp_config(mut self, config: TcpConfig) -> Result { match self.endpoint { Endpoint::Tcp(..) | Endpoint::Tls(..) | Endpoint::Ws(..) | Endpoint::Wss(..) => { @@ -162,13 +166,17 @@ impl ServerBuilder { /// ``` /// /// This function will return an error if the endpoint does not support TLS protocols. + #[cfg(feature = "tls")] pub fn tls_config(mut self, config: rustls::ServerConfig) -> Result { match self.endpoint { - Endpoint::Tcp(..) | Endpoint::Tls(..) | Endpoint::Ws(..) | Endpoint::Wss(..) => { + Endpoint::Tls(..) | Endpoint::Wss(..) => { self.tls_config = Some(config); Ok(self) } - _ => Err(Error::UnsupportedProtocol(self.endpoint.to_string())), + _ => Err(Error::UnsupportedProtocol(format!( + "Invalid tls config for endpoint: {}", + self.endpoint + ))), } } @@ -184,7 +192,12 @@ impl ServerBuilder { async fn _build(self, task_group: TaskGroup) -> Result> { let listener: Listener = match self.endpoint { - Endpoint::Tcp(..) | Endpoint::Tls(..) => match &self.tls_config { + #[cfg(feature = "tcp")] + Endpoint::Tcp(..) => Box::new( + karyon_net::tcp::listen(&self.endpoint, self.tcp_config, JsonCodec {}).await?, + ), + #[cfg(feature = "tls")] + Endpoint::Tls(..) => match &self.tls_config { Some(conf) => Box::new( karyon_net::tls::listen( &self.endpoint, @@ -196,12 +209,18 @@ impl ServerBuilder { ) .await?, ), - None => Box::new( - karyon_net::tcp::listen(&self.endpoint, self.tcp_config, JsonCodec {}).await?, - ), + None => return Err(Error::TLSConfigRequired), }, #[cfg(feature = "ws")] - Endpoint::Ws(..) | Endpoint::Wss(..) => match &self.tls_config { + Endpoint::Ws(..) => { + let config = ServerWsConfig { + tcp_config: self.tcp_config, + wss_config: None, + }; + Box::new(karyon_net::ws::listen(&self.endpoint, config, WsJsonCodec {}).await?) + } + #[cfg(all(feature = "ws", feature = "tls"))] + Endpoint::Wss(..) => match &self.tls_config { Some(conf) => Box::new( karyon_net::ws::listen( &self.endpoint, @@ -215,13 +234,7 @@ impl ServerBuilder { ) .await?, ), - None => { - let config = ServerWsConfig { - tcp_config: self.tcp_config, - wss_config: None, - }; - Box::new(karyon_net::ws::listen(&self.endpoint, config, WsJsonCodec {}).await?) - } + None => return Err(Error::TLSConfigRequired), }, #[cfg(all(feature = "unix", target_family = "unix"))] Endpoint::Unix(..) => Box::new(karyon_net::unix::listen( @@ -262,7 +275,9 @@ impl Server { endpoint, services: HashMap::new(), pubsub_services: HashMap::new(), + #[cfg(feature = "tcp")] tcp_config: Default::default(), + #[cfg(feature = "tls")] tls_config: None, }) } -- cgit v1.2.3