diff --git a/Cargo.toml b/Cargo.toml index ee580ff..98dd027 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,8 @@ serde = { version = "1.0.171", features = ["derive"] , optional = true} serde_with = { version = "3.0.0", optional = true} socket2 = {version="0.5.3", optional=true, features=["all"]} tokio = { version = "1.29.1", features = ["net", "io-std", "time", "sync"] } -tonic = { package = "tonic", version = "0.11.0", optional = true } +tonic_012 = { package = "tonic", version = "0.12.0", optional = true } +tonic_011 = { package = "tonic", version = "0.11.0", optional = true } tonic_010 = { package = "tonic", version = "0.10.2", optional = true } tracing = "0.1.37" axum07 = { version="0.7", package="axum", optional=true } @@ -73,7 +74,10 @@ socket_options = ["socket2"] tonic010 = ["dep:tonic_010"] ## Enable `tonic(v0.11)::transport::server::Connected` implementation for [`Connection`] -tonic011 = ["dep:tonic"] +tonic011 = ["dep:tonic_011"] + +## Enable `tonic(v0.12)::transport::server::Connected` implementation for [`Connection`] +tonic012 = ["dep:tonic_012"] ## Enable [`tokio_util::net::Listener`] implementation for [`Listener`]. tokio-util = ["dep:tokio-util"] @@ -90,7 +94,7 @@ hyper014 = { version = "0.14.27", features = ["server", "http1"], package="hyper tokio = { version = "1.29.1", features = ["macros", "rt", "io-util"] } tokio-test = "0.4.2" toml = "0.7.6" -tonic-health = "0.11.0" +tonic-health = "0.12.0" tracing-subscriber = "0.3.17" [[example]] diff --git a/examples/tonic.rs b/examples/tonic.rs index a59041e..fdd8f40 100644 --- a/examples/tonic.rs +++ b/examples/tonic.rs @@ -1,5 +1,8 @@ /// A simple example of how to use tokio-listener with a tonic gRPC server. -use tonic::transport::Server; +/// Keep in mind tokio-listener supports different tonic versions, and +/// version-suffixes them. +/// In your code, the crate would probably just be called "tonic". +use tonic_012::transport::Server; use tonic_health::server::health_reporter; #[tokio::main(flavor = "current_thread")] diff --git a/src/lib.rs b/src/lib.rs index daf5f67..9e70992 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,6 +118,10 @@ mod tonic010; #[cfg_attr(docsrs_alt, doc(cfg(feature = "tonic011")))] mod tonic011; +#[cfg(feature = "tonic012")] +#[cfg_attr(docsrs_alt, doc(cfg(feature = "tonic012")))] +mod tonic012; + #[cfg(feature = "tokio-util")] #[cfg_attr(docsrs_alt, doc(cfg(feature = "tokio-util")))] mod tokioutil; diff --git a/src/tonic011.rs b/src/tonic011.rs index b5f88c6..3d25d32 100644 --- a/src/tonic011.rs +++ b/src/tonic011.rs @@ -1,6 +1,6 @@ #[cfg(all(feature = "unix", unix))] -use tonic::transport::server::UdsConnectInfo; -use tonic::transport::server::{Connected, TcpConnectInfo}; +use tonic_011::transport::server::UdsConnectInfo; +use tonic_011::transport::server::{Connected, TcpConnectInfo}; use crate::Connection; diff --git a/src/tonic012.rs b/src/tonic012.rs new file mode 100644 index 0000000..4264826 --- /dev/null +++ b/src/tonic012.rs @@ -0,0 +1,37 @@ +#[cfg(all(feature = "unix", unix))] +use tonic_012::transport::server::UdsConnectInfo; +use tonic_012::transport::server::{Connected, TcpConnectInfo}; + +use crate::Connection; + +#[derive(Clone)] +pub enum ListenerConnectInfo { + Tcp(TcpConnectInfo), + #[cfg(all(feature = "unix", unix))] + #[cfg_attr(docsrs_alt, doc(cfg(all(feature = "unix", unix))))] + Unix(UdsConnectInfo), + #[cfg(feature = "inetd")] + #[cfg_attr(docsrs_alt, doc(cfg(feature = "inetd")))] + Stdio, + Other, +} + +impl Connected for Connection { + type ConnectInfo = ListenerConnectInfo; + + fn connect_info(&self) -> Self::ConnectInfo { + if let Some(tcp_stream) = self.try_borrow_tcp() { + return ListenerConnectInfo::Tcp(tcp_stream.connect_info()); + } + #[cfg(all(feature = "unix", unix))] + if let Some(unix_stream) = self.try_borrow_unix() { + return ListenerConnectInfo::Unix(unix_stream.connect_info()); + } + #[cfg(feature = "inetd")] + if self.try_borrow_stdio().is_some() { + return ListenerConnectInfo::Stdio; + } + + ListenerConnectInfo::Other + } +}