fakepoll.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * fakepoll.c
  3. *
  4. * On systems where 'poll' doesn't exist, fake it with 'select'.
  5. *
  6. * Copyright (c) 2001-2003, Nick Mathewson <nickm@freehaven.net>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the names of the copyright owners nor the names of its
  21. * contributors may be used to endorse or promote products derived from
  22. * this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "config.h"
  37. #include "defines.h"
  38. #include "common.h"
  39. /* prevents ISO C error */
  40. static void FAKEPOLL(int stop)
  41. {
  42. if (! stop)
  43. FAKEPOLL(1);
  44. return;
  45. }
  46. #ifdef USE_FAKE_POLL
  47. #include <sys/types.h>
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #ifdef HAVE_STRING_H
  52. #include <string.h>
  53. #endif
  54. #if _MSC_VER > 1300
  55. #include <winsock2.h>
  56. #include <ws2tcpip.h>
  57. #elif defined(_MSC_VER)
  58. #include <winsock.h>
  59. #endif
  60. /* by default, windows handles only 64 fd's */
  61. #if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
  62. #define FD_SETSIZE MAXCONNECTIONS
  63. #endif
  64. #include "util.h"
  65. int
  66. poll(struct pollfd *ufds, unsigned int nfds, int timeout)
  67. {
  68. int idx, maxfd, fd;
  69. int r;
  70. #ifdef MS_WINDOWS
  71. int any_fds_set = 0;
  72. #endif
  73. fd_set readfds, writefds, exceptfds;
  74. #ifdef USING_FAKE_TIMEVAL
  75. #undef timeval
  76. #undef tv_sec
  77. #undef tv_usec
  78. #endif
  79. struct timeval _timeout;
  80. _timeout.tv_sec = timeout / 1000;
  81. _timeout.tv_usec = (timeout % 1000) * 1000;
  82. FD_ZERO(&readfds);
  83. FD_ZERO(&writefds);
  84. FD_ZERO(&exceptfds);
  85. maxfd = -1;
  86. for (idx = 0; idx < nfds; ++idx) {
  87. ufds[idx].revents = 0;
  88. fd = ufds[idx].fd;
  89. if (fd > maxfd) {
  90. maxfd = fd;
  91. #ifdef MS_WINDOWS
  92. any_fds_set = 1;
  93. #endif
  94. }
  95. if (ufds[idx].events & POLLIN)
  96. FD_SET(fd, &readfds);
  97. if (ufds[idx].events & POLLOUT)
  98. FD_SET(fd, &writefds);
  99. FD_SET(fd, &exceptfds);
  100. }
  101. #ifdef MS_WINDOWS
  102. if (!any_fds_set) {
  103. Sleep(timeout);
  104. return 0;
  105. }
  106. #endif
  107. r = select(maxfd + 1, &readfds, &writefds, &exceptfds,
  108. timeout == -1 ? NULL : &_timeout);
  109. if (r <= 0)
  110. return r;
  111. r = 0;
  112. for (idx = 0; idx < nfds; ++idx) {
  113. fd = ufds[idx].fd;
  114. if (FD_ISSET(fd, &readfds))
  115. ufds[idx].revents |= POLLIN;
  116. if (FD_ISSET(fd, &writefds))
  117. ufds[idx].revents |= POLLOUT;
  118. if (FD_ISSET(fd, &exceptfds))
  119. ufds[idx].revents |= POLLERR;
  120. if (ufds[idx].revents)
  121. ++r;
  122. }
  123. return r;
  124. }
  125. #endif
  126. /*
  127. Local Variables:
  128. mode:c
  129. indent-tabs-mode:nil
  130. c-basic-offset:4
  131. End:
  132. */