fakepoll.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright 2002,2003 Nick Mathewson, Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file fakepoll.c
  6. *
  7. * \brief On systems where poll() doesn't exist, fake it with select().
  8. **/
  9. #include "orconfig.h"
  10. #include "fakepoll.h"
  11. #define MAXCONNECTIONS 10000 /* XXXX copied from or.h */
  12. #define FD_SETSIZE MAXCONNECTIONS
  13. #ifdef HAVE_SYS_TYPES_H
  14. #include <sys/types.h>
  15. #endif
  16. #ifdef HAVE_UNISTD_H
  17. #include <unistd.h>
  18. #endif
  19. #ifdef HAVE_STRING_H
  20. #include <string.h>
  21. #endif
  22. #ifdef HAVE_SYS_TIME_H
  23. #include <sys/time.h>
  24. #endif
  25. #if _MSC_VER > 1300
  26. #include <winsock2.h>
  27. #include <ws2tcpip.h>
  28. #elif defined(_MSC_VER)
  29. #include <winsock.h>
  30. #endif
  31. #include <assert.h>
  32. #include <stdlib.h>
  33. #include "util.h"
  34. #include "log.h"
  35. #ifndef USE_FAKE_POLL
  36. int
  37. tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout)
  38. {
  39. int i;
  40. for (i=0;i<nfds;++i) {
  41. tor_assert(ufds[i].fd >= 0);
  42. }
  43. return poll(ufds,nfds,timeout);
  44. }
  45. #else
  46. int
  47. tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout)
  48. {
  49. unsigned int idx;
  50. int maxfd, fd;
  51. int r;
  52. #ifdef MS_WINDOWS
  53. int any_fds_set = 0;
  54. #endif
  55. fd_set readfds, writefds, exceptfds;
  56. #ifdef USING_FAKE_TIMEVAL
  57. #undef timeval
  58. #undef tv_sec
  59. #undef tv_usec
  60. #endif
  61. struct timeval _timeout;
  62. _timeout.tv_sec = timeout/1000;
  63. _timeout.tv_usec = (timeout%1000)*1000;
  64. FD_ZERO(&readfds);
  65. FD_ZERO(&writefds);
  66. FD_ZERO(&exceptfds);
  67. maxfd = -1;
  68. for (idx = 0; idx < nfds; ++idx) {
  69. ufds[idx].revents = 0;
  70. fd = ufds[idx].fd;
  71. tor_assert(fd >= 0);
  72. if (fd > maxfd) {
  73. maxfd = fd;
  74. #ifdef MS_WINDOWS
  75. any_fds_set = 1;
  76. #endif
  77. }
  78. if (ufds[idx].events & POLLIN)
  79. FD_SET(fd, &readfds);
  80. if (ufds[idx].events & POLLOUT)
  81. FD_SET(fd, &writefds);
  82. FD_SET(fd, &exceptfds);
  83. }
  84. #ifdef MS_WINDOWS
  85. if (!any_fds_set) {
  86. Sleep(timeout);
  87. return 0;
  88. }
  89. #endif
  90. r = select(maxfd+1, &readfds, &writefds, &exceptfds,
  91. timeout == -1 ? NULL : &_timeout);
  92. if (r <= 0)
  93. return r;
  94. r = 0;
  95. for (idx = 0; idx < nfds; ++idx) {
  96. fd = ufds[idx].fd;
  97. if (FD_ISSET(fd, &readfds))
  98. ufds[idx].revents |= POLLIN;
  99. if (FD_ISSET(fd, &writefds))
  100. ufds[idx].revents |= POLLOUT;
  101. if (FD_ISSET(fd, &exceptfds))
  102. ufds[idx].revents |= POLLERR;
  103. if (ufds[idx].revents)
  104. ++r;
  105. }
  106. return r;
  107. }
  108. #endif
  109. /*
  110. Local Variables:
  111. mode:c
  112. indent-tabs-mode:nil
  113. c-basic-offset:2
  114. End:
  115. */