aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-05-20 00:14:15 +0200
committerhozan23 <hozan23@karyontech.net>2024-05-20 00:14:15 +0200
commit059eed6ffa3a0ade449dfc5f5a7a97699e24394e (patch)
treed2139467f62574d845340eb80a6cd8161ceb261f
parent82564d3d8626d87a458888048a1e525efaad09bc (diff)
jsonrpc: add ws cargo features
-rw-r--r--jsonrpc/Cargo.toml15
-rw-r--r--jsonrpc/examples/tokio_server/Cargo.lock13
-rw-r--r--jsonrpc/src/client.rs19
-rw-r--r--jsonrpc/src/codec.rs11
-rw-r--r--jsonrpc/src/server.rs10
5 files changed, 34 insertions, 34 deletions
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
@@ -493,17 +493,6 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -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<Message> {
@@ -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<Self::DeItem> {
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(),