Merge pull request #217 from yhirose/poll
Use 'poll' instead of 'select'
This commit is contained in:
commit
47bc7456d2
84
httplib.h
84
httplib.h
@ -8,6 +8,21 @@
|
|||||||
#ifndef CPPHTTPLIB_HTTPLIB_H
|
#ifndef CPPHTTPLIB_HTTPLIB_H
|
||||||
#define CPPHTTPLIB_HTTPLIB_H
|
#define CPPHTTPLIB_HTTPLIB_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Configuration
|
||||||
|
*/
|
||||||
|
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5
|
||||||
|
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND 0
|
||||||
|
#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5
|
||||||
|
#define CPPHTTPLIB_READ_TIMEOUT_SECOND 5
|
||||||
|
#define CPPHTTPLIB_READ_TIMEOUT_USECOND 0
|
||||||
|
#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192
|
||||||
|
#define CPPHTTPLIB_REDIRECT_MAX_COUNT 20
|
||||||
|
#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits<size_t>::max)()
|
||||||
|
#define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u)
|
||||||
|
#define CPPHTTPLIB_THREAD_POOL_COUNT 8
|
||||||
|
#define CPPHTTPLIB_USE_POLL
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#ifndef _CRT_SECURE_NO_WARNINGS
|
#ifndef _CRT_SECURE_NO_WARNINGS
|
||||||
#define _CRT_SECURE_NO_WARNINGS
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
@ -52,11 +67,19 @@ typedef int ssize_t;
|
|||||||
#endif // strcasecmp
|
#endif // strcasecmp
|
||||||
|
|
||||||
typedef SOCKET socket_t;
|
typedef SOCKET socket_t;
|
||||||
#else
|
#ifdef CPPHTTPLIB_USE_POLL
|
||||||
|
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#else // not _WIN32
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#ifdef CPPHTTPLIB_USE_POLL
|
||||||
|
#include <poll.h>
|
||||||
|
#endif
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
@ -104,20 +127,6 @@ inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1) {
|
|||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
* Configuration
|
|
||||||
*/
|
|
||||||
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5
|
|
||||||
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND 0
|
|
||||||
#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5
|
|
||||||
#define CPPHTTPLIB_READ_TIMEOUT_SECOND 5
|
|
||||||
#define CPPHTTPLIB_READ_TIMEOUT_USECOND 0
|
|
||||||
#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192
|
|
||||||
#define CPPHTTPLIB_REDIRECT_MAX_COUNT 20
|
|
||||||
#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits<size_t>::max)()
|
|
||||||
#define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u)
|
|
||||||
#define CPPHTTPLIB_THREAD_POOL_COUNT 8
|
|
||||||
|
|
||||||
namespace httplib {
|
namespace httplib {
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
@ -986,6 +995,15 @@ 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
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(sock, &fds);
|
FD_SET(sock, &fds);
|
||||||
@ -995,9 +1013,26 @@ 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) {
|
inline bool wait_until_socket_is_ready(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 | 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, (char *)&error, &len) >= 0 &&
|
||||||
|
!error;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
#else
|
||||||
fd_set fdsr;
|
fd_set fdsr;
|
||||||
FD_ZERO(&fdsr);
|
FD_ZERO(&fdsr);
|
||||||
FD_SET(sock, &fdsr);
|
FD_SET(sock, &fdsr);
|
||||||
@ -1009,20 +1044,15 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
|
|||||||
tv.tv_sec = static_cast<long>(sec);
|
tv.tv_sec = static_cast<long>(sec);
|
||||||
tv.tv_usec = static_cast<long>(usec);
|
tv.tv_usec = static_cast<long>(usec);
|
||||||
|
|
||||||
if (select(static_cast<int>(sock + 1), &fdsr, &fdsw, &fdse, &tv) < 0) {
|
if (select(static_cast<int>(sock + 1), &fdsr, &fdsw, &fdse, &tv) > 0 &&
|
||||||
return false;
|
(FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) {
|
||||||
} else if (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw)) {
|
|
||||||
int error = 0;
|
int error = 0;
|
||||||
socklen_t len = sizeof(error);
|
socklen_t len = sizeof(error);
|
||||||
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len) < 0 ||
|
return getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len) >= 0 &&
|
||||||
error) {
|
!error;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
#endif
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -2884,9 +2914,7 @@ inline bool Client::process_request(Stream &strm, const Request &req,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (req.response_handler) {
|
if (req.response_handler) {
|
||||||
if(!req.response_handler(res)) {
|
if (!req.response_handler(res)) { return false; }
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Body
|
// Body
|
||||||
|
Loading…
x
Reference in New Issue
Block a user