Permalink
Browse files

Windows version of nn_poll implemented.

Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
  • Loading branch information...
1 parent be8a857 commit d817e40bf661b3de54f6b9e2b3472592e1e2b6c4 @sustrik sustrik committed Nov 24, 2013
Showing with 89 additions and 1 deletion.
  1. +88 −0 src/core/poll.c
  2. +1 −1 tests/poll.c
View
@@ -22,6 +22,93 @@
#include "../nn.h"
+#if defined NN_HAVE_WINDOWS
+
+#include "../utils/win.h"
+#include "../utils/fast.h"
+#include "../utils/err.h"
+
+int nn_poll (struct nn_pollfd *fds, int nfds, int timeout)
+{
+ int rc;
+ int i;
+ fd_set fdset;
+ int fd;
+ int res;
+ size_t sz;
+ struct timeval tv;
+
+ /* Fill in the fdset, as appropriate. */
+ FD_ZERO (&fdset);
+ for (i = 0; i != nfds; ++i) {
+ if (fds [i].events & NN_POLLIN) {
+ sz = sizeof (fd);
+ rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_RCVFD, &fd, &sz);
+ if (nn_slow (rc < 0)) {
+ errno = -rc;
+ return -1;
+ }
+ nn_assert (sz == sizeof (fd));
+ FD_SET (fd, &fdset);
+ }
+ if (fds [i].events & NN_POLLOUT) {
+ sz = sizeof (fd);
+ rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_SNDFD, &fd, &sz);
+ if (nn_slow (rc < 0)) {
+ errno = -rc;
+ return -1;
+ }
+ nn_assert (sz == sizeof (fd));
+ FD_SET (fd, &fdset);
+ }
+ }
+
+ /* Do the polling itself. */
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = timeout % 1000 * 1000;
+ rc = select (-1, &fdset, NULL, NULL, &tv);
+ if (nn_slow (rc == 0))
+ return 0;
+ if (nn_slow (rc == SOCKET_ERROR)) {
+ errno = nn_err_wsa_to_posix (WSAGetLastError ());
+ return -1;
+ }
+
+ /* Move the results from fdset to the nanomsg pollset. */
+ res = 0;
+ for (i = 0; i != nfds; ++i) {
+ fds [i].revents = 0;
+ if (fds [i].events & NN_POLLIN) {
+ sz = sizeof (fd);
+ rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_RCVFD, &fd, &sz);
+ if (nn_slow (rc < 0)) {
+ errno = -rc;
+ return -1;
+ }
+ nn_assert (sz == sizeof (fd));
+ if (FD_ISSET (fd, &fdset))
+ fds [i].revents |= NN_POLLIN;
+ }
+ if (fds [i].events & NN_POLLOUT) {
+ sz = sizeof (fd);
+ rc = nn_getsockopt (fds [i].fd, NN_SOL_SOCKET, NN_SNDFD, &fd, &sz);
+ if (nn_slow (rc < 0)) {
+ errno = -rc;
+ return -1;
+ }
+ nn_assert (sz == sizeof (fd));
+ if (FD_ISSET (fd, &fdset))
+ fds [i].revents |= NN_POLLOUT;
+ }
+ if (fds [i].revents)
+ ++res;
+ }
+
+ return res;
+}
+
+#else
+
#include "../utils/alloc.h"
#include "../utils/fast.h"
#include "../utils/err.h"
@@ -103,3 +190,4 @@ int nn_poll (struct nn_pollfd *fds, int nfds, int timeout)
return res;
}
+#endif
View
@@ -141,7 +141,7 @@ int main ()
rc = nn_poll (pfd, 2, -1);
errno_assert (rc >= 0);
nn_assert (rc == 2);
- nn_assert (pfd [0].revents == NN_POLLIN | NN_POLLOUT);
+ nn_assert (pfd [0].revents == (NN_POLLIN | NN_POLLOUT));
nn_assert (pfd [1].revents == NN_POLLOUT);
test_close (sc);
test_close (sb);

0 comments on commit d817e40

Please sign in to comment.