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