mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-26 20:48:24 +00:00
Serve connections without parking through KeepAliveConnection
Server::process_and_close_socket() repeated what KeepAliveConnection already does for keep-alive parking: make the stream, look up the addresses, run process_server_socket_core(), and close. Make it create the connection with new_keep_alive_connection() and resume() it once, with a flag that waits for the next request on the worker through keep_alive(), as before. The flag is checked first, ahead of the stop check, since keep_alive() serves a request that is already on the socket even when the server is stopping. detail::process_server_socket() and process_server_socket_ssl() are no longer called by the library, but overrides of process_and_close_socket() call them. Instantiate them in include_httplib.cc, which only the header build compiles, so that a change to process_server_socket_core() cannot break them unnoticed.
This commit is contained in:
1 parent
084b30055e
commit
daf46bd502
2 files changed
+24
-23
No files matched your search
@@ -14404,6 +14404,10 @@ struct Server::KeepAliveConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool wait_for_next_request() {
|
bool wait_for_next_request() {
|
||||||
|
if (wait_in_place) {
|
||||||
|
return detail::keep_alive(svr.svr_sock_, sock,
|
||||||
|
svr.keep_alive_timeout_sec_);
|
||||||
|
}
|
||||||
// The event loop saw the socket readable just before this ran.
|
// The event loop saw the socket readable just before this ran.
|
||||||
if (turn.woken) {
|
if (turn.woken) {
|
||||||
turn.woken = false;
|
turn.woken = false;
|
||||||
@@ -14427,6 +14431,8 @@ struct Server::KeepAliveConnection {
|
|||||||
int remote_port = 0;
|
int remote_port = 0;
|
||||||
int local_port = 0;
|
int local_port = 0;
|
||||||
bool opened = false; // by resume(), once open() has succeeded
|
bool opened = false; // by resume(), once open() has succeeded
|
||||||
|
// Waits for the next request on the worker, as without parking.
|
||||||
|
bool wait_in_place = false;
|
||||||
Turn turn;
|
Turn turn;
|
||||||
size_t served = 0;
|
size_t served = 0;
|
||||||
std::string remote_addr;
|
std::string remote_addr;
|
||||||
@@ -16463,29 +16469,10 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
|||||||
inline bool Server::is_valid() const { return !has_invalid_registration_; }
|
inline bool Server::is_valid() const { return !has_invalid_registration_; }
|
||||||
|
|
||||||
inline bool Server::process_and_close_socket(socket_t sock) {
|
inline bool Server::process_and_close_socket(socket_t sock) {
|
||||||
std::string remote_addr;
|
auto conn = new_keep_alive_connection(sock);
|
||||||
int remote_port = 0;
|
conn->wait_in_place = true;
|
||||||
detail::get_remote_ip_and_port(sock, remote_addr, remote_port);
|
auto ret = conn->resume();
|
||||||
|
conn->close_served(!conn->turn.websocket_upgraded && ret);
|
||||||
std::string local_addr;
|
|
||||||
int local_port = 0;
|
|
||||||
detail::get_local_ip_and_port(sock, local_addr, local_port);
|
|
||||||
|
|
||||||
bool websocket_upgraded = false;
|
|
||||||
auto ret = serve_guarded([&]() {
|
|
||||||
return detail::process_server_socket(
|
|
||||||
svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_,
|
|
||||||
read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
|
|
||||||
write_timeout_usec_,
|
|
||||||
[&](Stream &strm, bool close_connection, bool &connection_closed) {
|
|
||||||
return process_request(strm, remote_addr, remote_port, local_addr,
|
|
||||||
local_port, close_connection,
|
|
||||||
connection_closed, nullptr,
|
|
||||||
&websocket_upgraded);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
detail::drain_and_close_socket(sock);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,3 +3,17 @@
|
|||||||
// when linked together with test.cc.
|
// when linked together with test.cc.
|
||||||
|
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
|
|
||||||
|
// The library no longer calls these, but subclasses that override
|
||||||
|
// process_and_close_socket() do. Instantiating them here, where the split build
|
||||||
|
// does not reach, keeps a change to process_server_socket_core() from breaking
|
||||||
|
// them unnoticed.
|
||||||
|
struct IncludeHttplibServeNothing {
|
||||||
|
bool operator()(httplib::Stream &, bool, bool &) const { return false; }
|
||||||
|
};
|
||||||
|
auto include_httplib_process_server_socket =
|
||||||
|
&httplib::detail::process_server_socket<IncludeHttplibServeNothing>;
|
||||||
|
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||||
|
auto include_httplib_process_server_socket_ssl =
|
||||||
|
&httplib::detail::process_server_socket_ssl<IncludeHttplibServeNothing>;
|
||||||
|
#endif
|
||||||
Reference in new issue
Block a user