Skip to content

Commit a93e9c2

Browse files
committed
auto merge of #18130 : mahkoh/rust/udp, r=alexcrichton
Closes #18111 Note that the non-empty part doesn't matter right now because of #18129.
2 parents f037452 + d6dc01e commit a93e9c2

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/libstd/io/net/udp.rs

+32-13
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ impl UdpSocket {
100100
///
101101
/// Note that this call does not perform any actual network communication,
102102
/// because UDP is a datagram protocol.
103+
#[deprecated = "`UdpStream` has been deprecated"]
104+
#[allow(deprecated)]
103105
pub fn connect(self, other: SocketAddr) -> UdpStream {
104106
UdpStream {
105107
socket: self,
@@ -205,6 +207,14 @@ impl Clone for UdpSocket {
205207

206208
/// A type that allows convenient usage of a UDP stream connected to one
207209
/// 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]
208218
pub struct UdpStream {
209219
socket: UdpSocket,
210220
connected_to: SocketAddr
@@ -225,13 +235,15 @@ impl UdpStream {
225235
}
226236

227237
impl Reader for UdpStream {
238+
/// Returns the next non-empty message from the specified address.
228239
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
229240
let peer = self.connected_to;
230241
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+
}
235247
}
236248
})
237249
}
@@ -334,22 +346,28 @@ mod test {
334346
}
335347

336348
#[test]
349+
#[allow(deprecated)]
337350
fn stream_smoke_test_ip4() {
338351
let server_ip = next_test_ip4();
339352
let client_ip = next_test_ip4();
353+
let dummy_ip = next_test_ip4();
340354
let (tx1, rx1) = channel();
341355
let (tx2, rx2) = channel();
342356

343357
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!()
350366
}
351-
Err(..) => fail!()
352-
}
367+
};
368+
rx1.recv();
369+
send_as(dummy_ip, [98]);
370+
send_as(client_ip, [99]);
353371
tx2.send(());
354372
});
355373

@@ -364,7 +382,7 @@ mod test {
364382
assert_eq!(nread, 1);
365383
assert_eq!(buf[0], 99);
366384
}
367-
Err(..) => fail!()
385+
Err(..) => fail!(),
368386
}
369387
}
370388
Err(..) => fail!()
@@ -373,6 +391,7 @@ mod test {
373391
}
374392

375393
#[test]
394+
#[allow(deprecated)]
376395
fn stream_smoke_test_ip6() {
377396
let server_ip = next_test_ip6();
378397
let client_ip = next_test_ip6();

0 commit comments

Comments
 (0)