1
1
use 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 } ;
4
4
5
5
use crate :: future;
6
6
use crate :: net:: driver:: Watcher ;
@@ -69,9 +69,7 @@ impl UdpSocket {
69
69
/// ```
70
70
pub async fn bind < A : ToSocketAddrs > ( addrs : A ) -> io:: Result < UdpSocket > {
71
71
let mut last_err = None ;
72
- let addrs = addrs
73
- . to_socket_addrs ( )
74
- . await ?;
72
+ let addrs = addrs. to_socket_addrs ( ) . await ?;
75
73
76
74
for addr in addrs {
77
75
match mio:: net:: UdpSocket :: bind ( & addr) {
@@ -116,6 +114,19 @@ impl UdpSocket {
116
114
. context ( || String :: from ( "could not get local address" ) )
117
115
}
118
116
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
+
119
130
/// Sends data on the socket to the given address.
120
131
///
121
132
/// On success, returns the number of bytes written.
@@ -153,12 +164,21 @@ impl UdpSocket {
153
164
}
154
165
} ;
155
166
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) )
162
182
}
163
183
164
184
/// Receives data from the socket.
@@ -181,22 +201,19 @@ impl UdpSocket {
181
201
/// # Ok(()) }) }
182
202
/// ```
183
203
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
+ } )
200
217
}
201
218
202
219
/// Connects the UDP socket to a remote address.
0 commit comments