From 026a69ebb0e99beff9de837dfbf6b4e1dbe92eda Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 25 May 2016 18:53:59 -0700 Subject: [PATCH] http: Allow non-blocking on Windows. --- ext/native/file/fd_util.cpp | 14 +++++++++++--- ext/native/file/fd_util.h | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/ext/native/file/fd_util.cpp b/ext/native/file/fd_util.cpp index f8f9fa7e86..ecb0027e98 100644 --- a/ext/native/file/fd_util.cpp +++ b/ext/native/file/fd_util.cpp @@ -75,7 +75,7 @@ ssize_t Write(int fd, const std::string &str) { return WriteLine(fd, str.c_str(), str.size()); } -bool WaitUntilReady(int fd, double timeout) { +bool WaitUntilReady(int fd, double timeout, bool for_write) { struct timeval tv; tv.tv_sec = floor(timeout); tv.tv_usec = (timeout - floor(timeout)) * 1000000.0; @@ -84,7 +84,12 @@ bool WaitUntilReady(int fd, double timeout) { FD_ZERO(&fds); FD_SET(fd, &fds); // First argument to select is the highest socket in the set + 1. - int rval = select(fd + 1, &fds, NULL, NULL, &tv); + int rval; + if (for_write) { + rval = select(fd + 1, NULL, &fds, NULL, &tv); + } else { + rval = select(fd + 1, &fds, NULL, NULL, &tv); + } if (rval < 0) { // Error calling select. return false; @@ -115,7 +120,10 @@ void SetNonBlocking(int sock, bool non_blocking) { ELOG("Error setting socket nonblocking status"); } #else - WLOG("NonBlocking mode not supported on Win32"); + u_long val = non_blocking ? 1 : 0; + if (ioctlsocket(sock, FIONBIO, &val) != 0) { + ELOG("Error setting socket nonblocking status"); + } #endif } diff --git a/ext/native/file/fd_util.h b/ext/native/file/fd_util.h index a38e59b357..6d64e018ab 100644 --- a/ext/native/file/fd_util.h +++ b/ext/native/file/fd_util.h @@ -18,7 +18,7 @@ ssize_t Write(int fd, const std::string &str); // Returns true if the fd became ready, false if it didn't or // if there was another error. -bool WaitUntilReady(int fd, double timeout); +bool WaitUntilReady(int fd, double timeout, bool for_write = false); void SetNonBlocking(int fd, bool non_blocking);