├── .travis.yml ├── Cargo.toml ├── LICENSE ├── README.md └── src └── lib.rs /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | 3 | script: 4 | - cargo test 5 | - cargo build 6 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "select-rs" 3 | version = "0.1.2" 4 | authors = ["b23r0 "] 5 | license = "MIT" 6 | readme = "README.md" 7 | keywords = ["select", "async" , "fd_set" , "fd_isset" ,"fd_zero"] 8 | repository = "https://github.com/b23r0/select-rs" 9 | homepage = "https://crates.io/crates/select-rs" 10 | documentation = "https://docs.rs/crate/select-rs/0.1.0" 11 | description = """ 12 | A POSIX select I/O Multiplexing Rust library. 13 | """ 14 | 15 | [target.'cfg(not(target_os = "windows"))'.dependencies] 16 | libc = "0.2.107" 17 | 18 | [target.'cfg(target_os = "windows")'.dependencies] 19 | winapi = { version = "0.3.9", features = ["winsock2"] } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 b23r0 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # select-rs [![Build Status](https://app.travis-ci.com/b23r0/select-rs.svg?branch=main)](https://app.travis-ci.com/b23r0/select-rs) [![ChatOnDiscord](https://img.shields.io/badge/chat-on%20discord-blue)](https://discord.gg/ZKtYMvDFN4) [![Crate](https://img.shields.io/crates/v/select-rs)](https://crates.io/crates/select-rs) 2 | 3 | A POSIX select I/O Multiplexing Rust library. 4 | 5 | [select-rs]: https://github.com/b23r0/select-rs 6 | 7 | # Get started 8 | 9 | ```toml 10 | # Cargo.toml 11 | [dependencies] 12 | select-rs = "0.1.2" 13 | ``` 14 | 15 | # Example 16 | 17 | ```rust 18 | use select_rs::*; 19 | 20 | fn main(){ 21 | let mut fds : FdSet = unsafe {std::mem::zeroed()}; 22 | FD_ZERO(&mut fds); 23 | FD_SET(0 , &mut fds); 24 | assert!(select(1, std::ptr::null_mut() , &mut fds ,std::ptr::null_mut()) > 0); 25 | assert!(FD_ISSET(0, &mut fds)); 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![allow(non_snake_case)] 2 | 3 | #[cfg(target_os = "windows")] 4 | extern crate winapi; 5 | #[cfg(not(target_os = "windows"))] 6 | extern crate libc; 7 | 8 | #[cfg(target_os = "windows")] 9 | pub type FdSet = winapi::um::winsock2::fd_set; 10 | #[cfg(target_os = "windows")] 11 | pub type TimeVal = winapi::um::winsock2::timeval; 12 | #[cfg(not(target_os = "windows"))] 13 | pub type FdSet = libc::fd_set; 14 | #[cfg(not(target_os = "windows"))] 15 | pub type TimeVal = libc::timeval; 16 | 17 | #[cfg(not(target_os = "windows"))] 18 | pub fn FD_ISSET(fd: libc::c_int, set: *const libc::fd_set) -> bool { 19 | return unsafe { libc::FD_ISSET(fd, set) }; 20 | } 21 | 22 | #[cfg(not(target_os = "windows"))] 23 | pub fn FD_SET(fd: libc::c_int, set: *mut libc::fd_set) -> () { 24 | return unsafe { libc::FD_SET(fd, set) }; 25 | } 26 | 27 | #[cfg(not(target_os = "windows"))] 28 | pub fn FD_ZERO(set: *mut libc::fd_set) -> () { 29 | return unsafe { libc::FD_ZERO(set) }; 30 | } 31 | 32 | #[cfg(target_os = "windows")] 33 | pub fn FD_ISSET(a : u64 , set : &mut FdSet) -> bool{ 34 | let mut i = 0 ; 35 | while i < set.fd_count { 36 | 37 | if set.fd_array[i as usize] == a as usize { 38 | return true; 39 | } 40 | 41 | i += 1; 42 | } 43 | return false 44 | } 45 | 46 | #[cfg(target_os = "windows")] 47 | pub fn FD_SET(a : u64 , set : &mut FdSet){ 48 | use winapi::um::winsock2::FD_SETSIZE; 49 | 50 | let mut i = 0 ; 51 | while i < set.fd_count { 52 | 53 | if set.fd_array[i as usize] == a as usize { 54 | return 55 | } 56 | i += 1; 57 | } 58 | if (i as usize) < FD_SETSIZE { 59 | set.fd_array[i as usize] = a as usize; 60 | set.fd_count += 1; 61 | } 62 | } 63 | 64 | #[cfg(target_os = "windows")] 65 | pub fn FD_ZERO(set : &mut FdSet){ 66 | let mut i = 0 ; 67 | while i < set.fd_count { 68 | set.fd_array[i as usize] =0; 69 | i += 1; 70 | } 71 | set.fd_count = 0; 72 | } 73 | 74 | pub fn select( maxfd : i32, readfds: *mut FdSet, writefds: *mut FdSet,exceptfds: *mut FdSet , timeout: *mut TimeVal) -> i32 { 75 | 76 | #[cfg(target_os = "windows")] 77 | return unsafe { winapi::um::winsock2::select( 78 | maxfd , 79 | readfds , 80 | writefds, 81 | exceptfds , 82 | timeout) as i32 }; 83 | 84 | #[cfg(not(target_os = "windows"))] 85 | return unsafe { libc::select( 86 | maxfd , 87 | readfds , 88 | writefds, 89 | exceptfds , 90 | timeout) as i32 }; 91 | } 92 | 93 | #[test] 94 | #[cfg(not(target_os = "windows"))] 95 | fn test_select(){ 96 | let mut fds : FdSet = unsafe {std::mem::zeroed()}; 97 | FD_ZERO(&mut fds); 98 | FD_SET(0 , &mut fds); 99 | assert!(select(1, std::ptr::null_mut() , &mut fds ,std::ptr::null_mut() , std::ptr::null_mut()) > 0); 100 | assert!(FD_ISSET(0, &mut fds)); 101 | } --------------------------------------------------------------------------------