File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -663,6 +663,13 @@ impl Connection {
663663 Poll :: Pending
664664 }
665665
666+ /// Try to receive an application datagram. Returns None if no datagram is
667+ /// available.
668+ pub fn try_recv_datagram ( & self ) -> Result < Option < Bytes > , ConnectionError > {
669+ let mut state = self . 0 . try_state ( ) ?;
670+ Ok ( state. conn . datagrams ( ) . recv ( ) )
671+ }
672+
666673 /// Receive an application datagram.
667674 pub async fn recv_datagram ( & self ) -> Result < Bytes , ConnectionError > {
668675 future:: poll_fn ( |cx| self . poll_recv_datagram ( cx) ) . await
Original file line number Diff line number Diff line change @@ -264,3 +264,30 @@ async fn two_datagram_readers() {
264264 assert ! ( a == MSG1 || b == MSG1 ) ;
265265 assert ! ( a == MSG2 || b == MSG2 ) ;
266266}
267+
268+ #[ compio_macros:: test]
269+ async fn try_recv_datagram ( ) {
270+ let _guard = subscribe ( ) ;
271+
272+ let endpoint = endpoint ( ) . await ;
273+
274+ const MSG1 : & [ u8 ] = b"one" ;
275+ const MSG2 : & [ u8 ] = b"two" ;
276+
277+ let ( conn1, conn2) = join ! (
278+ async {
279+ endpoint
280+ . connect( endpoint. local_addr( ) . unwrap( ) , "localhost" , None )
281+ . unwrap( )
282+ . await
283+ . unwrap( )
284+ } ,
285+ async { endpoint. wait_incoming( ) . await . unwrap( ) . await . unwrap( ) } ,
286+ ) ;
287+
288+ conn1. send_datagram_wait ( MSG1 . into ( ) ) . await . unwrap ( ) ;
289+ conn1. send_datagram_wait ( MSG2 . into ( ) ) . await . unwrap ( ) ;
290+
291+ assert_eq ! ( conn2. recv_datagram( ) . await . unwrap( ) , MSG1 ) ;
292+ assert_eq ! ( conn2. try_recv_datagram( ) . unwrap( ) . unwrap( ) , MSG2 ) ;
293+ }
You can’t perform that action at this time.
0 commit comments