]> git.leonardobizzoni.com Git - http-lib/commitdiff
Multithreaded `serve` call
authorLeonardoBizzoni <leo2002714@gmail.com>
Thu, 15 Aug 2024 18:47:26 +0000 (20:47 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Thu, 15 Aug 2024 18:47:26 +0000 (20:47 +0200)
src/listener.cpp

index 022d0336336b3f4eefc5c61092653d46fa5b6569..2dca863f0a84195c3527f0a4f6917de92c532b83 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <cstdio>
 #include <sstream>
+#include <thread>
 
 #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 {};