From b5ebcd199c1b603cea652847bfc9177c60fb8e28 Mon Sep 17 00:00:00 2001 From: Lei Feiwei Date: Sat, 4 Apr 2015 22:12:07 +0800 Subject: [PATCH] THRIFT-3080: use select() instead poll() for early windows compatibility. --- .../src/thrift/server/TNonblockingServer.cpp | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/cpp/src/thrift/server/TNonblockingServer.cpp b/lib/cpp/src/thrift/server/TNonblockingServer.cpp index 31bc34b9c..8590bff72 100644 --- a/lib/cpp/src/thrift/server/TNonblockingServer.cpp +++ b/lib/cpp/src/thrift/server/TNonblockingServer.cpp @@ -28,7 +28,10 @@ #include #include -#include + +#ifdef HAVE_SYS_SELECT_H +#include +#endif #ifdef HAVE_SYS_SOCKET_H #include @@ -1394,33 +1397,36 @@ bool TNonblockingIOThread::notify(TNonblockingServer::TConnection* conn) { return false; } + fd_set wfds, efds; int ret = -1; - struct pollfd pfd = {fd, POLLOUT, 0}; int kSize = sizeof(conn); const char * pos = (const char *)const_cast_sockopt(&conn); while (kSize > 0) { - pfd.revents = 0; - ret = poll(&pfd, 1, -1); + FD_ZERO(&wfds); + FD_ZERO(&efds); + FD_SET(fd, &wfds); + FD_SET(fd, &efds); + ret = select(fd + 1, NULL, &wfds, &efds, NULL); if (ret < 0) { return false; } else if (ret == 0) { continue; } - if (pfd.revents & POLLHUP || pfd.revents & POLLERR) { - ::close(fd); + if (FD_ISSET(fd, &efds)) { + ::THRIFT_CLOSESOCKET(fd); return false; } - if (pfd.revents & POLLOUT) { + if (FD_ISSET(fd, &wfds)) { ret = send(fd, pos, kSize, 0); if (ret < 0) { if (errno == EAGAIN) { continue; } - ::close(fd); + ::THRIFT_CLOSESOCKET(fd); return false; }