fix includes
[m6w6/libmemcached] / src / libmemcached / poll.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/common.h"
17
18 #if defined(_WIN32)
19 # include "libmemcached/poll.h"
20 # if HAVE_SYS_TIME_H
21 # include <sys/time.h>
22 # endif
23 # include <time.h>
24 # if HAVE_STRINGS_H
25 # include <cstrings>
26 # endif
27
28 int poll(struct pollfd fds[], nfds_t nfds, int tmo) {
29 fd_set readfds, writefds, errorfds;
30 FD_ZERO(&readfds);
31 FD_ZERO(&writefds);
32 FD_ZERO(&errorfds);
33
34 int maxfd = 0;
35
36 for (nfds_t x = 0; x < nfds; ++x) {
37 if (fds[x].events & (POLLIN | POLLOUT)) {
38 # ifndef _WIN32
39 if (fds[x].fd > maxfd) {
40 maxfd = fds[x].fd;
41 }
42 # endif
43 if (fds[x].events & POLLIN) {
44 FD_SET(fds[x].fd, &readfds);
45 }
46 if (fds[x].events & POLLOUT) {
47 FD_SET(fds[x].fd, &writefds);
48 }
49 }
50 }
51
52 struct timeval timeout = {tmo / 1000, (tmo % 1000) * 1000};
53 struct timeval *tp = &timeout;
54 if (tmo == -1) {
55 tp = NULL;
56 }
57 int ret = select(maxfd + 1, &readfds, &writefds, &errorfds, tp);
58 if (ret <= 0) {
59 return ret;
60 }
61
62 /* Iterate through all of them because I need to clear the revent map */
63 for (nfds_t x = 0; x < nfds; ++x) {
64 fds[x].revents = 0;
65 if (FD_ISSET(fds[x].fd, &readfds)) {
66 fds[x].revents |= POLLIN;
67 }
68 if (FD_ISSET(fds[x].fd, &writefds)) {
69 fds[x].revents |= POLLOUT;
70 }
71 if (FD_ISSET(fds[x].fd, &errorfds)) {
72 fds[x].revents |= POLLERR;
73 }
74 }
75
76 return ret;
77 }
78
79 #endif // defined(_WIN32)