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.
This commit is contained in:
commit
a93e9c20f2
1 changed files with 32 additions and 13 deletions
|
@ -100,6 +100,8 @@ impl UdpSocket {
|
||||||
///
|
///
|
||||||
/// Note that this call does not perform any actual network communication,
|
/// Note that this call does not perform any actual network communication,
|
||||||
/// because UDP is a datagram protocol.
|
/// because UDP is a datagram protocol.
|
||||||
|
#[deprecated = "`UdpStream` has been deprecated"]
|
||||||
|
#[allow(deprecated)]
|
||||||
pub fn connect(self, other: SocketAddr) -> UdpStream {
|
pub fn connect(self, other: SocketAddr) -> UdpStream {
|
||||||
UdpStream {
|
UdpStream {
|
||||||
socket: self,
|
socket: self,
|
||||||
|
@ -205,6 +207,14 @@ impl Clone for UdpSocket {
|
||||||
|
|
||||||
/// A type that allows convenient usage of a UDP stream connected to one
|
/// A type that allows convenient usage of a UDP stream connected to one
|
||||||
/// address via the `Reader` and `Writer` traits.
|
/// 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 {
|
pub struct UdpStream {
|
||||||
socket: UdpSocket,
|
socket: UdpSocket,
|
||||||
connected_to: SocketAddr
|
connected_to: SocketAddr
|
||||||
|
@ -225,13 +235,15 @@ impl UdpStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Reader for UdpStream {
|
impl Reader for UdpStream {
|
||||||
|
/// Returns the next non-empty message from the specified address.
|
||||||
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
||||||
let peer = self.connected_to;
|
let peer = self.connected_to;
|
||||||
self.as_socket(|sock| {
|
self.as_socket(|sock| {
|
||||||
match sock.recv_from(buf) {
|
loop {
|
||||||
Ok((_nread, src)) if src != peer => Ok(0),
|
let (nread, src) = try!(sock.recv_from(buf));
|
||||||
Ok((nread, _src)) => Ok(nread),
|
if nread > 0 && src == peer {
|
||||||
Err(e) => Err(e),
|
return Ok(nread);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -334,22 +346,28 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[allow(deprecated)]
|
||||||
fn stream_smoke_test_ip4() {
|
fn stream_smoke_test_ip4() {
|
||||||
let server_ip = next_test_ip4();
|
let server_ip = next_test_ip4();
|
||||||
let client_ip = next_test_ip4();
|
let client_ip = next_test_ip4();
|
||||||
|
let dummy_ip = next_test_ip4();
|
||||||
let (tx1, rx1) = channel();
|
let (tx1, rx1) = channel();
|
||||||
let (tx2, rx2) = channel();
|
let (tx2, rx2) = channel();
|
||||||
|
|
||||||
spawn(proc() {
|
spawn(proc() {
|
||||||
match UdpSocket::bind(client_ip) {
|
let send_as = |ip, val: &[u8]| {
|
||||||
Ok(client) => {
|
match UdpSocket::bind(ip) {
|
||||||
let client = box client;
|
Ok(client) => {
|
||||||
let mut stream = client.connect(server_ip);
|
let client = box client;
|
||||||
rx1.recv();
|
let mut stream = client.connect(server_ip);
|
||||||
stream.write([99]).unwrap();
|
stream.write(val).unwrap();
|
||||||
|
}
|
||||||
|
Err(..) => fail!()
|
||||||
}
|
}
|
||||||
Err(..) => fail!()
|
};
|
||||||
}
|
rx1.recv();
|
||||||
|
send_as(dummy_ip, [98]);
|
||||||
|
send_as(client_ip, [99]);
|
||||||
tx2.send(());
|
tx2.send(());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -364,7 +382,7 @@ mod test {
|
||||||
assert_eq!(nread, 1);
|
assert_eq!(nread, 1);
|
||||||
assert_eq!(buf[0], 99);
|
assert_eq!(buf[0], 99);
|
||||||
}
|
}
|
||||||
Err(..) => fail!()
|
Err(..) => fail!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(..) => fail!()
|
Err(..) => fail!()
|
||||||
|
@ -373,6 +391,7 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[allow(deprecated)]
|
||||||
fn stream_smoke_test_ip6() {
|
fn stream_smoke_test_ip6() {
|
||||||
let server_ip = next_test_ip6();
|
let server_ip = next_test_ip6();
|
||||||
let client_ip = next_test_ip6();
|
let client_ip = next_test_ip6();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue