From: LeonardoBizzoni Date: Thu, 15 Aug 2024 18:47:26 +0000 (+0200) Subject: Multithreaded `serve` call X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=868832315cf06c6de581126728fdab15d12ba672;p=http-lib Multithreaded `serve` call --- diff --git a/src/listener.cpp b/src/listener.cpp index 022d033..2dca863 100644 --- a/src/listener.cpp +++ b/src/listener.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "const_definitions.h" #include "error.h" @@ -47,25 +48,27 @@ namespace http { continue; } - Response resp; - auto raw_req = read_raw_message(clientfd); - if (raw_req.has_value()) { - auto maybe_req = Request::build(raw_req.value()); - - if (maybe_req.has_value()) { - try { - std::stringstream ss; - ss << maybe_req.value().method << " " << maybe_req.value().path; - resp = this->routes.at(ss.str())(maybe_req.value()); - } catch (...) { + std::thread([&]() { + Response resp; + auto raw_req = read_raw_message(clientfd); + if (raw_req.has_value()) { + auto maybe_req = Request::build(raw_req.value()); + + if (maybe_req.has_value()) { + try { + std::stringstream ss; + ss << maybe_req.value().method << " " << maybe_req.value().path; + resp = this->routes.at(ss.str())(maybe_req.value()); + } catch (...) { + } } } - } - // Send `resp` to client + // Send `resp` to client - ::shutdown(clientfd, SHUT_RDWR); - ::close(clientfd); + ::shutdown(clientfd, SHUT_RDWR); + ::close(clientfd); + }).detach(); } return {};