33 ULONGLONG ms_total, limit;
34 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
35 int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
37 fd_set sock_read, sock_write, sock_except;
38 fd_set aread, awrite, aexcept;
40 struct timeval tvslice;
48#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
54 ms_total =
tv->tv_sec * 1000;
55 ms_total +=
tv->tv_usec / 1000;
60 FD_ZERO(&sock_except);
63 for (i = 0; (uint32_t)i < max_fd; i++) {
65 handles[n_handles] = (HANDLE)(uintptr_t)_get_osfhandle(i);
66 if (handles[n_handles] == INVALID_HANDLE_VALUE) {
69 FD_SET((uint32_t)i, &sock_read);
72 FD_SET((uint32_t)i, &sock_write);
75 FD_SET((uint32_t)i, &sock_except);
77 if (i > sock_max_fd) {
81 handle_slot_to_fd[n_handles] = i;
89 return select(-1, rfds, wfds, efds,
tv);
99 limit = GetTickCount64() + ms_total;
103 if (sock_max_fd >= 0) {
108 aexcept = sock_except;
111 tvslice.tv_usec = 100000;
113 retcode = select(-1, &aread, &awrite, &aexcept, &tvslice);
119 wret = WaitForMultipleObjects(n_handles, handles,
FALSE, retcode > 0 ? 0 : 100);
121 if (wret == WAIT_TIMEOUT) {
127 }
else if (wret == WAIT_FAILED) {
135 for (i = 0; i < n_handles; i++) {
136 if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
138 FD_SET((uint32_t)handle_slot_to_fd[i], &aread);
141 FD_SET((uint32_t)handle_slot_to_fd[i], &awrite);
144 FD_SET((uint32_t)handle_slot_to_fd[i], &aexcept);
151 }
while (retcode == 0 && (ms_total == INFINITE || GetTickCount64() < limit));