forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std: Add an experimental connect_timeout function
This adds a `TcpStream::connect_timeout` function in order to assist opening connections with a timeout (cc rust-lang#13523). There isn't really much design space for this specific operation (unlike timing out normal blocking reads/writes), so I am fairly confident that this is the correct interface for this function. The function is marked #[experimental] because it takes a u64 timeout argument, and the u64 type is likely to change in the future.
- Loading branch information
1 parent
9d5082e
commit 3915e17
Showing
15 changed files
with
490 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2014 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. | ||
|
||
//! C definitions used by libnative that don't belong in liblibc | ||
pub use self::select::fd_set; | ||
|
||
use libc; | ||
|
||
#[cfg(target_os = "macos")] | ||
#[cfg(target_os = "freebsd")] | ||
pub static FIONBIO: libc::c_ulong = 0x8004667e; | ||
#[cfg(target_os = "linux")] | ||
#[cfg(target_os = "android")] | ||
pub static FIONBIO: libc::c_ulong = 0x5421; | ||
#[cfg(target_os = "macos")] | ||
#[cfg(target_os = "freebsd")] | ||
pub static FIOCLEX: libc::c_ulong = 0x20006601; | ||
#[cfg(target_os = "linux")] | ||
#[cfg(target_os = "android")] | ||
pub static FIOCLEX: libc::c_ulong = 0x5451; | ||
|
||
extern { | ||
pub fn gettimeofday(timeval: *mut libc::timeval, | ||
tzp: *libc::c_void) -> libc::c_int; | ||
pub fn select(nfds: libc::c_int, | ||
readfds: *fd_set, | ||
writefds: *fd_set, | ||
errorfds: *fd_set, | ||
timeout: *libc::timeval) -> libc::c_int; | ||
pub fn getsockopt(sockfd: libc::c_int, | ||
level: libc::c_int, | ||
optname: libc::c_int, | ||
optval: *mut libc::c_void, | ||
optlen: *mut libc::socklen_t) -> libc::c_int; | ||
pub fn ioctl(fd: libc::c_int, req: libc::c_ulong, ...) -> libc::c_int; | ||
|
||
} | ||
|
||
#[cfg(target_os = "macos")] | ||
mod select { | ||
pub static FD_SETSIZE: uint = 1024; | ||
|
||
pub struct fd_set { | ||
fds_bits: [i32, ..(FD_SETSIZE / 32)] | ||
} | ||
|
||
pub fn fd_set(set: &mut fd_set, fd: i32) { | ||
set.fds_bits[(fd / 32) as uint] |= 1 << (fd % 32); | ||
} | ||
} | ||
|
||
#[cfg(target_os = "android")] | ||
#[cfg(target_os = "freebsd")] | ||
#[cfg(target_os = "linux")] | ||
mod select { | ||
use std::uint; | ||
|
||
pub static FD_SETSIZE: uint = 1024; | ||
|
||
pub struct fd_set { | ||
fds_bits: [uint, ..(FD_SETSIZE / uint::BITS)] | ||
} | ||
|
||
pub fn fd_set(set: &mut fd_set, fd: i32) { | ||
let fd = fd as uint; | ||
set.fds_bits[fd / uint::BITS] |= 1 << (fd % uint::BITS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2014 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. | ||
|
||
//! C definitions used by libnative that don't belong in liblibc | ||
#![allow(type_overflow)] | ||
|
||
use libc; | ||
|
||
pub static WSADESCRIPTION_LEN: uint = 256; | ||
pub static WSASYS_STATUS_LEN: uint = 128; | ||
pub static FIONBIO: libc::c_long = 0x8004667e; | ||
static FD_SETSIZE: uint = 64; | ||
|
||
pub struct WSADATA { | ||
pub wVersion: libc::WORD, | ||
pub wHighVersion: libc::WORD, | ||
pub szDescription: [u8, ..WSADESCRIPTION_LEN + 1], | ||
pub szSystemStatus: [u8, ..WSASYS_STATUS_LEN + 1], | ||
pub iMaxSockets: u16, | ||
pub iMaxUdpDg: u16, | ||
pub lpVendorInfo: *u8, | ||
} | ||
|
||
pub type LPWSADATA = *mut WSADATA; | ||
|
||
pub struct fd_set { | ||
fd_count: libc::c_uint, | ||
fd_array: [libc::SOCKET, ..FD_SETSIZE], | ||
} | ||
|
||
pub fn fd_set(set: &mut fd_set, s: libc::SOCKET) { | ||
set.fd_array[set.fd_count as uint] = s; | ||
set.fd_count += 1; | ||
} | ||
|
||
#[link(name = "ws2_32")] | ||
extern "system" { | ||
pub fn WSAStartup(wVersionRequested: libc::WORD, | ||
lpWSAData: LPWSADATA) -> libc::c_int; | ||
pub fn WSAGetLastError() -> libc::c_int; | ||
|
||
pub fn ioctlsocket(s: libc::SOCKET, cmd: libc::c_long, | ||
argp: *mut libc::c_ulong) -> libc::c_int; | ||
pub fn select(nfds: libc::c_int, | ||
readfds: *mut fd_set, | ||
writefds: *mut fd_set, | ||
exceptfds: *mut fd_set, | ||
timeout: *libc::timeval) -> libc::c_int; | ||
pub fn getsockopt(sockfd: libc::SOCKET, | ||
level: libc::c_int, | ||
optname: libc::c_int, | ||
optval: *mut libc::c_char, | ||
optlen: *mut libc::c_int) -> libc::c_int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
3915e17
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r=brson