Fix server messages (clean up errors in general).
[m6w6/libmemcached] / poll / poll.c
1 /* LibMemcached
2 * Copyright (C) 2010 Brian Aker, Trond Norbye
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Implementation of poll by using select
9 *
10 */
11 #include "config.h"
12 #include <sys/time.h>
13 #include <strings.h>
14
15 int poll(struct pollfd fds[], nfds_t nfds, int tmo)
16 {
17 fd_set readfds, writefds, errorfds;
18 FD_ZERO(&readfds);
19 FD_ZERO(&writefds);
20 FD_ZERO(&errorfds);
21
22 int maxfd= 0;
23
24 for (nfds_t x= 0; x < nfds; ++x)
25 {
26 if (fds[x].events & (POLLIN | POLLOUT))
27 {
28 #ifndef WIN32
29 if (fds[x].fd > maxfd)
30 {
31 maxfd= fds[x].fd;
32 }
33 #endif
34 if (fds[x].events & POLLIN)
35 {
36 FD_SET(fds[x].fd, &readfds);
37 }
38 if (fds[x].events & POLLOUT)
39 {
40 FD_SET(fds[x].fd, &writefds);
41 }
42 }
43 }
44
45 struct timeval timeout= { .tv_sec = tmo / 1000,
46 .tv_usec= (tmo % 1000) * 1000 };
47 struct timeval *tp= &timeout;
48 if (tmo == -1)
49 {
50 tp= NULL;
51 }
52 int ret= select(maxfd + 1, &readfds, &writefds, &errorfds, tp);
53 if (ret <= 0)
54 {
55 return ret;
56 }
57
58 /* Iterate through all of them because I need to clear the revent map */
59 for (nfds_t x= 0; x < nfds; ++x)
60 {
61 fds[x].revents= 0;
62 if (FD_ISSET(fds[x].fd, &readfds))
63 {
64 fds[x].revents |= POLLIN;
65 }
66 if (FD_ISSET(fds[x].fd, &writefds))
67 {
68 fds[x].revents |= POLLOUT;
69 }
70 if (FD_ISSET(fds[x].fd, &errorfds))
71 {
72 fds[x].revents |= POLLERR;
73 }
74 }
75
76 return ret;
77 }