11#![ cfg_attr( not( unix) , allow( unused_imports) ) ]
22
3- use futures:: TryFutureExt ;
43use std:: path:: Path ;
54#[ cfg( unix) ]
65use tokio:: net:: UnixListener ;
6+ #[ cfg( unix) ]
7+ use tokio_stream:: wrappers:: UnixListenerStream ;
8+ #[ cfg( unix) ]
9+ use tonic:: transport:: server:: UdsConnectInfo ;
710use tonic:: { transport:: Server , Request , Response , Status } ;
811
912pub mod hello_world {
@@ -26,7 +29,7 @@ impl Greeter for MyGreeter {
2629 ) -> Result < Response < HelloReply > , Status > {
2730 #[ cfg( unix) ]
2831 {
29- let conn_info = request. extensions ( ) . get :: < unix :: UdsConnectInfo > ( ) . unwrap ( ) ;
32+ let conn_info = request. extensions ( ) . get :: < UdsConnectInfo > ( ) . unwrap ( ) ;
3033 println ! ( "Got a request {:?} with info {:?}" , request, conn_info) ;
3134 }
3235
@@ -46,89 +49,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4649
4750 let greeter = MyGreeter :: default ( ) ;
4851
49- let incoming = {
50- let uds = UnixListener :: bind ( path) ?;
51-
52- async_stream:: stream! {
53- loop {
54- let item = uds. accept( ) . map_ok( |( st, _) | unix:: UnixStream ( st) ) . await ;
55-
56- yield item;
57- }
58- }
59- } ;
52+ let uds = UnixListener :: bind ( path) ?;
53+ let uds_stream = UnixListenerStream :: new ( uds) ;
6054
6155 Server :: builder ( )
6256 . add_service ( GreeterServer :: new ( greeter) )
63- . serve_with_incoming ( incoming )
57+ . serve_with_incoming ( uds_stream )
6458 . await ?;
6559
6660 Ok ( ( ) )
6761}
6862
69- #[ cfg( unix) ]
70- mod unix {
71- use std:: {
72- pin:: Pin ,
73- sync:: Arc ,
74- task:: { Context , Poll } ,
75- } ;
76-
77- use tokio:: io:: { AsyncRead , AsyncWrite , ReadBuf } ;
78- use tonic:: transport:: server:: Connected ;
79-
80- #[ derive( Debug ) ]
81- pub struct UnixStream ( pub tokio:: net:: UnixStream ) ;
82-
83- impl Connected for UnixStream {
84- type ConnectInfo = UdsConnectInfo ;
85-
86- fn connect_info ( & self ) -> Self :: ConnectInfo {
87- UdsConnectInfo {
88- peer_addr : self . 0 . peer_addr ( ) . ok ( ) . map ( Arc :: new) ,
89- peer_cred : self . 0 . peer_cred ( ) . ok ( ) ,
90- }
91- }
92- }
93-
94- #[ derive( Clone , Debug ) ]
95- pub struct UdsConnectInfo {
96- pub peer_addr : Option < Arc < tokio:: net:: unix:: SocketAddr > > ,
97- pub peer_cred : Option < tokio:: net:: unix:: UCred > ,
98- }
99-
100- impl AsyncRead for UnixStream {
101- fn poll_read (
102- mut self : Pin < & mut Self > ,
103- cx : & mut Context < ' _ > ,
104- buf : & mut ReadBuf < ' _ > ,
105- ) -> Poll < std:: io:: Result < ( ) > > {
106- Pin :: new ( & mut self . 0 ) . poll_read ( cx, buf)
107- }
108- }
109-
110- impl AsyncWrite for UnixStream {
111- fn poll_write (
112- mut self : Pin < & mut Self > ,
113- cx : & mut Context < ' _ > ,
114- buf : & [ u8 ] ,
115- ) -> Poll < std:: io:: Result < usize > > {
116- Pin :: new ( & mut self . 0 ) . poll_write ( cx, buf)
117- }
118-
119- fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < std:: io:: Result < ( ) > > {
120- Pin :: new ( & mut self . 0 ) . poll_flush ( cx)
121- }
122-
123- fn poll_shutdown (
124- mut self : Pin < & mut Self > ,
125- cx : & mut Context < ' _ > ,
126- ) -> Poll < std:: io:: Result < ( ) > > {
127- Pin :: new ( & mut self . 0 ) . poll_shutdown ( cx)
128- }
129- }
130- }
131-
13263#[ cfg( not( unix) ) ]
13364fn main ( ) {
13465 panic ! ( "The `uds` example only works on unix systems!" ) ;
0 commit comments