diff options
| author | Steven Fackler <[email protected]> | 2015-11-16 21:11:00 -0800 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2015-11-16 21:11:00 -0800 |
| commit | 094e8e5b3e6eef13b03fd8c5b67b4aaf81af5d5c (patch) | |
| tree | 21e7a32e45992656ddefbf95c3179eb0f914b865 /openssl/src/ssl/tests/select.rs | |
| parent | Merge branch 'release-v0.6.7' into release (diff) | |
| parent | Release v0.7.0 (diff) | |
| download | rust-openssl-0.7.0.tar.xz rust-openssl-0.7.0.zip | |
Merge branch 'release-v0.7.0' into releasev0.7.0
Diffstat (limited to 'openssl/src/ssl/tests/select.rs')
| -rw-r--r-- | openssl/src/ssl/tests/select.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/openssl/src/ssl/tests/select.rs b/openssl/src/ssl/tests/select.rs new file mode 100644 index 00000000..abdf9339 --- /dev/null +++ b/openssl/src/ssl/tests/select.rs @@ -0,0 +1,72 @@ +use libc; +pub use self::imp::*; + +#[cfg(unix)] +mod imp { + use std::os::unix::prelude::*; + use std::io; + use libc; + + pub use libc::fd_set; + + pub fn fd_set<F: AsRawFd>(set: &mut fd_set, f: &F) { + unsafe { + libc::FD_SET(f.as_raw_fd(), set); + } + } + + pub unsafe fn select<F: AsRawFd>(max: &F, + read: *mut fd_set, + write: *mut fd_set, + error: *mut fd_set, + timeout_ms: u32) + -> io::Result<bool> { + let mut timeout = libc::timeval { + tv_sec: (timeout_ms / 1000) as libc::time_t, + tv_usec: (timeout_ms % 1000 * 1000) as libc::suseconds_t, + }; + let rc = libc::select(max.as_raw_fd() + 1, read, write, error, &mut timeout); + if rc < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(rc != 0) + } + } +} + +#[cfg(windows)] +mod imp { + extern crate winapi; + extern crate ws2_32; + + use std::os::windows::prelude::*; + use std::io; + use libc::{c_uint, c_long}; + use self::winapi::SOCKET; + use self::winapi::winsock2; + + pub use self::winapi::winsock2::fd_set; + + pub fn fd_set<F: AsRawSocket>(set: &mut fd_set, f: &F) { + set.fd_array[set.fd_count as usize] = f.as_raw_socket(); + set.fd_count += 1; + } + + pub unsafe fn select<F: AsRawSocket>(_max: &F, + read: *mut fd_set, + write: *mut fd_set, + error: *mut fd_set, + timeout_ms: u32) + -> io::Result<bool> { + let mut timeout = winsock2::timeval { + tv_sec: (timeout_ms / 1000) as c_long, + tv_usec: (timeout_ms % 1000 * 1000) as c_long, + }; + let rc = ws2_32::select(1, read, write, error, &mut timeout); + if rc < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(rc != 0) + } + } +} |