1 #![cfg_attr(not(unix), allow(unused_imports))] 2 3 pub mod hello_world { 4 tonic::include_proto!("helloworld"); 5 } 6 7 use hello_world::{greeter_client::GreeterClient, HelloRequest}; 8 use hyper_util::rt::TokioIo; 9 #[cfg(unix)] 10 use tokio::net::UnixStream; 11 use tonic::transport::{Endpoint, Uri}; 12 use tower::service_fn; 13 14 #[cfg(unix)] 15 #[tokio::main] main() -> Result<(), Box<dyn std::error::Error>>16async fn main() -> Result<(), Box<dyn std::error::Error>> { 17 // We will ignore this uri because uds do not use it 18 // if your connector does use the uri it will be provided 19 // as the request to the `MakeConnection`. 20 21 let channel = Endpoint::try_from("http://[::]:50051")? 22 .connect_with_connector(service_fn(|_: Uri| async { 23 let path = "/tmp/tonic/helloworld"; 24 25 // Connect to a Uds socket 26 Ok::<_, std::io::Error>(TokioIo::new(UnixStream::connect(path).await?)) 27 })) 28 .await?; 29 30 let mut client = GreeterClient::new(channel); 31 32 let request = tonic::Request::new(HelloRequest { 33 name: "Tonic".into(), 34 }); 35 36 let response = client.say_hello(request).await?; 37 38 println!("RESPONSE={:?}", response); 39 40 Ok(()) 41 } 42 43 #[cfg(not(unix))] main()44fn main() { 45 panic!("The `uds` example only works on unix systems!"); 46 } 47