2013-04-17 17:55:21 -07:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-03-16 15:59:04 -07:00
|
|
|
//! UDP (User Datagram Protocol) network connections.
|
|
|
|
//!
|
|
|
|
//! This module contains the ability to open a UDP stream to a socket address.
|
|
|
|
//! The destination and binding addresses can either be an IPv4 or IPv6
|
|
|
|
//! address. There is no corresponding notion of a server because UDP is a
|
|
|
|
//! datagram protocol.
|
2014-02-24 00:31:08 -08:00
|
|
|
|
2014-01-22 19:32:16 -08:00
|
|
|
use clone::Clone;
|
2014-04-25 11:13:02 -07:00
|
|
|
use io::net::ip::{SocketAddr, IpAddr};
|
2014-06-03 20:09:39 -07:00
|
|
|
use io::{Reader, Writer, IoResult, IoError};
|
2014-03-08 18:21:49 -08:00
|
|
|
use kinds::Send;
|
2014-07-10 14:19:17 -07:00
|
|
|
use boxed::Box;
|
2014-04-25 20:47:49 -07:00
|
|
|
use option::Option;
|
2014-03-08 18:21:49 -08:00
|
|
|
use result::{Ok, Err};
|
2013-12-05 17:25:48 -08:00
|
|
|
use rt::rtio::{RtioSocket, RtioUdpSocket, IoFactory, LocalIo};
|
2014-06-03 20:09:39 -07:00
|
|
|
use rt::rtio;
|
2013-06-19 17:23:55 -07:00
|
|
|
|
2014-03-16 15:59:04 -07:00
|
|
|
/// A User Datagram Protocol socket.
|
2014-03-25 10:23:51 -07:00
|
|
|
///
|
|
|
|
/// This is an implementation of a bound UDP socket. This supports both IPv4 and
|
|
|
|
/// IPv6 addresses, and there is no corresponding notion of a server because UDP
|
|
|
|
/// is a datagram protocol.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2014-04-14 21:00:31 +05:30
|
|
|
/// # #![allow(unused_must_use)]
|
2014-03-25 10:23:51 -07:00
|
|
|
/// use std::io::net::udp::UdpSocket;
|
|
|
|
/// use std::io::net::ip::{Ipv4Addr, SocketAddr};
|
|
|
|
///
|
|
|
|
/// let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 34254 };
|
|
|
|
/// let mut socket = match UdpSocket::bind(addr) {
|
|
|
|
/// Ok(s) => s,
|
|
|
|
/// Err(e) => fail!("couldn't bind socket: {}", e),
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// let mut buf = [0, ..10];
|
2014-06-30 12:08:27 +09:00
|
|
|
/// match socket.recv_from(buf) {
|
2014-03-25 10:23:51 -07:00
|
|
|
/// Ok((amt, src)) => {
|
|
|
|
/// // Send a reply to the socket we received data from
|
|
|
|
/// let buf = buf.mut_slice_to(amt);
|
|
|
|
/// buf.reverse();
|
2014-06-30 12:08:27 +09:00
|
|
|
/// socket.send_to(buf, src);
|
2014-03-25 10:23:51 -07:00
|
|
|
/// }
|
|
|
|
/// Err(e) => println!("couldn't receive a datagram: {}", e)
|
|
|
|
/// }
|
|
|
|
/// drop(socket); // close the socket
|
|
|
|
/// ```
|
2013-10-02 18:52:03 -07:00
|
|
|
pub struct UdpSocket {
|
2014-06-14 11:03:34 -07:00
|
|
|
obj: Box<RtioUdpSocket + Send>,
|
2013-10-02 18:52:03 -07:00
|
|
|
}
|
2013-06-19 17:23:55 -07:00
|
|
|
|
|
|
|
impl UdpSocket {
|
2014-03-16 15:59:04 -07:00
|
|
|
/// Creates a UDP socket from the given socket address.
|
2014-01-29 16:33:57 -08:00
|
|
|
pub fn bind(addr: SocketAddr) -> IoResult<UdpSocket> {
|
2014-06-03 20:09:39 -07:00
|
|
|
let SocketAddr { ip, port } = addr;
|
2013-12-12 17:30:41 -08:00
|
|
|
LocalIo::maybe_raise(|io| {
|
2014-06-03 20:09:39 -07:00
|
|
|
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
|
2013-12-12 17:30:41 -08:00
|
|
|
io.udp_bind(addr).map(|s| UdpSocket { obj: s })
|
2014-06-03 20:09:39 -07:00
|
|
|
}).map_err(IoError::from_rtio_error)
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
|
|
|
|
2014-03-16 15:59:04 -07:00
|
|
|
/// Receives data from the socket. On success, returns the number of bytes
|
|
|
|
/// read and the address from whence the data came.
|
2014-06-30 12:08:27 +09:00
|
|
|
pub fn recv_from(&mut self, buf: &mut [u8])
|
2014-03-16 15:59:04 -07:00
|
|
|
-> IoResult<(uint, SocketAddr)> {
|
2014-06-30 12:08:27 +09:00
|
|
|
match self.obj.recv_from(buf) {
|
2014-06-03 20:09:39 -07:00
|
|
|
Ok((amt, rtio::SocketAddr { ip, port })) => {
|
|
|
|
Ok((amt, SocketAddr { ip: super::from_rtio(ip), port: port }))
|
|
|
|
}
|
|
|
|
Err(e) => Err(IoError::from_rtio_error(e)),
|
|
|
|
}
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
|
|
|
|
2014-07-01 08:34:18 +09:00
|
|
|
#[allow(missing_doc)]
|
|
|
|
#[deprecated = "renamed to `recv_from`"]
|
|
|
|
pub fn recvfrom(&mut self, buf: &mut [u8])
|
|
|
|
-> IoResult<(uint, SocketAddr)> {
|
|
|
|
self.recv_from(buf)
|
|
|
|
}
|
|
|
|
|
2014-03-16 15:59:04 -07:00
|
|
|
/// Sends data on the socket to the given address. Returns nothing on
|
|
|
|
/// success.
|
2014-06-30 12:08:27 +09:00
|
|
|
pub fn send_to(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()> {
|
|
|
|
self.obj.send_to(buf, rtio::SocketAddr {
|
2014-06-03 20:09:39 -07:00
|
|
|
ip: super::to_rtio(dst.ip),
|
|
|
|
port: dst.port,
|
|
|
|
}).map_err(IoError::from_rtio_error)
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
|
|
|
|
2014-07-01 08:34:18 +09:00
|
|
|
#[allow(missing_doc)]
|
|
|
|
#[deprecated = "renamed to `send_to`"]
|
|
|
|
pub fn sendto(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()> {
|
|
|
|
self.send_to(buf, dst)
|
|
|
|
}
|
|
|
|
|
2014-03-16 15:59:04 -07:00
|
|
|
/// Creates a `UdpStream`, which allows use of the `Reader` and `Writer`
|
|
|
|
/// traits to receive and send data from the same address. This transfers
|
|
|
|
/// ownership of the socket to the stream.
|
|
|
|
///
|
|
|
|
/// Note that this call does not perform any actual network communication,
|
|
|
|
/// because UDP is a datagram protocol.
|
2013-08-03 02:06:24 +04:00
|
|
|
pub fn connect(self, other: SocketAddr) -> UdpStream {
|
2014-03-16 15:59:04 -07:00
|
|
|
UdpStream {
|
|
|
|
socket: self,
|
|
|
|
connected_to: other,
|
|
|
|
}
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
2013-07-25 02:33:43 -04:00
|
|
|
|
2014-03-16 15:59:04 -07:00
|
|
|
/// Returns the socket address that this socket was created from.
|
2014-01-29 16:33:57 -08:00
|
|
|
pub fn socket_name(&mut self) -> IoResult<SocketAddr> {
|
2014-06-03 20:09:39 -07:00
|
|
|
match self.obj.socket_name() {
|
|
|
|
Ok(a) => Ok(SocketAddr { ip: super::from_rtio(a.ip), port: a.port }),
|
|
|
|
Err(e) => Err(IoError::from_rtio_error(e))
|
|
|
|
}
|
2013-07-25 02:33:43 -04:00
|
|
|
}
|
2014-04-25 11:13:02 -07:00
|
|
|
|
|
|
|
/// Joins a multicast IP address (becomes a member of it)
|
|
|
|
#[experimental]
|
|
|
|
pub fn join_multicast(&mut self, multi: IpAddr) -> IoResult<()> {
|
2014-06-03 20:09:39 -07:00
|
|
|
let e = self.obj.join_multicast(super::to_rtio(multi));
|
|
|
|
e.map_err(IoError::from_rtio_error)
|
2014-04-25 11:13:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Leaves a multicast IP address (drops membership from it)
|
|
|
|
#[experimental]
|
|
|
|
pub fn leave_multicast(&mut self, multi: IpAddr) -> IoResult<()> {
|
2014-06-03 20:09:39 -07:00
|
|
|
let e = self.obj.leave_multicast(super::to_rtio(multi));
|
|
|
|
e.map_err(IoError::from_rtio_error)
|
2014-04-25 11:13:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the multicast loop flag to the specified value
|
|
|
|
///
|
|
|
|
/// This lets multicast packets loop back to local sockets (if enabled)
|
|
|
|
#[experimental]
|
|
|
|
pub fn set_multicast_loop(&mut self, on: bool) -> IoResult<()> {
|
|
|
|
if on {
|
|
|
|
self.obj.loop_multicast_locally()
|
|
|
|
} else {
|
|
|
|
self.obj.dont_loop_multicast_locally()
|
2014-06-03 20:09:39 -07:00
|
|
|
}.map_err(IoError::from_rtio_error)
|
2014-04-25 11:13:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the multicast TTL
|
|
|
|
#[experimental]
|
|
|
|
pub fn set_multicast_ttl(&mut self, ttl: int) -> IoResult<()> {
|
2014-06-03 20:09:39 -07:00
|
|
|
self.obj.multicast_time_to_live(ttl).map_err(IoError::from_rtio_error)
|
2014-04-25 11:13:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets this socket's TTL
|
|
|
|
#[experimental]
|
|
|
|
pub fn set_ttl(&mut self, ttl: int) -> IoResult<()> {
|
2014-06-03 20:09:39 -07:00
|
|
|
self.obj.time_to_live(ttl).map_err(IoError::from_rtio_error)
|
2014-04-25 11:13:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the broadcast flag on or off
|
|
|
|
#[experimental]
|
2014-07-03 01:30:00 -04:00
|
|
|
pub fn set_broadcast(&mut self, broadcast: bool) -> IoResult<()> {
|
2014-04-25 11:13:02 -07:00
|
|
|
if broadcast {
|
|
|
|
self.obj.hear_broadcasts()
|
|
|
|
} else {
|
|
|
|
self.obj.ignore_broadcasts()
|
2014-06-03 20:09:39 -07:00
|
|
|
}.map_err(IoError::from_rtio_error)
|
2014-04-25 11:13:02 -07:00
|
|
|
}
|
2014-04-25 20:47:49 -07:00
|
|
|
|
2014-07-03 01:30:00 -04:00
|
|
|
/// Sets the broadcast flag on or off
|
|
|
|
#[deprecated="renamed to `set_broadcast`"]
|
|
|
|
pub fn set_broadast(&mut self, broadcast: bool) -> IoResult<()> {
|
|
|
|
self.set_broadcast(broadcast)
|
|
|
|
}
|
|
|
|
|
2014-04-25 20:47:49 -07:00
|
|
|
/// Sets the read/write timeout for this socket.
|
|
|
|
///
|
|
|
|
/// For more information, see `TcpStream::set_timeout`
|
2014-05-08 01:45:08 -07:00
|
|
|
#[experimental = "the timeout argument may change in type and value"]
|
2014-04-25 20:47:49 -07:00
|
|
|
pub fn set_timeout(&mut self, timeout_ms: Option<u64>) {
|
|
|
|
self.obj.set_timeout(timeout_ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the read timeout for this socket.
|
|
|
|
///
|
|
|
|
/// For more information, see `TcpStream::set_timeout`
|
2014-05-08 01:45:08 -07:00
|
|
|
#[experimental = "the timeout argument may change in type and value"]
|
2014-04-25 20:47:49 -07:00
|
|
|
pub fn set_read_timeout(&mut self, timeout_ms: Option<u64>) {
|
|
|
|
self.obj.set_read_timeout(timeout_ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the write timeout for this socket.
|
|
|
|
///
|
|
|
|
/// For more information, see `TcpStream::set_timeout`
|
2014-05-08 01:45:08 -07:00
|
|
|
#[experimental = "the timeout argument may change in type and value"]
|
2014-04-25 20:47:49 -07:00
|
|
|
pub fn set_write_timeout(&mut self, timeout_ms: Option<u64>) {
|
|
|
|
self.obj.set_write_timeout(timeout_ms)
|
|
|
|
}
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
2014-01-22 19:32:16 -08:00
|
|
|
impl Clone for UdpSocket {
|
2014-03-16 15:59:04 -07:00
|
|
|
/// Creates a new handle to this UDP socket, allowing for simultaneous
|
|
|
|
/// reads and writes of the socket.
|
2014-01-22 19:32:16 -08:00
|
|
|
///
|
|
|
|
/// The underlying UDP socket will not be closed until all handles to the
|
2014-03-16 15:59:04 -07:00
|
|
|
/// socket have been deallocated. Two concurrent reads will not receive
|
|
|
|
/// the same data. Instead, the first read will receive the first packet
|
2014-01-22 19:32:16 -08:00
|
|
|
/// received, and the second read will receive the second packet.
|
|
|
|
fn clone(&self) -> UdpSocket {
|
2014-03-16 15:59:04 -07:00
|
|
|
UdpSocket {
|
|
|
|
obj: self.obj.clone(),
|
|
|
|
}
|
2014-01-22 19:32:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 15:59:04 -07:00
|
|
|
/// A type that allows convenient usage of a UDP stream connected to one
|
|
|
|
/// address via the `Reader` and `Writer` traits.
|
2013-06-17 12:34:58 -07:00
|
|
|
pub struct UdpStream {
|
2014-03-27 15:09:47 -07:00
|
|
|
socket: UdpSocket,
|
|
|
|
connected_to: SocketAddr
|
2013-06-17 12:34:58 -07:00
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
|
|
|
impl UdpStream {
|
2014-03-16 15:59:04 -07:00
|
|
|
/// Allows access to the underlying UDP socket owned by this stream. This
|
|
|
|
/// is useful to, for example, use the socket to send data to hosts other
|
|
|
|
/// than the one that this stream is connected to.
|
2013-11-18 21:15:42 -08:00
|
|
|
pub fn as_socket<T>(&mut self, f: |&mut UdpSocket| -> T) -> T {
|
|
|
|
f(&mut self.socket)
|
|
|
|
}
|
2013-06-17 12:34:58 -07:00
|
|
|
|
2014-03-16 15:59:04 -07:00
|
|
|
/// Consumes this UDP stream and returns out the underlying socket.
|
|
|
|
pub fn disconnect(self) -> UdpSocket {
|
|
|
|
self.socket
|
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for UdpStream {
|
2014-01-29 16:33:57 -08:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2014-02-16 01:02:31 -05:00
|
|
|
let peer = self.connected_to;
|
2013-11-20 14:17:12 -08:00
|
|
|
self.as_socket(|sock| {
|
2014-06-30 12:08:27 +09:00
|
|
|
match sock.recv_from(buf) {
|
2014-01-29 16:33:57 -08:00
|
|
|
Ok((_nread, src)) if src != peer => Ok(0),
|
|
|
|
Ok((nread, _src)) => Ok(nread),
|
|
|
|
Err(e) => Err(e),
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
2013-11-20 14:17:12 -08:00
|
|
|
})
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for UdpStream {
|
2014-01-29 16:33:57 -08:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-02-16 01:02:31 -05:00
|
|
|
let connected_to = self.connected_to;
|
2014-06-30 12:08:27 +09:00
|
|
|
self.as_socket(|sock| sock.send_to(buf, connected_to))
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 11:43:40 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
#[allow(experimental)]
|
2013-06-25 11:43:40 -07:00
|
|
|
mod test {
|
|
|
|
use super::*;
|
2014-01-05 13:05:02 -05:00
|
|
|
use io::net::ip::{SocketAddr};
|
2013-06-25 11:43:40 -07:00
|
|
|
|
2014-01-13 19:45:08 -08:00
|
|
|
// FIXME #11530 this fails on android because tests are run as root
|
2013-12-28 16:40:15 -08:00
|
|
|
iotest!(fn bind_error() {
|
2014-01-30 14:10:53 -08:00
|
|
|
let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
|
|
|
|
match UdpSocket::bind(addr) {
|
|
|
|
Ok(..) => fail!(),
|
|
|
|
Err(e) => assert_eq!(e.kind, PermissionDenied),
|
|
|
|
}
|
2014-01-13 19:45:08 -08:00
|
|
|
} #[ignore(cfg(windows))] #[ignore(cfg(target_os = "android"))])
|
2013-06-25 11:43:40 -07:00
|
|
|
|
2013-12-28 16:40:15 -08:00
|
|
|
iotest!(fn socket_smoke_test_ip4() {
|
2013-12-12 17:20:58 -08:00
|
|
|
let server_ip = next_test_ip4();
|
|
|
|
let client_ip = next_test_ip4();
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx1, rx1) = channel();
|
|
|
|
let (tx2, rx2) = channel();
|
2013-06-25 11:43:40 -07:00
|
|
|
|
2014-01-26 22:42:26 -05:00
|
|
|
spawn(proc() {
|
2013-12-05 18:19:06 -08:00
|
|
|
match UdpSocket::bind(client_ip) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(ref mut client) => {
|
2014-03-09 14:58:32 -07:00
|
|
|
rx1.recv();
|
2014-06-30 12:08:27 +09:00
|
|
|
client.send_to([99], server_ip).unwrap()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
2014-03-09 14:58:32 -07:00
|
|
|
tx2.send(());
|
2014-01-26 22:42:26 -05:00
|
|
|
});
|
2013-06-25 11:43:40 -07:00
|
|
|
|
2013-12-12 17:20:58 -08:00
|
|
|
match UdpSocket::bind(server_ip) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(ref mut server) => {
|
2014-03-09 14:58:32 -07:00
|
|
|
tx1.send(());
|
2013-12-12 17:20:58 -08:00
|
|
|
let mut buf = [0];
|
2014-06-30 12:08:27 +09:00
|
|
|
match server.recv_from(buf) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok((nread, src)) => {
|
2013-12-12 17:20:58 -08:00
|
|
|
assert_eq!(nread, 1);
|
|
|
|
assert_eq!(buf[0], 99);
|
|
|
|
assert_eq!(src, client_ip);
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-12-12 17:20:58 -08:00
|
|
|
}
|
2014-03-09 14:58:32 -07:00
|
|
|
rx2.recv();
|
2013-12-28 16:40:15 -08:00
|
|
|
})
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2013-12-28 16:40:15 -08:00
|
|
|
iotest!(fn socket_smoke_test_ip6() {
|
2013-12-12 17:20:58 -08:00
|
|
|
let server_ip = next_test_ip6();
|
|
|
|
let client_ip = next_test_ip6();
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel::<()>();
|
2013-12-12 17:20:58 -08:00
|
|
|
|
2014-01-26 22:42:26 -05:00
|
|
|
spawn(proc() {
|
2013-12-05 18:19:06 -08:00
|
|
|
match UdpSocket::bind(client_ip) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(ref mut client) => {
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2014-06-30 12:08:27 +09:00
|
|
|
client.send_to([99], server_ip).unwrap()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
2014-01-26 22:42:26 -05:00
|
|
|
});
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2013-12-12 17:20:58 -08:00
|
|
|
match UdpSocket::bind(server_ip) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(ref mut server) => {
|
2014-03-09 14:58:32 -07:00
|
|
|
tx.send(());
|
2013-12-12 17:20:58 -08:00
|
|
|
let mut buf = [0];
|
2014-06-30 12:08:27 +09:00
|
|
|
match server.recv_from(buf) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok((nread, src)) => {
|
2013-12-12 17:20:58 -08:00
|
|
|
assert_eq!(nread, 1);
|
|
|
|
assert_eq!(buf[0], 99);
|
|
|
|
assert_eq!(src, client_ip);
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-12-12 17:20:58 -08:00
|
|
|
}
|
2013-12-28 16:40:15 -08:00
|
|
|
})
|
2013-12-12 17:20:58 -08:00
|
|
|
|
2013-12-28 16:40:15 -08:00
|
|
|
iotest!(fn stream_smoke_test_ip4() {
|
2013-12-12 17:20:58 -08:00
|
|
|
let server_ip = next_test_ip4();
|
|
|
|
let client_ip = next_test_ip4();
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx1, rx1) = channel();
|
|
|
|
let (tx2, rx2) = channel();
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2014-01-26 22:42:26 -05:00
|
|
|
spawn(proc() {
|
2013-12-05 18:19:06 -08:00
|
|
|
match UdpSocket::bind(client_ip) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(client) => {
|
2014-04-25 01:08:02 -07:00
|
|
|
let client = box client;
|
2013-12-05 18:19:06 -08:00
|
|
|
let mut stream = client.connect(server_ip);
|
2014-03-09 14:58:32 -07:00
|
|
|
rx1.recv();
|
2014-01-30 14:10:53 -08:00
|
|
|
stream.write([99]).unwrap();
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
2014-03-09 14:58:32 -07:00
|
|
|
tx2.send(());
|
2014-01-26 22:42:26 -05:00
|
|
|
});
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2013-12-12 17:20:58 -08:00
|
|
|
match UdpSocket::bind(server_ip) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(server) => {
|
2014-04-25 01:08:02 -07:00
|
|
|
let server = box server;
|
2013-12-12 17:20:58 -08:00
|
|
|
let mut stream = server.connect(client_ip);
|
2014-03-09 14:58:32 -07:00
|
|
|
tx1.send(());
|
2013-12-12 17:20:58 -08:00
|
|
|
let mut buf = [0];
|
|
|
|
match stream.read(buf) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(nread) => {
|
2013-12-12 17:20:58 -08:00
|
|
|
assert_eq!(nread, 1);
|
|
|
|
assert_eq!(buf[0], 99);
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-12-12 17:20:58 -08:00
|
|
|
}
|
2014-03-09 14:58:32 -07:00
|
|
|
rx2.recv();
|
2013-12-28 16:40:15 -08:00
|
|
|
})
|
2013-12-12 17:20:58 -08:00
|
|
|
|
2013-12-28 16:40:15 -08:00
|
|
|
iotest!(fn stream_smoke_test_ip6() {
|
2013-12-12 17:20:58 -08:00
|
|
|
let server_ip = next_test_ip6();
|
|
|
|
let client_ip = next_test_ip6();
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx1, rx1) = channel();
|
|
|
|
let (tx2, rx2) = channel();
|
2013-06-25 11:43:40 -07:00
|
|
|
|
2014-01-26 22:42:26 -05:00
|
|
|
spawn(proc() {
|
2013-12-05 18:19:06 -08:00
|
|
|
match UdpSocket::bind(client_ip) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(client) => {
|
2014-04-25 01:08:02 -07:00
|
|
|
let client = box client;
|
2013-12-05 18:19:06 -08:00
|
|
|
let mut stream = client.connect(server_ip);
|
2014-03-09 14:58:32 -07:00
|
|
|
rx1.recv();
|
2014-01-30 14:10:53 -08:00
|
|
|
stream.write([99]).unwrap();
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
2014-03-09 14:58:32 -07:00
|
|
|
tx2.send(());
|
2014-01-26 22:42:26 -05:00
|
|
|
});
|
2013-12-12 17:20:58 -08:00
|
|
|
|
|
|
|
match UdpSocket::bind(server_ip) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(server) => {
|
2014-04-25 01:08:02 -07:00
|
|
|
let server = box server;
|
2013-12-12 17:20:58 -08:00
|
|
|
let mut stream = server.connect(client_ip);
|
2014-03-09 14:58:32 -07:00
|
|
|
tx1.send(());
|
2013-12-12 17:20:58 -08:00
|
|
|
let mut buf = [0];
|
|
|
|
match stream.read(buf) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(nread) => {
|
2013-12-12 17:20:58 -08:00
|
|
|
assert_eq!(nread, 1);
|
|
|
|
assert_eq!(buf[0], 99);
|
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-12-12 17:20:58 -08:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
Err(..) => fail!()
|
2013-12-12 17:20:58 -08:00
|
|
|
}
|
2014-03-09 14:58:32 -07:00
|
|
|
rx2.recv();
|
2013-12-28 16:40:15 -08:00
|
|
|
})
|
2013-07-26 05:02:53 -04:00
|
|
|
|
2013-12-28 16:40:15 -08:00
|
|
|
pub fn socket_name(addr: SocketAddr) {
|
2013-12-12 17:20:58 -08:00
|
|
|
let server = UdpSocket::bind(addr);
|
2013-07-26 05:02:53 -04:00
|
|
|
|
2014-01-30 14:10:53 -08:00
|
|
|
assert!(server.is_ok());
|
2013-12-12 17:20:58 -08:00
|
|
|
let mut server = server.unwrap();
|
2013-07-26 05:02:53 -04:00
|
|
|
|
2013-12-12 17:20:58 -08:00
|
|
|
// Make sure socket_name gives
|
|
|
|
// us the socket we binded to.
|
|
|
|
let so_name = server.socket_name();
|
2014-01-30 14:10:53 -08:00
|
|
|
assert!(so_name.is_ok());
|
2013-12-12 17:20:58 -08:00
|
|
|
assert_eq!(addr, so_name.unwrap());
|
2013-07-26 05:02:53 -04:00
|
|
|
}
|
|
|
|
|
2013-12-28 16:40:15 -08:00
|
|
|
iotest!(fn socket_name_ip4() {
|
2013-07-26 05:02:53 -04:00
|
|
|
socket_name(next_test_ip4());
|
2013-12-28 16:40:15 -08:00
|
|
|
})
|
2013-07-26 05:02:53 -04:00
|
|
|
|
2013-12-28 16:40:15 -08:00
|
|
|
iotest!(fn socket_name_ip6() {
|
2013-07-26 05:02:53 -04:00
|
|
|
socket_name(next_test_ip6());
|
2013-12-28 16:40:15 -08:00
|
|
|
})
|
2014-01-22 19:32:16 -08:00
|
|
|
|
|
|
|
iotest!(fn udp_clone_smoke() {
|
|
|
|
let addr1 = next_test_ip4();
|
|
|
|
let addr2 = next_test_ip4();
|
|
|
|
let mut sock1 = UdpSocket::bind(addr1).unwrap();
|
|
|
|
let sock2 = UdpSocket::bind(addr2).unwrap();
|
|
|
|
|
|
|
|
spawn(proc() {
|
|
|
|
let mut sock2 = sock2;
|
|
|
|
let mut buf = [0, 0];
|
2014-06-30 12:08:27 +09:00
|
|
|
assert_eq!(sock2.recv_from(buf), Ok((1, addr1)));
|
2014-01-22 19:32:16 -08:00
|
|
|
assert_eq!(buf[0], 1);
|
2014-06-30 12:08:27 +09:00
|
|
|
sock2.send_to([2], addr1).unwrap();
|
2014-01-22 19:32:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
let sock3 = sock1.clone();
|
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx1, rx1) = channel();
|
|
|
|
let (tx2, rx2) = channel();
|
2014-01-22 19:32:16 -08:00
|
|
|
spawn(proc() {
|
|
|
|
let mut sock3 = sock3;
|
2014-03-09 14:58:32 -07:00
|
|
|
rx1.recv();
|
2014-06-30 12:08:27 +09:00
|
|
|
sock3.send_to([1], addr2).unwrap();
|
2014-03-09 14:58:32 -07:00
|
|
|
tx2.send(());
|
2014-01-22 19:32:16 -08:00
|
|
|
});
|
2014-03-09 14:58:32 -07:00
|
|
|
tx1.send(());
|
2014-01-22 19:32:16 -08:00
|
|
|
let mut buf = [0, 0];
|
2014-06-30 12:08:27 +09:00
|
|
|
assert_eq!(sock1.recv_from(buf), Ok((1, addr2)));
|
2014-03-09 14:58:32 -07:00
|
|
|
rx2.recv();
|
2014-01-22 19:32:16 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
iotest!(fn udp_clone_two_read() {
|
|
|
|
let addr1 = next_test_ip4();
|
|
|
|
let addr2 = next_test_ip4();
|
|
|
|
let mut sock1 = UdpSocket::bind(addr1).unwrap();
|
|
|
|
let sock2 = UdpSocket::bind(addr2).unwrap();
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx1, rx) = channel();
|
|
|
|
let tx2 = tx1.clone();
|
2014-01-22 19:32:16 -08:00
|
|
|
|
|
|
|
spawn(proc() {
|
|
|
|
let mut sock2 = sock2;
|
2014-06-30 12:08:27 +09:00
|
|
|
sock2.send_to([1], addr1).unwrap();
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2014-06-30 12:08:27 +09:00
|
|
|
sock2.send_to([2], addr1).unwrap();
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2014-01-22 19:32:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
let sock3 = sock1.clone();
|
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
let (done, rx) = channel();
|
2014-01-22 19:32:16 -08:00
|
|
|
spawn(proc() {
|
|
|
|
let mut sock3 = sock3;
|
|
|
|
let mut buf = [0, 0];
|
2014-06-30 12:08:27 +09:00
|
|
|
sock3.recv_from(buf).unwrap();
|
2014-03-09 14:58:32 -07:00
|
|
|
tx2.send(());
|
2014-01-22 19:32:16 -08:00
|
|
|
done.send(());
|
|
|
|
});
|
|
|
|
let mut buf = [0, 0];
|
2014-06-30 12:08:27 +09:00
|
|
|
sock1.recv_from(buf).unwrap();
|
2014-03-09 14:58:32 -07:00
|
|
|
tx1.send(());
|
2014-01-22 19:32:16 -08:00
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2014-01-22 19:32:16 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
iotest!(fn udp_clone_two_write() {
|
|
|
|
let addr1 = next_test_ip4();
|
|
|
|
let addr2 = next_test_ip4();
|
|
|
|
let mut sock1 = UdpSocket::bind(addr1).unwrap();
|
|
|
|
let sock2 = UdpSocket::bind(addr2).unwrap();
|
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
let (serv_tx, serv_rx) = channel();
|
2014-01-22 19:32:16 -08:00
|
|
|
|
|
|
|
spawn(proc() {
|
|
|
|
let mut sock2 = sock2;
|
|
|
|
let mut buf = [0, 1];
|
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2014-06-30 12:08:27 +09:00
|
|
|
match sock2.recv_from(buf) {
|
2014-02-05 16:09:59 -08:00
|
|
|
Ok(..) => {}
|
|
|
|
Err(e) => fail!("failed receive: {}", e),
|
2014-01-22 19:32:16 -08:00
|
|
|
}
|
2014-03-09 14:58:32 -07:00
|
|
|
serv_tx.send(());
|
2014-01-22 19:32:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
let sock3 = sock1.clone();
|
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
let (done, rx) = channel();
|
|
|
|
let tx2 = tx.clone();
|
2014-01-22 19:32:16 -08:00
|
|
|
spawn(proc() {
|
|
|
|
let mut sock3 = sock3;
|
2014-06-30 12:08:27 +09:00
|
|
|
match sock3.send_to([1], addr2) {
|
std: Make std::comm return types consistent
There are currently a number of return values from the std::comm methods, not
all of which are necessarily completely expressive:
Sender::try_send(t: T) -> bool
This method currently doesn't transmit back the data `t` if the send fails
due to the other end having disconnected. Additionally, this shares the name
of the synchronous try_send method, but it differs in semantics in that it
only has one failure case, not two (the buffer can never be full).
SyncSender::try_send(t: T) -> TrySendResult<T>
This method accurately conveys all possible information, but it uses a
custom type to the std::comm module with no convenience methods on it.
Additionally, if you want to inspect the result you're forced to import
something from `std::comm`.
SyncSender::send_opt(t: T) -> Option<T>
This method uses Some(T) as an "error value" and None as a "success value",
but almost all other uses of Option<T> have Some/None the other way
Receiver::try_recv(t: T) -> TryRecvResult<T>
Similarly to the synchronous try_send, this custom return type is lacking in
terms of usability (no convenience methods).
With this number of drawbacks in mind, I believed it was time to re-work the
return types of these methods. The new API for the comm module is:
Sender::send(t: T) -> ()
Sender::send_opt(t: T) -> Result<(), T>
SyncSender::send(t: T) -> ()
SyncSender::send_opt(t: T) -> Result<(), T>
SyncSender::try_send(t: T) -> Result<(), TrySendError<T>>
Receiver::recv() -> T
Receiver::recv_opt() -> Result<T, ()>
Receiver::try_recv() -> Result<T, TryRecvError>
The notable changes made are:
* Sender::try_send => Sender::send_opt. This renaming brings the semantics in
line with the SyncSender::send_opt method. An asychronous send only has one
failure case, unlike the synchronous try_send method which has two failure
cases (full/disconnected).
* Sender::send_opt returns the data back to the caller if the send is guaranteed
to fail. This method previously returned `bool`, but then it was unable to
retrieve the data if the data was guaranteed to fail to send. There is still a
race such that when `Ok(())` is returned the data could still fail to be
received, but that's inherent to an asynchronous channel.
* Result is now the basis of all return values. This not only adds lots of
convenience methods to all return values for free, but it also means that you
can inspect the return values with no extra imports (Ok/Err are in the
prelude). Additionally, it's now self documenting when something failed or not
because the return value has "Err" in the name.
Things I'm a little uneasy about:
* The methods send_opt and recv_opt are not returning options, but rather
results. I felt more strongly that Option was the wrong return type than the
_opt prefix was wrong, and I coudn't think of a much better name for these
methods. One possible way to think about them is to read the _opt suffix as
"optionally".
* Result<T, ()> is often better expressed as Option<T>. This is only applicable
to the recv_opt() method, but I thought it would be more consistent for
everything to return Result rather than one method returning an Option.
Despite my two reasons to feel uneasy, I feel much better about the consistency
in return values at this point, and I think the only real open question is if
there's a better suffix for {send,recv}_opt.
Closes #11527
2014-04-10 10:53:49 -07:00
|
|
|
Ok(..) => { let _ = tx2.send_opt(()); }
|
2014-01-22 19:32:16 -08:00
|
|
|
Err(..) => {}
|
|
|
|
}
|
|
|
|
done.send(());
|
|
|
|
});
|
2014-06-30 12:08:27 +09:00
|
|
|
match sock1.send_to([2], addr2) {
|
std: Make std::comm return types consistent
There are currently a number of return values from the std::comm methods, not
all of which are necessarily completely expressive:
Sender::try_send(t: T) -> bool
This method currently doesn't transmit back the data `t` if the send fails
due to the other end having disconnected. Additionally, this shares the name
of the synchronous try_send method, but it differs in semantics in that it
only has one failure case, not two (the buffer can never be full).
SyncSender::try_send(t: T) -> TrySendResult<T>
This method accurately conveys all possible information, but it uses a
custom type to the std::comm module with no convenience methods on it.
Additionally, if you want to inspect the result you're forced to import
something from `std::comm`.
SyncSender::send_opt(t: T) -> Option<T>
This method uses Some(T) as an "error value" and None as a "success value",
but almost all other uses of Option<T> have Some/None the other way
Receiver::try_recv(t: T) -> TryRecvResult<T>
Similarly to the synchronous try_send, this custom return type is lacking in
terms of usability (no convenience methods).
With this number of drawbacks in mind, I believed it was time to re-work the
return types of these methods. The new API for the comm module is:
Sender::send(t: T) -> ()
Sender::send_opt(t: T) -> Result<(), T>
SyncSender::send(t: T) -> ()
SyncSender::send_opt(t: T) -> Result<(), T>
SyncSender::try_send(t: T) -> Result<(), TrySendError<T>>
Receiver::recv() -> T
Receiver::recv_opt() -> Result<T, ()>
Receiver::try_recv() -> Result<T, TryRecvError>
The notable changes made are:
* Sender::try_send => Sender::send_opt. This renaming brings the semantics in
line with the SyncSender::send_opt method. An asychronous send only has one
failure case, unlike the synchronous try_send method which has two failure
cases (full/disconnected).
* Sender::send_opt returns the data back to the caller if the send is guaranteed
to fail. This method previously returned `bool`, but then it was unable to
retrieve the data if the data was guaranteed to fail to send. There is still a
race such that when `Ok(())` is returned the data could still fail to be
received, but that's inherent to an asynchronous channel.
* Result is now the basis of all return values. This not only adds lots of
convenience methods to all return values for free, but it also means that you
can inspect the return values with no extra imports (Ok/Err are in the
prelude). Additionally, it's now self documenting when something failed or not
because the return value has "Err" in the name.
Things I'm a little uneasy about:
* The methods send_opt and recv_opt are not returning options, but rather
results. I felt more strongly that Option was the wrong return type than the
_opt prefix was wrong, and I coudn't think of a much better name for these
methods. One possible way to think about them is to read the _opt suffix as
"optionally".
* Result<T, ()> is often better expressed as Option<T>. This is only applicable
to the recv_opt() method, but I thought it would be more consistent for
everything to return Result rather than one method returning an Option.
Despite my two reasons to feel uneasy, I feel much better about the consistency
in return values at this point, and I think the only real open question is if
there's a better suffix for {send,recv}_opt.
Closes #11527
2014-04-10 10:53:49 -07:00
|
|
|
Ok(..) => { let _ = tx.send_opt(()); }
|
2014-01-22 19:32:16 -08:00
|
|
|
Err(..) => {}
|
|
|
|
}
|
2014-03-09 14:58:32 -07:00
|
|
|
drop(tx);
|
2014-01-22 19:32:16 -08:00
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
|
|
|
serv_rx.recv();
|
2014-01-22 19:32:16 -08:00
|
|
|
})
|
2014-04-25 20:47:49 -07:00
|
|
|
|
2014-06-30 12:08:27 +09:00
|
|
|
iotest!(fn recv_from_timeout() {
|
2014-04-25 20:47:49 -07:00
|
|
|
let addr1 = next_test_ip4();
|
|
|
|
let addr2 = next_test_ip4();
|
|
|
|
let mut a = UdpSocket::bind(addr1).unwrap();
|
|
|
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
let (tx2, rx2) = channel();
|
|
|
|
spawn(proc() {
|
|
|
|
let mut a = UdpSocket::bind(addr2).unwrap();
|
2014-06-30 12:08:27 +09:00
|
|
|
assert_eq!(a.recv_from([0]), Ok((1, addr1)));
|
|
|
|
assert_eq!(a.send_to([0], addr1), Ok(()));
|
2014-04-25 20:47:49 -07:00
|
|
|
rx.recv();
|
2014-06-30 12:08:27 +09:00
|
|
|
assert_eq!(a.send_to([0], addr1), Ok(()));
|
2014-04-25 20:47:49 -07:00
|
|
|
|
|
|
|
tx2.send(());
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure that reads time out, but writes can continue
|
|
|
|
a.set_read_timeout(Some(20));
|
2014-06-30 12:08:27 +09:00
|
|
|
assert_eq!(a.recv_from([0]).err().unwrap().kind, TimedOut);
|
|
|
|
assert_eq!(a.recv_from([0]).err().unwrap().kind, TimedOut);
|
|
|
|
assert_eq!(a.send_to([0], addr2), Ok(()));
|
2014-04-25 20:47:49 -07:00
|
|
|
|
|
|
|
// Cloned handles should be able to block
|
|
|
|
let mut a2 = a.clone();
|
2014-06-30 12:08:27 +09:00
|
|
|
assert_eq!(a2.recv_from([0]), Ok((1, addr2)));
|
2014-04-25 20:47:49 -07:00
|
|
|
|
|
|
|
// Clearing the timeout should allow for receiving
|
|
|
|
a.set_timeout(None);
|
|
|
|
tx.send(());
|
2014-06-30 12:08:27 +09:00
|
|
|
assert_eq!(a2.recv_from([0]), Ok((1, addr2)));
|
2014-04-25 20:47:49 -07:00
|
|
|
|
|
|
|
// Make sure the child didn't die
|
|
|
|
rx2.recv();
|
|
|
|
})
|
|
|
|
|
2014-06-30 12:08:27 +09:00
|
|
|
iotest!(fn send_to_timeout() {
|
2014-04-25 20:47:49 -07:00
|
|
|
let addr1 = next_test_ip4();
|
|
|
|
let addr2 = next_test_ip4();
|
|
|
|
let mut a = UdpSocket::bind(addr1).unwrap();
|
|
|
|
let _b = UdpSocket::bind(addr2).unwrap();
|
|
|
|
|
|
|
|
a.set_write_timeout(Some(1000));
|
2014-04-21 17:58:52 -04:00
|
|
|
for _ in range(0u, 100) {
|
2014-06-30 12:08:27 +09:00
|
|
|
match a.send_to([0, ..4*1024], addr2) {
|
2014-04-25 20:47:49 -07:00
|
|
|
Ok(()) | Err(IoError { kind: ShortWrite(..), .. }) => {},
|
|
|
|
Err(IoError { kind: TimedOut, .. }) => break,
|
|
|
|
Err(e) => fail!("other error: {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|