fakepoll.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright 2002,2003 Nick Mathewson, Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /* On systems where 'poll' doesn't exist, fake it with 'select'. */
  5. #include "orconfig.h"
  6. #include "fakepoll.h"
  7. #ifdef USE_FAKE_POLL
  8. #include <sys/types.h>
  9. #ifdef HAVE_UNISTD_H
  10. #include <unistd.h>
  11. #endif
  12. #ifdef HAVE_STRING_H
  13. #include <string.h>
  14. #endif
  15. #if _MSC_VER > 1300
  16. #include <winsock2.h>
  17. #include <ws2tcpip.h>
  18. #elif defined(_MSC_VER)
  19. #include <winsock.h>
  20. #endif
  21. /* by default, windows handles only 64 fd's */
  22. #if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
  23. #define FD_SETSIZE MAXCONNECTIONS
  24. #endif
  25. #include "util.h"
  26. int
  27. poll(struct pollfd *ufds, unsigned int nfds, int timeout)
  28. {
  29. int idx, maxfd, fd;
  30. int r;
  31. #ifdef MS_WINDOWS
  32. int any_fds_set = 0;
  33. #endif
  34. fd_set readfds, writefds, exceptfds;
  35. #ifdef USING_FAKE_TIMEVAL
  36. #undef timeval
  37. #undef tv_sec
  38. #undef tv_usec
  39. #endif
  40. struct timeval _timeout;
  41. _timeout.tv_sec = timeout/1000;
  42. _timeout.tv_usec = (timeout%1000)*1000;
  43. FD_ZERO(&readfds);
  44. FD_ZERO(&writefds);
  45. FD_ZERO(&exceptfds);
  46. maxfd = -1;
  47. for (idx = 0; idx < nfds; ++idx) {
  48. ufds[idx].revents = 0;
  49. fd = ufds[idx].fd;
  50. if (fd > maxfd) {
  51. maxfd = fd;
  52. #ifdef MS_WINDOWS
  53. any_fds_set = 1;
  54. #endif
  55. }
  56. if (ufds[idx].events & POLLIN)
  57. FD_SET(fd, &readfds);
  58. if (ufds[idx].events & POLLOUT)
  59. FD_SET(fd, &writefds);
  60. FD_SET(fd, &exceptfds);
  61. }
  62. #ifdef MS_WINDOWS
  63. if (!any_fds_set) {
  64. Sleep(timeout);
  65. return 0;
  66. }
  67. #endif
  68. r = select(maxfd+1, &readfds, &writefds, &exceptfds,
  69. timeout == -1 ? NULL : &_timeout);
  70. if (r <= 0)
  71. return r;
  72. r = 0;
  73. for (idx = 0; idx < nfds; ++idx) {
  74. fd = ufds[idx].fd;
  75. if (FD_ISSET(fd, &readfds))
  76. ufds[idx].revents |= POLLIN;
  77. if (FD_ISSET(fd, &writefds))
  78. ufds[idx].revents |= POLLOUT;
  79. if (FD_ISSET(fd, &exceptfds))
  80. ufds[idx].revents |= POLLERR;
  81. if (ufds[idx].revents)
  82. ++r;
  83. }
  84. return r;
  85. }
  86. #endif