| 54 | #define WAITFD_C (WAITFD_E|WAITFD_W) |
| 55 | |
| 56 | int socket_waitfd(p_socket ps, int sw, p_timeout tm) { |
| 57 | int ret; |
| 58 | fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; |
| 59 | struct timeval tv, *tp = NULL; |
| 60 | double t; |
| 61 | if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ |
| 62 | if (sw & WAITFD_R) { |
| 63 | FD_ZERO(&rfds); |
| 64 | FD_SET(*ps, &rfds); |
| 65 | rp = &rfds; |
| 66 | } |
| 67 | if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } |
| 68 | if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; } |
| 69 | if ((t = timeout_get(tm)) >= 0.0) { |
| 70 | tv.tv_sec = (int) t; |
| 71 | tv.tv_usec = (int) ((t-tv.tv_sec)*1.0e6); |
| 72 | tp = &tv; |
| 73 | } |
| 74 | ret = select(0, rp, wp, ep, tp); |
| 75 | if (ret == -1) return WSAGetLastError(); |
| 76 | if (ret == 0) return IO_TIMEOUT; |
| 77 | if (sw == WAITFD_C && FD_ISSET(*ps, &efds)) return IO_CLOSED; |
| 78 | return IO_DONE; |
| 79 | } |
| 80 | |
| 81 | /*-------------------------------------------------------------------------*\ |
| 82 | * Select with int timeout in ms |
no test coverage detected