From d04947980d1eb8cad2105015df19923ebff16cf3 Mon Sep 17 00:00:00 2001 From: The-EDev Date: Wed, 21 Oct 2020 16:06:49 +0300 Subject: [PATCH] Added method to set timeout (default is 5 seconds) --- include/crow/app.h | 19 ++++++++++++++++++- include/crow/dumb_timer_queue.h | 7 +++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/include/crow/app.h b/include/crow/app.h index 169c4484b..f1e37435d 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -17,6 +17,7 @@ #include "crow/middleware_context.h" #include "crow/http_request.h" #include "crow/http_server.h" +#include "crow/dumb_timer_queue.h" #ifdef CROW_MSVC_WORKAROUND @@ -27,6 +28,7 @@ namespace crow { + int detail::dumb_timer_queue::tick = 5; #ifdef CROW_ENABLE_SSL using ssl_context_t = boost::asio::ssl::context; #endif @@ -43,12 +45,14 @@ namespace crow { } - template + + template void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) { router_.handle_upgrade(req, res, adaptor); } + ///Process the request and generate a response for it void handle(const request& req, response& res) { router_.handle(req, res); @@ -66,23 +70,34 @@ namespace crow return router_.new_rule_tagged(std::move(rule)); } + ///Set the port that Crow will hadnle requests on self_t& port(std::uint16_t port) { port_ = port; return *this; } + ///Set the connection timeout in seconds (default is 5) + self_t& timeout(std::uint8_t timeout) + { + detail::dumb_timer_queue::tick = timeout; + return *this; + } + + ///The IP address that Crow will handle requests on (default is 0.0.0.0) self_t& bindaddr(std::string bindaddr) { bindaddr_ = bindaddr; return *this; } + ///Run the server on multiple threads using all available threads self_t& multithreaded() { return concurrency(std::thread::hardware_concurrency()); } + ///Run the server on multiple threads using a specific number self_t& concurrency(std::uint16_t concurrency) { if (concurrency < 1) @@ -103,6 +118,7 @@ namespace crow cv_started_.notify_all(); } + ///Run the server void run() { validate(); @@ -124,6 +140,7 @@ namespace crow } } + ///Stop the server void stop() { #ifdef CROW_ENABLE_SSL diff --git a/include/crow/dumb_timer_queue.h b/include/crow/dumb_timer_queue.h index fd5151834..e597ce970 100644 --- a/include/crow/dumb_timer_queue.h +++ b/include/crow/dumb_timer_queue.h @@ -12,10 +12,11 @@ namespace crow { namespace detail { - // fast timer queue for fixed tick value. + ///Fast timer queue for fixed tick value. class dumb_timer_queue { public: + static int tick; using key = std::pair; void cancel(key& k) @@ -48,7 +49,7 @@ namespace crow while(!dq_.empty()) { auto& x = dq_.front(); - if (now - x.first < std::chrono::seconds(tick)) + if (now - x.first < std::chrono::seconds(dumb_timer_queue::tick)) break; if (x.second) { @@ -71,8 +72,6 @@ namespace crow } private: - - int tick{5}; boost::asio::io_service* io_service_{}; std::deque>> dq_; int step_{};