diff options
author | Nick Mathewson <nickm@torproject.org> | 2002-09-03 18:43:50 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2002-09-03 18:43:50 +0000 |
commit | 8878d8cc27d94839053063aa8925d6f2620f4c20 (patch) | |
tree | 9057be3492bb9cd7e3367e11e20eb6eabe685e03 | |
parent | 27adc0f20bd0d5f0e5b90a695394518072316d8d (diff) | |
download | tor-8878d8cc27d94839053063aa8925d6f2620f4c20.tar.gz tor-8878d8cc27d94839053063aa8925d6f2620f4c20.zip |
Add function to fake a poll call using select
svn:r87
-rw-r--r-- | src/common/fakepoll.c | 69 | ||||
-rw-r--r-- | src/common/fakepoll.h | 43 |
2 files changed, 112 insertions, 0 deletions
diff --git a/src/common/fakepoll.c b/src/common/fakepoll.c new file mode 100644 index 0000000000..168d1e9660 --- /dev/null +++ b/src/common/fakepoll.c @@ -0,0 +1,69 @@ +/* + * fakepoll.c + * + * On systems where 'poll' doesn't exist, fake it with 'select'. + * + * Nick Mathewson <nickm@freehaven.net> + */ + + +/* + * Changes : + * $Log$ + * Revision 1.1 2002/09/03 18:43:50 nickm + * Add function to fake a poll call using select + * + */ +#include "fakepoll.h" + +#ifdef USE_FAKE_POLL +#include <sys/time.h> +#include <sys/types.h> +#include <unistd.h> +#include <string.h> + +int +poll(struct pollfd *ufds, unsigned int nfds, int timeout) +{ + int idx, maxfd, fd, r; + fd_set readfds, writefds, exceptfds; + 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; + if (fd > maxfd && ufds[idx].events) + maxfd = fd; + if (ufds[idx].events & (POLLIN)) + FD_SET(fd, &readfds); + if (ufds[idx].events & POLLOUT) + FD_SET(fd, &writefds); + if (ufds[idx].events & (POLLERR)) + FD_SET(fd, &exceptfds); + } + 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 diff --git a/src/common/fakepoll.h b/src/common/fakepoll.h new file mode 100644 index 0000000000..533ee3f984 --- /dev/null +++ b/src/common/fakepoll.h @@ -0,0 +1,43 @@ +/* + * fakepoll.h + * + * On systems where 'poll' doesn't exist, fake it with 'select'. + * + * Nick Mathewson <nickm@freehaven.net> + */ + +/* + * Changes : + * $Log$ + * Revision 1.1 2002/09/03 18:43:50 nickm + * Add function to fake a poll call using select + * + */ +#ifndef __FAKEPOLL_H +#define __FAKEPOLL_H + +#include "orconfig.h" +#undef VERSION + +#ifndef HAVE_POLL_H +#ifndef HAVE_SYS_POLL_H +#define USE_FAKE_POLL + +struct pollfd { + int fd; + short events; + short revents; +}; + +#define POLLIN 0x0001 +#define POLLPRI 0x0002 +#define POLLOUT 0x0004 +#define POLLERR 0x0008 +#define POLLHUP 0x0010 +#define POLLNVAL 0x0020 + +int poll(struct pollfd *ufds, unsigned int nfds, int timeout); + +#endif +#endif +#endif |