11use std:: io;
2- use std:: net:: SocketAddr ;
3- use std:: net :: { Ipv4Addr , Ipv6Addr } ;
2+ use std:: net:: { Ipv4Addr , Ipv6Addr , SocketAddr } ;
3+ use std:: task :: { Context , Poll } ;
44
55use crate :: future;
66use crate :: net:: driver:: Watcher ;
@@ -69,9 +69,7 @@ impl UdpSocket {
6969 /// ```
7070 pub async fn bind < A : ToSocketAddrs > ( addrs : A ) -> io:: Result < UdpSocket > {
7171 let mut last_err = None ;
72- let addrs = addrs
73- . to_socket_addrs ( )
74- . await ?;
72+ let addrs = addrs. to_socket_addrs ( ) . await ?;
7573
7674 for addr in addrs {
7775 match mio:: net:: UdpSocket :: bind ( & addr) {
@@ -116,6 +114,19 @@ impl UdpSocket {
116114 . context ( || String :: from ( "could not get local address" ) )
117115 }
118116
117+ /// Sends data on the socket to the given address.
118+ ///
119+ /// If this function returns `Poll::Ready(Ok(_))`, returns the number of bytes written.
120+ pub fn poll_send_to (
121+ & self ,
122+ cx : & mut Context < ' _ > ,
123+ buf : & [ u8 ] ,
124+ addr : & SocketAddr ,
125+ ) -> Poll < io:: Result < usize > > {
126+ self . watcher
127+ . poll_write_with ( cx, |inner| inner. send_to ( buf, & addr) )
128+ }
129+
119130 /// Sends data on the socket to the given address.
120131 ///
121132 /// On success, returns the number of bytes written.
@@ -153,12 +164,21 @@ impl UdpSocket {
153164 }
154165 } ;
155166
156- future:: poll_fn ( |cx| {
157- self . watcher
158- . poll_write_with ( cx, |inner| inner. send_to ( buf, & addr) )
159- } )
160- . await
161- . context ( || format ! ( "could not send packet to {}" , addr) )
167+ future:: poll_fn ( |cx| self . poll_send_to ( cx, buf, & addr) )
168+ . await
169+ . context ( || format ! ( "could not send packet to {}" , addr) )
170+ }
171+
172+ /// Receives data from the socket.
173+ ///
174+ /// On success, returns the number of bytes read and the origin.
175+ pub fn poll_recv_from (
176+ & self ,
177+ cx : & mut Context < ' _ > ,
178+ buf : & mut [ u8 ] ,
179+ ) -> Poll < io:: Result < ( usize , SocketAddr ) > > {
180+ self . watcher
181+ . poll_read_with ( cx, |inner| inner. recv_from ( buf) )
162182 }
163183
164184 /// Receives data from the socket.
@@ -181,22 +201,19 @@ impl UdpSocket {
181201 /// # Ok(()) }) }
182202 /// ```
183203 pub async fn recv_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
184- future:: poll_fn ( |cx| {
185- self . watcher
186- . poll_read_with ( cx, |inner| inner. recv_from ( buf) )
187- } )
188- . await
189- . context ( || {
190- use std:: fmt:: Write ;
191-
192- let mut error = String :: from ( "could not receive data on " ) ;
193- if let Ok ( addr) = self . local_addr ( ) {
194- let _ = write ! ( & mut error, "{}" , addr) ;
195- } else {
196- error. push_str ( "socket" ) ;
197- }
198- error
199- } )
204+ future:: poll_fn ( |cx| self . poll_recv_from ( cx, buf) )
205+ . await
206+ . context ( || {
207+ use std:: fmt:: Write ;
208+
209+ let mut error = String :: from ( "could not receive data on " ) ;
210+ if let Ok ( addr) = self . local_addr ( ) {
211+ let _ = write ! ( & mut error, "{}" , addr) ;
212+ } else {
213+ error. push_str ( "socket" ) ;
214+ }
215+ error
216+ } )
200217 }
201218
202219 /// Connects the UDP socket to a remote address.
0 commit comments