From c7d22e451f083706123ea4bdc0510565c3b2f291 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 16 Jun 2020 21:20:47 -0400 Subject: [PATCH] Fixed timeout calculation bugs --- httplib.h | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/httplib.h b/httplib.h index 0859b4d..54056e5 100644 --- a/httplib.h +++ b/httplib.h @@ -1701,7 +1701,7 @@ inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { pfd_read.fd = sock; pfd_read.events = POLLIN; - auto timeout = static_cast(sec * 1000 + usec / 1000); + auto timeout = static_cast(sec * 1000 + usec); return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); #else @@ -1749,7 +1749,7 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { pfd_read.fd = sock; pfd_read.events = POLLIN | POLLOUT; - auto timeout = static_cast(sec * 1000 + usec / 1000); + auto timeout = static_cast(sec * 1000 + usec); auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); @@ -1850,25 +1850,19 @@ private: size_t position = 0; }; -inline bool keep_alive(socket_t sock, std::function is_shutting_down) { +inline bool keep_alive(socket_t sock) { using namespace std::chrono; auto start = steady_clock::now(); while (true) { auto val = select_read(sock, 0, 10000); - if (is_shutting_down && is_shutting_down()) { - return false; - } else if (val < 0) { + if (val < 0) { return false; } else if (val == 0) { auto current = steady_clock::now(); - auto sec = duration_cast(current - start); - if (sec.count() > CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND) { + auto duration = duration_cast(current - start); + auto timeout = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND * 100 + CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; + if (duration.count() > timeout) { return false; - } else if (sec.count() == CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND) { - auto usec = duration_cast(current - start); - if (usec.count() > CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) { - return false; - } } std::this_thread::sleep_for(std::chrono::milliseconds(1)); } else {