mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-21 20:01:46 +00:00
Pass the keep-alive wait and the served count into the server loop
process_server_socket_core() now takes the function that waits for the next request and the number of requests served so far, instead of calling keep_alive() and counting on its own stack. Both callers pass keep_alive() and a fresh counter, so nothing changes yet; this lets a caller stop waiting and resume the same connection later without losing track of keep_alive_max_count.
This commit is contained in:
1 parent
a69d6a0a35
commit
c51417f458
1 file changed
+14
-10
@@ -7183,20 +7183,20 @@ inline bool keep_alive(const std::atomic<socket_t> &svr_sock, socket_t sock,
|
|||||||
// 9.3.2), so reading one request can pull the start of the next one into the
|
// 9.3.2), so reading one request can pull the start of the next one into the
|
||||||
// stream's buffer; that request must be served without waiting for the socket
|
// stream's buffer; that request must be served without waiting for the socket
|
||||||
// to become readable again, since its bytes are no longer on the socket.
|
// to become readable again, since its bytes are no longer on the socket.
|
||||||
// `callback` is told whether keep_alive() has just seen the socket go readable.
|
// Otherwise `wait_for_next_request` waits for the socket, and returning false
|
||||||
template <typename P, typename T>
|
// ends the loop. `callback` is told whether the socket has just been seen
|
||||||
inline bool process_server_socket_core(const std::atomic<socket_t> &svr_sock,
|
// readable. `served` counts the requests served on this connection so far.
|
||||||
socket_t sock,
|
template <typename W, typename P, typename T>
|
||||||
|
inline bool process_server_socket_core(size_t &served,
|
||||||
size_t keep_alive_max_count,
|
size_t keep_alive_max_count,
|
||||||
time_t keep_alive_timeout_sec,
|
W wait_for_next_request,
|
||||||
P has_buffered_request, T callback) {
|
P has_buffered_request, T callback) {
|
||||||
auto ret = false;
|
auto ret = false;
|
||||||
// A keep_alive_max_count of 0 sets no limit.
|
// A keep_alive_max_count of 0 sets no limit.
|
||||||
for (size_t served = 0;
|
for (; keep_alive_max_count == 0 || served < keep_alive_max_count; served++) {
|
||||||
keep_alive_max_count == 0 || served < keep_alive_max_count; served++) {
|
|
||||||
auto socket_readable = false;
|
auto socket_readable = false;
|
||||||
if (!has_buffered_request()) {
|
if (!has_buffered_request()) {
|
||||||
if (!keep_alive(svr_sock, sock, keep_alive_timeout_sec)) { break; }
|
if (!wait_for_next_request()) { break; }
|
||||||
socket_readable = true;
|
socket_readable = true;
|
||||||
}
|
}
|
||||||
auto close_connection = served + 1 == keep_alive_max_count;
|
auto close_connection = served + 1 == keep_alive_max_count;
|
||||||
@@ -7218,8 +7218,10 @@ process_server_socket(const std::atomic<socket_t> &svr_sock, socket_t sock,
|
|||||||
// the next, pipelined request.
|
// the next, pipelined request.
|
||||||
SocketStream strm(sock, read_timeout_sec, read_timeout_usec,
|
SocketStream strm(sock, read_timeout_sec, read_timeout_usec,
|
||||||
write_timeout_sec, write_timeout_usec);
|
write_timeout_sec, write_timeout_usec);
|
||||||
|
size_t served = 0;
|
||||||
return process_server_socket_core(
|
return process_server_socket_core(
|
||||||
svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec,
|
served, keep_alive_max_count,
|
||||||
|
[&]() { return keep_alive(svr_sock, sock, keep_alive_timeout_sec); },
|
||||||
[&]() { return strm.is_readable(); },
|
[&]() { return strm.is_readable(); },
|
||||||
[&](bool socket_readable, bool close_connection,
|
[&](bool socket_readable, bool close_connection,
|
||||||
bool &connection_closed) {
|
bool &connection_closed) {
|
||||||
@@ -11406,8 +11408,10 @@ inline bool process_server_socket_ssl(
|
|||||||
// of a pipelined request, which keep_alive() cannot see on the socket.
|
// of a pipelined request, which keep_alive() cannot see on the socket.
|
||||||
SSLSocketStream strm(sock, session, read_timeout_sec, read_timeout_usec,
|
SSLSocketStream strm(sock, session, read_timeout_sec, read_timeout_usec,
|
||||||
write_timeout_sec, write_timeout_usec);
|
write_timeout_sec, write_timeout_usec);
|
||||||
|
size_t served = 0;
|
||||||
return process_server_socket_core(
|
return process_server_socket_core(
|
||||||
svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec,
|
served, keep_alive_max_count,
|
||||||
|
[&]() { return keep_alive(svr_sock, sock, keep_alive_timeout_sec); },
|
||||||
[&]() { return strm.is_readable(); },
|
[&]() { return strm.is_readable(); },
|
||||||
[&](bool socket_readable, bool close_connection,
|
[&](bool socket_readable, bool close_connection,
|
||||||
bool &connection_closed) {
|
bool &connection_closed) {
|
||||||
|
|||||||
Reference in new issue
Block a user