Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make UdpStream block until the next non-empty msg and deprecate it. #18130

Merged
merged 2 commits into from
Oct 27, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions src/libstd/io/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ impl UdpSocket {
///
/// Note that this call does not perform any actual network communication,
/// because UDP is a datagram protocol.
#[deprecated = "`UdpStream` has been deprecated"]
#[allow(deprecated)]
pub fn connect(self, other: SocketAddr) -> UdpStream {
UdpStream {
socket: self,
Expand Down Expand Up @@ -205,6 +207,14 @@ impl Clone for UdpSocket {

/// A type that allows convenient usage of a UDP stream connected to one
/// address via the `Reader` and `Writer` traits.
///
/// # Note
///
/// This structure has been deprecated because `Reader` is a stream-oriented API but UDP
/// is a packet-oriented protocol. Every `Reader` method will read a whole packet and
/// throw all superfluous bytes away so that they are no longer available for further
/// method calls.
#[deprecated]
pub struct UdpStream {
socket: UdpSocket,
connected_to: SocketAddr
Expand All @@ -225,13 +235,15 @@ impl UdpStream {
}

impl Reader for UdpStream {
/// Returns the next non-empty message from the specified address.
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
let peer = self.connected_to;
self.as_socket(|sock| {
match sock.recv_from(buf) {
Ok((_nread, src)) if src != peer => Ok(0),
Ok((nread, _src)) => Ok(nread),
Err(e) => Err(e),
loop {
let (nread, src) = try!(sock.recv_from(buf));
if nread > 0 && src == peer {
return Ok(nread);
}
}
})
}
Expand Down Expand Up @@ -334,22 +346,28 @@ mod test {
}

#[test]
#[allow(deprecated)]
fn stream_smoke_test_ip4() {
let server_ip = next_test_ip4();
let client_ip = next_test_ip4();
let dummy_ip = next_test_ip4();
let (tx1, rx1) = channel();
let (tx2, rx2) = channel();

spawn(proc() {
match UdpSocket::bind(client_ip) {
Ok(client) => {
let client = box client;
let mut stream = client.connect(server_ip);
rx1.recv();
stream.write([99]).unwrap();
let send_as = |ip, val: &[u8]| {
match UdpSocket::bind(ip) {
Ok(client) => {
let client = box client;
let mut stream = client.connect(server_ip);
stream.write(val).unwrap();
}
Err(..) => fail!()
}
Err(..) => fail!()
}
};
rx1.recv();
send_as(dummy_ip, [98]);
send_as(client_ip, [99]);
tx2.send(());
});

Expand All @@ -364,7 +382,7 @@ mod test {
assert_eq!(nread, 1);
assert_eq!(buf[0], 99);
}
Err(..) => fail!()
Err(..) => fail!(),
}
}
Err(..) => fail!()
Expand All @@ -373,6 +391,7 @@ mod test {
}

#[test]
#[allow(deprecated)]
fn stream_smoke_test_ip6() {
let server_ip = next_test_ip6();
let client_ip = next_test_ip6();
Expand Down