Skip to content

Commit

Permalink
use cargo features to enable/disable protocols for net crate
Browse files Browse the repository at this point in the history
  • Loading branch information
hozan23 committed May 19, 2024
1 parent a6016c7 commit f6f4478
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 85 deletions.
29 changes: 0 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions jsonrpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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 }
Expand Down
16 changes: 0 additions & 16 deletions jsonrpc/examples/tokio_server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 24 additions & 10 deletions net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
17 changes: 0 additions & 17 deletions net/examples/tcp_codec_tokio/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion net/examples/tcp_codec_tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

3 changes: 3 additions & 0 deletions net/src/codec/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 2 additions & 0 deletions net/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand All @@ -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),

Expand Down
17 changes: 16 additions & 1 deletion net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@ mod connection;
mod endpoint;
mod error;
mod listener;
#[cfg(feature = "stream")]
mod stream;
mod transports;

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;

Expand Down
2 changes: 2 additions & 0 deletions net/src/stream/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod buffer;
#[cfg(feature = "ws")]
mod websocket;

#[cfg(feature = "ws")]
pub use websocket::WsStream;

use std::{
Expand Down
11 changes: 9 additions & 2 deletions net/src/stream/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,6 +37,7 @@ where
}
}

#[cfg(feature = "tls")]
pub fn new_wss(conn: WebSocketStream<TlsStream<TcpStream>>, codec: C) -> Self {
Self {
inner: InnerWSConn::Tls(conn),
Expand All @@ -59,6 +60,7 @@ where

enum InnerWSConn {
Plain(WebSocketStream<TcpStream>),
#[cfg(feature = "tls")]
Tls(WebSocketStream<TlsStream<TcpStream>>),
}

Expand All @@ -68,27 +70,31 @@ 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),
}
}

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),
}
}

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),
}
}

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)
Expand All @@ -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),
}
}
Expand Down
5 changes: 5 additions & 0 deletions net/src/transports/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit f6f4478

Please sign in to comment.