std: Make the TCP/UDP connect_timeout methods take Duration
[breaking-change]
This commit is contained in:
parent
63cd4acf53
commit
9fdcddb317
@ -34,6 +34,7 @@ use boxed::Box;
|
||||
use rt::rtio::{IoFactory, LocalIo, RtioSocket, RtioTcpListener};
|
||||
use rt::rtio::{RtioTcpAcceptor, RtioTcpStream};
|
||||
use rt::rtio;
|
||||
use time::Duration;
|
||||
|
||||
/// A structure which represents a TCP stream between a local socket and a
|
||||
/// remote socket.
|
||||
@ -102,11 +103,11 @@ impl TcpStream {
|
||||
/// and port, similar to the API seen in `connect`.
|
||||
#[experimental = "the timeout argument may eventually change types"]
|
||||
pub fn connect_timeout(addr: SocketAddr,
|
||||
timeout_ms: u64) -> IoResult<TcpStream> {
|
||||
timeout: Duration) -> IoResult<TcpStream> {
|
||||
let SocketAddr { ip, port } = addr;
|
||||
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
|
||||
LocalIo::maybe_raise(|io| {
|
||||
io.tcp_connect(addr, Some(timeout_ms)).map(TcpStream::new)
|
||||
io.tcp_connect(addr, Some(in_ms_u64(timeout))).map(TcpStream::new)
|
||||
}).map_err(IoError::from_rtio_error)
|
||||
}
|
||||
|
||||
@ -443,6 +444,12 @@ impl Acceptor<TcpStream> for TcpAcceptor {
|
||||
}
|
||||
}
|
||||
|
||||
fn in_ms_u64(d: Duration) -> u64 {
|
||||
let ms = d.num_milliseconds();
|
||||
if ms < 0 { return 0 };
|
||||
return ms as u64;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(experimental)]
|
||||
mod test {
|
||||
|
@ -33,6 +33,7 @@ use kinds::Send;
|
||||
use boxed::Box;
|
||||
use rt::rtio::{IoFactory, LocalIo, RtioUnixListener};
|
||||
use rt::rtio::{RtioUnixAcceptor, RtioPipe};
|
||||
use time::Duration;
|
||||
|
||||
/// A stream which communicates over a named pipe.
|
||||
pub struct UnixStream {
|
||||
@ -68,9 +69,9 @@ impl UnixStream {
|
||||
/// elapses the function will return an error of kind `TimedOut`.
|
||||
#[experimental = "the timeout argument is likely to change types"]
|
||||
pub fn connect_timeout<P: ToCStr>(path: &P,
|
||||
timeout_ms: u64) -> IoResult<UnixStream> {
|
||||
timeout: Duration) -> IoResult<UnixStream> {
|
||||
LocalIo::maybe_raise(|io| {
|
||||
let s = io.unix_connect(&path.to_c_str(), Some(timeout_ms));
|
||||
let s = io.unix_connect(&path.to_c_str(), Some(in_ms_u64(timeout)));
|
||||
s.map(|p| UnixStream { obj: p })
|
||||
}).map_err(IoError::from_rtio_error)
|
||||
}
|
||||
@ -499,13 +500,13 @@ mod tests {
|
||||
|
||||
iotest!(fn connect_timeout_error() {
|
||||
let addr = next_test_unix();
|
||||
assert!(UnixStream::connect_timeout(&addr, 100).is_err());
|
||||
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_err());
|
||||
})
|
||||
|
||||
iotest!(fn connect_timeout_success() {
|
||||
let addr = next_test_unix();
|
||||
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
|
||||
assert!(UnixStream::connect_timeout(&addr, 100).is_ok());
|
||||
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_ok());
|
||||
})
|
||||
|
||||
iotest!(fn close_readwrite_smoke() {
|
||||
|
@ -72,12 +72,6 @@ pub struct Timer {
|
||||
|
||||
struct TimerCallback { tx: Sender<()> }
|
||||
|
||||
fn in_ms(d: Duration) -> u64 {
|
||||
let ms = d.num_milliseconds();
|
||||
if ms < 0 { return 0 };
|
||||
return ms as u64;
|
||||
}
|
||||
|
||||
/// Sleep the current task for the specified duration.
|
||||
///
|
||||
/// When provided a zero or negative `duration`, the function will
|
||||
@ -108,7 +102,7 @@ impl Timer {
|
||||
/// return immediately.
|
||||
pub fn sleep(&mut self, duration: Duration) {
|
||||
// Short-circuit the timer backend for 0 duration
|
||||
let ms = in_ms(duration);
|
||||
let ms = in_ms_u64(duration);
|
||||
if ms == 0 { return }
|
||||
self.obj.sleep(ms);
|
||||
}
|
||||
@ -153,8 +147,8 @@ impl Timer {
|
||||
pub fn oneshot(&mut self, duration: Duration) -> Receiver<()> {
|
||||
let (tx, rx) = channel();
|
||||
// Short-circuit the timer backend for 0 duration
|
||||
if in_ms(duration) != 0 {
|
||||
self.obj.oneshot(in_ms(duration), box TimerCallback { tx: tx });
|
||||
if in_ms_u64(duration) != 0 {
|
||||
self.obj.oneshot(in_ms_u64(duration), box TimerCallback { tx: tx });
|
||||
} else {
|
||||
tx.send(());
|
||||
}
|
||||
@ -207,7 +201,7 @@ impl Timer {
|
||||
/// When provided a zero or negative `duration`, the messages will
|
||||
/// be sent without delay.
|
||||
pub fn periodic(&mut self, duration: Duration) -> Receiver<()> {
|
||||
let ms = in_ms(duration);
|
||||
let ms = in_ms_u64(duration);
|
||||
// FIXME: The backend implementations don't ever send a message
|
||||
// if given a 0 ms duration. Temporarily using 1ms. It's
|
||||
// not clear what use a 0ms period is anyway...
|
||||
@ -224,6 +218,12 @@ impl Callback for TimerCallback {
|
||||
}
|
||||
}
|
||||
|
||||
fn in_ms_u64(d: Duration) -> u64 {
|
||||
let ms = d.num_milliseconds();
|
||||
if ms < 0 { return 0 };
|
||||
return ms as u64;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
iotest!(fn test_io_timer_sleep_simple() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user