@@ -100,6 +100,8 @@ impl UdpSocket {
100
100
///
101
101
/// Note that this call does not perform any actual network communication,
102
102
/// because UDP is a datagram protocol.
103
+ #[ deprecated = "`UdpStream` has been deprecated" ]
104
+ #[ allow( deprecated) ]
103
105
pub fn connect ( self , other : SocketAddr ) -> UdpStream {
104
106
UdpStream {
105
107
socket : self ,
@@ -205,6 +207,14 @@ impl Clone for UdpSocket {
205
207
206
208
/// A type that allows convenient usage of a UDP stream connected to one
207
209
/// address via the `Reader` and `Writer` traits.
210
+ ///
211
+ /// # Note
212
+ ///
213
+ /// This structure has been deprecated because `Reader` is a stream-oriented API but UDP
214
+ /// is a packet-oriented protocol. Every `Reader` method will read a whole packet and
215
+ /// throw all superfluous bytes away so that they are no longer available for further
216
+ /// method calls.
217
+ #[ deprecated]
208
218
pub struct UdpStream {
209
219
socket : UdpSocket ,
210
220
connected_to : SocketAddr
@@ -225,13 +235,15 @@ impl UdpStream {
225
235
}
226
236
227
237
impl Reader for UdpStream {
238
+ /// Returns the next non-empty message from the specified address.
228
239
fn read ( & mut self , buf : & mut [ u8 ] ) -> IoResult < uint > {
229
240
let peer = self . connected_to ;
230
241
self . as_socket ( |sock| {
231
- match sock. recv_from ( buf) {
232
- Ok ( ( _nread, src) ) if src != peer => Ok ( 0 ) ,
233
- Ok ( ( nread, _src) ) => Ok ( nread) ,
234
- Err ( e) => Err ( e) ,
242
+ loop {
243
+ let ( nread, src) = try!( sock. recv_from ( buf) ) ;
244
+ if nread > 0 && src == peer {
245
+ return Ok ( nread) ;
246
+ }
235
247
}
236
248
} )
237
249
}
@@ -334,22 +346,28 @@ mod test {
334
346
}
335
347
336
348
#[ test]
349
+ #[ allow( deprecated) ]
337
350
fn stream_smoke_test_ip4 ( ) {
338
351
let server_ip = next_test_ip4 ( ) ;
339
352
let client_ip = next_test_ip4 ( ) ;
353
+ let dummy_ip = next_test_ip4 ( ) ;
340
354
let ( tx1, rx1) = channel ( ) ;
341
355
let ( tx2, rx2) = channel ( ) ;
342
356
343
357
spawn ( proc ( ) {
344
- match UdpSocket :: bind ( client_ip) {
345
- Ok ( client) => {
346
- let client = box client;
347
- let mut stream = client. connect ( server_ip) ;
348
- rx1. recv ( ) ;
349
- stream. write ( [ 99 ] ) . unwrap ( ) ;
358
+ let send_as = |ip, val : & [ u8 ] | {
359
+ match UdpSocket :: bind ( ip) {
360
+ Ok ( client) => {
361
+ let client = box client;
362
+ let mut stream = client. connect ( server_ip) ;
363
+ stream. write ( val) . unwrap ( ) ;
364
+ }
365
+ Err ( ..) => fail ! ( )
350
366
}
351
- Err ( ..) => fail ! ( )
352
- }
367
+ } ;
368
+ rx1. recv ( ) ;
369
+ send_as ( dummy_ip, [ 98 ] ) ;
370
+ send_as ( client_ip, [ 99 ] ) ;
353
371
tx2. send ( ( ) ) ;
354
372
} ) ;
355
373
@@ -364,7 +382,7 @@ mod test {
364
382
assert_eq ! ( nread, 1 ) ;
365
383
assert_eq ! ( buf[ 0 ] , 99 ) ;
366
384
}
367
- Err ( ..) => fail ! ( )
385
+ Err ( ..) => fail ! ( ) ,
368
386
}
369
387
}
370
388
Err ( ..) => fail ! ( )
@@ -373,6 +391,7 @@ mod test {
373
391
}
374
392
375
393
#[ test]
394
+ #[ allow( deprecated) ]
376
395
fn stream_smoke_test_ip6 ( ) {
377
396
let server_ip = next_test_ip6 ( ) ;
378
397
let client_ip = next_test_ip6 ( ) ;
0 commit comments