feat(transport): Add remote_addr to Request on the server si… (#186)

This commit is contained in:
Lucio Franco
2019-12-13 20:25:00 -05:00
committed by GitHub
parent 0505dff65a
commit 3eb76abf9f
9 changed files with 193 additions and 26 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ impl Greeter for MyGreeter {
&self,
request: Request<HelloRequest>,
) -> Result<Response<HelloReply>, Status> {
println!("Got a request: {:?}", request);
println!("Got a request from {:?}", request.remote_addr());
let reply = hello_world::HelloReply {
message: format!("Hello {}!", request.into_inner().name).into(),
+48 -4
View File
@@ -1,6 +1,17 @@
use std::path::Path;
use tokio::net::UnixListener;
use tonic::{transport::Server, Request, Response, Status};
use futures::stream::TryStreamExt;
use std::{
path::Path,
pin::Pin,
task::{Context, Poll},
};
use tokio::{
io::{AsyncRead, AsyncWrite},
net::UnixListener,
};
use tonic::{
transport::{server::Connected, Server},
Request, Response, Status,
};
pub mod hello_world {
tonic::include_proto!("helloworld");
@@ -41,8 +52,41 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Server::builder()
.add_service(GreeterServer::new(greeter))
.serve_with_incoming(uds.incoming())
.serve_with_incoming(uds.incoming().map_ok(UnixStream))
.await?;
Ok(())
}
#[derive(Debug)]
struct UnixStream(tokio::net::UnixStream);
impl Connected for UnixStream {}
impl AsyncRead for UnixStream {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
}
impl AsyncWrite for UnixStream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.0).poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_shutdown(cx)
}
}