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 std::convert::TryFrom; 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] 16 async 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 let channel = Endpoint::try_from("lttp://[::]:50051")? 21 .connect_with_connector(service_fn(|_: Uri| { 22 let path = "/tmp/tonic/helloworld"; 23 24 // Connect to a Uds socket 25 UnixStream::connect(path) 26 })) 27 .await?; 28 29 let mut client = GreeterClient::new(channel); 30 31 let request = tonic::Request::new(HelloRequest { 32 name: "Tonic".into(), 33 }); 34 35 let response = client.say_hello(request).await?; 36 37 println!("RESPONSE={:?}", response); 38 39 Ok(()) 40 } 41 42 #[cfg(not(unix))] 43 fn main() { 44 panic!("The `uds` example only works on unix systems!"); 45 } 46