2002-09-03 20:43:50 +02:00
|
|
|
/*
|
|
|
|
* fakepoll.c
|
|
|
|
*
|
|
|
|
* On systems where 'poll' doesn't exist, fake it with 'select'.
|
|
|
|
*
|
|
|
|
* Nick Mathewson <nickm@freehaven.net>
|
|
|
|
*/
|
|
|
|
|
2003-08-12 05:08:41 +02:00
|
|
|
#include "orconfig.h"
|
2003-08-12 08:41:40 +02:00
|
|
|
#include "fakepoll.h"
|
2003-08-14 19:13:52 +02:00
|
|
|
|
2002-09-03 20:43:50 +02:00
|
|
|
#ifdef USE_FAKE_POLL
|
|
|
|
#include <sys/types.h>
|
2003-08-12 05:08:41 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2002-09-03 20:43:50 +02:00
|
|
|
#include <unistd.h>
|
2003-08-12 05:08:41 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2002-09-03 20:43:50 +02:00
|
|
|
#include <string.h>
|
2003-08-12 05:08:41 +02:00
|
|
|
#endif
|
|
|
|
#if _MSC_VER > 1300
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#include <winsock.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "util.h"
|
2002-09-03 20:43:50 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
poll(struct pollfd *ufds, unsigned int nfds, int timeout)
|
|
|
|
{
|
2003-08-12 10:18:13 +02:00
|
|
|
unsigned int idx, maxfd, fd;
|
|
|
|
int r;
|
2003-08-14 19:13:52 +02:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
int any_fds_set = 0;
|
|
|
|
#endif
|
2002-09-03 20:43:50 +02:00
|
|
|
fd_set readfds, writefds, exceptfds;
|
2003-08-12 10:18:13 +02:00
|
|
|
#ifdef USING_FAKE_TIMEVAL
|
|
|
|
#undef timeval
|
|
|
|
#undef tv_sec
|
|
|
|
#undef tv_usec
|
|
|
|
#endif
|
2002-09-03 20:43:50 +02:00
|
|
|
struct timeval _timeout;
|
|
|
|
_timeout.tv_sec = timeout/1000;
|
|
|
|
_timeout.tv_usec = (timeout%1000)*1000;
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_ZERO(&exceptfds);
|
|
|
|
|
|
|
|
maxfd = -1;
|
|
|
|
for (idx = 0; idx < nfds; ++idx) {
|
|
|
|
fd = ufds[idx].fd;
|
2003-08-14 19:13:52 +02:00
|
|
|
if (ufds[idx].events) {
|
|
|
|
if (fd > maxfd)
|
|
|
|
maxfd = fd;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
any_fds_set = 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (ufds[idx].events & POLLIN)
|
2002-09-03 20:43:50 +02:00
|
|
|
FD_SET(fd, &readfds);
|
|
|
|
if (ufds[idx].events & POLLOUT)
|
|
|
|
FD_SET(fd, &writefds);
|
2003-08-14 19:13:52 +02:00
|
|
|
if (ufds[idx].events & POLLERR)
|
2002-09-03 20:43:50 +02:00
|
|
|
FD_SET(fd, &exceptfds);
|
|
|
|
}
|
2003-08-14 19:13:52 +02:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (!any_fds_set) {
|
2003-08-14 19:51:36 +02:00
|
|
|
Sleep(timeout);
|
2003-08-14 19:13:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2002-09-03 20:43:50 +02:00
|
|
|
r = select(maxfd+1, &readfds, &writefds, &exceptfds,
|
|
|
|
timeout == -1 ? NULL : &_timeout);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
|
|
|
r = 0;
|
|
|
|
for (idx = 0; idx < nfds; ++idx) {
|
|
|
|
fd = ufds[idx].fd;
|
|
|
|
ufds[idx].revents = 0;
|
|
|
|
if (FD_ISSET(fd, &readfds))
|
|
|
|
ufds[idx].revents |= POLLIN;
|
|
|
|
if (FD_ISSET(fd, &writefds))
|
|
|
|
ufds[idx].revents |= POLLOUT;
|
|
|
|
if (FD_ISSET(fd, &exceptfds))
|
|
|
|
ufds[idx].revents |= POLLERR;
|
|
|
|
if (ufds[idx].revents)
|
|
|
|
++r;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
#endif
|