diff --git a/include/ruby/win32.h b/include/ruby/win32.h index 1f96648..757d2f1 100644 --- a/include/ruby/win32.h +++ b/include/ruby/win32.h @@ -581,7 +581,22 @@ extern char *rb_w32_strerror(int); #define O_NONBLOCK 1 #undef FD_SET -#define FD_SET(f, s) rb_w32_fdset(f, s) +#define FD_SET(fd, set) do {\ + unsigned int i;\ + SOCKET s = _get_osfhandle(fd);\ +\ + for (i = 0; i < (set)->fd_count; i++) {\ + if ((set)->fd_array[i] == s) {\ + break;\ + }\ + }\ + if (i == (set)->fd_count) {\ + if ((set)->fd_count < FD_SETSIZE) {\ + (set)->fd_array[i] = s;\ + (set)->fd_count++;\ + }\ + }\ +} while(0) #undef FD_CLR #define FD_CLR(f, s) rb_w32_fdclr(f, s) diff --git a/win32/win32.c b/win32/win32.c index 5038784..5ee4a72 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -2452,25 +2452,10 @@ ioctl(int i, int u, ...) return -1; } -#undef FD_SET - void rb_w32_fdset(int fd, fd_set *set) { - unsigned int i; - SOCKET s = TO_SOCKET(fd); - - for (i = 0; i < set->fd_count; i++) { - if (set->fd_array[i] == s) { - return; - } - } - if (i == set->fd_count) { - if (set->fd_count < FD_SETSIZE) { - set->fd_array[i] = s; - set->fd_count++; - } - } + FD_SET(fd, set); } #undef FD_CLR @@ -2598,7 +2583,8 @@ copy_fd(fd_set *dst, fd_set *src) if (dst->fd_array[d] == fd) break; } - if (d == dst->fd_count && d < FD_SETSIZE) { + if (d == dst->fd_count && + d < (sizeof(dst->fd_array) / sizeof(dst->fd_array[0]))) { dst->fd_array[dst->fd_count++] = fd; } } @@ -2868,14 +2854,19 @@ rb_w32_select_with_thread(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, fd_set orig_rd; fd_set orig_wr; fd_set orig_ex; - if (rd) orig_rd = *rd; - if (wr) orig_wr = *wr; - if (ex) orig_ex = *ex; + + FD_ZERO(&orig_rd); + FD_ZERO(&orig_wr); + FD_ZERO(&orig_ex); + + if (rd) copy_fd(&orig_rd, rd); + if (wr) copy_fd(&orig_wr, wr); + if (ex) copy_fd(&orig_ex, ex); r = do_select(nfds, rd, wr, ex, &zero); // polling if (r != 0) break; // signaled or error - if (rd) *rd = orig_rd; - if (wr) *wr = orig_wr; - if (ex) *ex = orig_ex; + if (rd) copy_fd(rd, &orig_rd); + if (wr) copy_fd(wr, &orig_wr); + if (ex) copy_fd(ex, &orig_ex); if (timeout) { struct timeval now;