Browse Source

Removed CPPHTTPLIB_USE_POLL, added CPPHTTPLIB_USE_SELECT

yhirose 6 years ago
parent
commit
c02849e269
1 changed files with 26 additions and 27 deletions
  1. 26 27
      httplib.h

+ 26 - 27
httplib.h

@@ -21,7 +21,6 @@
 #define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits<size_t>::max)()
 #define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits<size_t>::max)()
 #define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u)
 #define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u)
 #define CPPHTTPLIB_THREAD_POOL_COUNT 8
 #define CPPHTTPLIB_THREAD_POOL_COUNT 8
-#define CPPHTTPLIB_USE_POLL
 
 
 #ifdef _WIN32
 #ifdef _WIN32
 #ifndef _CRT_SECURE_NO_WARNINGS
 #ifndef _CRT_SECURE_NO_WARNINGS
@@ -73,7 +72,7 @@ typedef int ssize_t;
 #endif // strcasecmp
 #endif // strcasecmp
 
 
 typedef SOCKET socket_t;
 typedef SOCKET socket_t;
-#ifdef CPPHTTPLIB_USE_POLL
+#ifndef CPPHTTPLIB_USE_SELECT
 #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
 #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
 #endif
 #endif
 
 
@@ -83,7 +82,7 @@ typedef SOCKET socket_t;
 #include <cstring>
 #include <cstring>
 #include <netdb.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <netinet/in.h>
-#ifdef CPPHTTPLIB_USE_POLL
+#ifndef CPPHTTPLIB_USE_SELECT
 #include <poll.h>
 #include <poll.h>
 #endif
 #endif
 #include <pthread.h>
 #include <pthread.h>
@@ -1003,15 +1002,7 @@ inline int close_socket(socket_t sock) {
 }
 }
 
 
 inline int select_read(socket_t sock, time_t sec, time_t usec) {
 inline int select_read(socket_t sock, time_t sec, time_t usec) {
-#ifdef CPPHTTPLIB_USE_POLL
-  struct pollfd pfd_read;
-  pfd_read.fd = sock;
-  pfd_read.events = POLLIN;
-
-  auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
-
-  return poll(&pfd_read, 1, timeout);
-#else
+#ifdef CPPHTTPLIB_USE_SELECT
   fd_set fds;
   fd_set fds;
   FD_ZERO(&fds);
   FD_ZERO(&fds);
   FD_SET(sock, &fds);
   FD_SET(sock, &fds);
@@ -1021,26 +1012,19 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) {
   tv.tv_usec = static_cast<long>(usec);
   tv.tv_usec = static_cast<long>(usec);
 
 
   return select(static_cast<int>(sock + 1), &fds, nullptr, nullptr, &tv);
   return select(static_cast<int>(sock + 1), &fds, nullptr, nullptr, &tv);
-#endif
-}
-
-inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
-#ifdef CPPHTTPLIB_USE_POLL
+#else
   struct pollfd pfd_read;
   struct pollfd pfd_read;
   pfd_read.fd = sock;
   pfd_read.fd = sock;
-  pfd_read.events = POLLIN | POLLOUT;
+  pfd_read.events = POLLIN;
 
 
   auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
   auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
 
 
-  if (poll(&pfd_read, 1, timeout) > 0 &&
-      pfd_read.revents & (POLLIN | POLLOUT)) {
-    int error = 0;
-    socklen_t len = sizeof(error);
-    return getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) >= 0 &&
-           !error;
-  }
-  return false;
-#else
+  return poll(&pfd_read, 1, timeout);
+#endif
+}
+
+inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
+#ifdef CPPHTTPLIB_USE_SELECT
   fd_set fdsr;
   fd_set fdsr;
   FD_ZERO(&fdsr);
   FD_ZERO(&fdsr);
   FD_SET(sock, &fdsr);
   FD_SET(sock, &fdsr);
@@ -1060,6 +1044,21 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
            !error;
            !error;
   }
   }
   return false;
   return false;
+#else
+  struct pollfd pfd_read;
+  pfd_read.fd = sock;
+  pfd_read.events = POLLIN | POLLOUT;
+
+  auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
+
+  if (poll(&pfd_read, 1, timeout) > 0 &&
+      pfd_read.revents & (POLLIN | POLLOUT)) {
+    int error = 0;
+    socklen_t len = sizeof(error);
+    return getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) >= 0 &&
+           !error;
+  }
+  return false;
 #endif
 #endif
 }
 }