1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use super::AsRawFd;

use core::{ptr, time};
use std::io;
use std::os::windows::io::RawSocket;
use std::os::raw::{c_int, c_uint, c_long};

const SOCKET_ERROR: c_int = -1;
pub type RawFd = RawSocket;
///Limit on file descriptors in select's set
pub const FD_LIMIT: usize = 64;

impl<T: std::os::windows::io::AsRawSocket> AsRawFd for T {
    #[inline(always)]
    fn as_raw_fd(&self) -> RawFd {
        self.as_raw_socket()
    }
}

mod winsock {
    use super::*;

    extern "system" {
        pub fn select(nfds: c_int, readfds: *mut FdSet, writefds: *mut FdSet, exceptfds: *mut FdSet, timeout: *const TimeVal) -> c_int;
    }
}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct TimeVal {
    tv_sec: c_long,
    tv_usec: c_long,
}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct FdSet {
    count: c_uint,
    fds: [RawSocket; 64],
}

impl FdSet {
    #[inline]
    pub fn new() -> Self {
        Self {
            count: 0,
            fds: unsafe {
                core::mem::MaybeUninit::zeroed().assume_init()
            }
        }
    }

    #[inline]
    pub fn add<T: AsRawFd>(&mut self, source: &T) {
        let idx = self.count as usize;
        self.fds[idx] = source.as_raw_fd();
        self.count += 1;
    }

    #[inline]
    pub fn clear(&mut self) {
        for fd in self.fds[..self.count as usize].iter_mut() {
            *fd = 0;
        }
        self.count = 0;
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.count as usize
    }

    #[inline]
    pub fn is_present<T: AsRawFd>(&self, source: &T) -> bool {
        let expected = source.as_raw_fd();
        for fd in self.fds[..self.count as usize].iter() {
            if *fd == expected {
                return true;
            }
        }

        false
    }
}

pub fn select(read: &mut FdSet, write: &mut FdSet, except: &mut FdSet) -> io::Result<usize> {
    let read_fd = match read.len() {
        0 => ptr::null_mut(),
        _ => read as *mut _ as *mut _,
    };

    let write_fd = match write.len() {
        0 => ptr::null_mut(),
        _ => write as *mut _ as *mut _,
    };

    let except_fd = match except.len() {
        0 => ptr::null_mut(),
        _ => except as *mut _ as *mut _
    };

    let result = unsafe {
        winsock::select(0, read_fd, write_fd, except_fd, ptr::null_mut())
    };

    match result {
        SOCKET_ERROR => Err(io::Error::last_os_error()),
        result => Ok(result as usize)
    }
}

pub fn select_timeout(read: &mut FdSet, write: &mut FdSet, except: &mut FdSet, timeout: time::Duration) -> io::Result<usize> {
    use core::convert::TryInto;

    let timeout = TimeVal {
        tv_sec: match timeout.as_secs().try_into() {
            Ok(secs) => secs,
            Err(_) => return Err(io::Error::new(io::ErrorKind::InvalidInput, "Duration as seconds doesn't fit timeval")),
        },
        tv_usec: timeout.subsec_micros() as _,
    };

    let read_fd = match read.len() {
        0 => ptr::null_mut(),
        _ => read as *mut _ as *mut _,
    };

    let write_fd = match write.len() {
        0 => ptr::null_mut(),
        _ => write as *mut _ as *mut _,
    };

    let except_fd = match except.len() {
        0 => ptr::null_mut(),
        _ => except as *mut _ as *mut _
    };

    let result = unsafe {
        winsock::select(0, read_fd, write_fd, except_fd, &timeout)
    };

    if result == SOCKET_ERROR {
        Err(io::Error::last_os_error())
    } else {
        Ok(result as usize)
    }
}