poll.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Copyright (c) 2013 Martin Sustrik All rights reserved.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. and/or sell copies of the Software, and to permit persons to whom
  8. the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. */
  19. #include "../nn.h"
  20. #if defined NN_HAVE_WINDOWS
  21. #include "../utils/win.h"
  22. #include "../utils/fast.h"
  23. #include "../utils/sleep.h"
  24. #include "../utils/err.h"
  25. int nn_poll (struct nn_pollfd *fds, int nfds, int timeout)
  26. {
  27. int rc;
  28. int i;
  29. fd_set fdset;
  30. SOCKET fd;
  31. int res;
  32. size_t sz;
  33. struct timeval tv;
  34. /* Fill in the fdset, as appropriate. */
  35. FD_ZERO (&fdset);
  36. for (i = 0; i != nfds; ++i) {
  37. if (fds [i].events & NN_POLLIN) {
  38. sz = sizeof (fd);
  39. rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_RCVFD, &fd, &sz);
  40. if (nn_slow (rc < 0)) {
  41. errno = -rc;
  42. return -1;
  43. }
  44. nn_assert (sz == sizeof (fd));
  45. FD_SET (fd, &fdset);
  46. }
  47. if (fds [i].events & NN_POLLOUT) {
  48. sz = sizeof (fd);
  49. rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_SNDFD, &fd, &sz);
  50. if (nn_slow (rc < 0)) {
  51. errno = -rc;
  52. return -1;
  53. }
  54. nn_assert (sz == sizeof (fd));
  55. FD_SET (fd, &fdset);
  56. }
  57. }
  58. /* Do the polling itself. */
  59. tv.tv_sec = timeout / 1000;
  60. tv.tv_usec = timeout % 1000 * 1000;
  61. if (nn_fast (nfds)) {
  62. rc = select (-1, &fdset, NULL, NULL, &tv);
  63. if (nn_slow (rc == 0))
  64. return 0;
  65. if (nn_slow (rc == SOCKET_ERROR)) {
  66. errno = nn_err_wsa_to_posix (WSAGetLastError ());
  67. return -1;
  68. }
  69. }
  70. else {
  71. /* POSIX platforms will sleep until timeout is expired,
  72. so let's do the same on Windows. */
  73. if (timeout > 0)
  74. nn_sleep(timeout);
  75. return 0;
  76. }
  77. /* Move the results from fdset to the nanomsg pollset. */
  78. res = 0;
  79. for (i = 0; i != nfds; ++i) {
  80. fds [i].revents = 0;
  81. if (fds [i].events & NN_POLLIN) {
  82. sz = sizeof (fd);
  83. rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_RCVFD, &fd, &sz);
  84. if (nn_slow (rc < 0)) {
  85. errno = -rc;
  86. return -1;
  87. }
  88. nn_assert (sz == sizeof (fd));
  89. if (FD_ISSET (fd, &fdset))
  90. fds [i].revents |= NN_POLLIN;
  91. }
  92. if (fds [i].events & NN_POLLOUT) {
  93. sz = sizeof (fd);
  94. rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_SNDFD, &fd, &sz);
  95. if (nn_slow (rc < 0)) {
  96. errno = -rc;
  97. return -1;
  98. }
  99. nn_assert (sz == sizeof (fd));
  100. if (FD_ISSET (fd, &fdset))
  101. fds [i].revents |= NN_POLLOUT;
  102. }
  103. if (fds [i].revents)
  104. ++res;
  105. }
  106. return res;
  107. }
  108. #else
  109. #include "../utils/alloc.h"
  110. #include "../utils/fast.h"
  111. #include "../utils/err.h"
  112. #include <poll.h>
  113. #include <stddef.h>
  114. int nn_poll (struct nn_pollfd *fds, int nfds, int timeout)
  115. {
  116. int rc;
  117. int i;
  118. int pos;
  119. int fd;
  120. int res;
  121. size_t sz;
  122. struct pollfd *pfd;
  123. /* Construct a pollset to be used with OS-level 'poll' function. */
  124. pfd = nn_alloc (sizeof (struct pollfd) * nfds * 2, "pollset");
  125. alloc_assert (pfd);
  126. pos = 0;
  127. for (i = 0; i != nfds; ++i) {
  128. if (fds [i].events & NN_POLLIN) {
  129. sz = sizeof (fd);
  130. rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_RCVFD, &fd, &sz);
  131. if (nn_slow (rc < 0)) {
  132. nn_free (pfd);
  133. errno = -rc;
  134. return -1;
  135. }
  136. nn_assert (sz == sizeof (fd));
  137. pfd [pos].fd = fd;
  138. pfd [pos].events = POLLIN;
  139. ++pos;
  140. }
  141. if (fds [i].events & NN_POLLOUT) {
  142. sz = sizeof (fd);
  143. rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_SNDFD, &fd, &sz);
  144. if (nn_slow (rc < 0)) {
  145. nn_free (pfd);
  146. errno = -rc;
  147. return -1;
  148. }
  149. nn_assert (sz == sizeof (fd));
  150. pfd [pos].fd = fd;
  151. pfd [pos].events = POLLIN;
  152. ++pos;
  153. }
  154. }
  155. /* Do the polling itself. */
  156. rc = poll (pfd, pos, timeout);
  157. if (nn_slow (rc <= 0)) {
  158. res = errno;
  159. nn_free (pfd);
  160. errno = res;
  161. return rc;
  162. }
  163. /* Move the results from OS-level poll to nn_poll's pollset. */
  164. res = 0;
  165. pos = 0;
  166. for (i = 0; i != nfds; ++i) {
  167. fds [i].revents = 0;
  168. if (fds [i].events & NN_POLLIN) {
  169. if (pfd [pos].revents & POLLIN)
  170. fds [i].revents |= NN_POLLIN;
  171. ++pos;
  172. }
  173. if (fds [i].events & NN_POLLOUT) {
  174. if (pfd [pos].revents & POLLIN)
  175. fds [i].revents |= NN_POLLOUT;
  176. ++pos;
  177. }
  178. if (fds [i].revents)
  179. ++res;
  180. }
  181. nn_free (pfd);
  182. return res;
  183. }
  184. #endif