Added method to set timeout (default is 5 seconds)

This commit is contained in:
The-EDev 2020-10-21 16:06:49 +03:00
parent 8a99167d59
commit d04947980d
2 changed files with 21 additions and 5 deletions

View File

@ -17,6 +17,7 @@
#include "crow/middleware_context.h" #include "crow/middleware_context.h"
#include "crow/http_request.h" #include "crow/http_request.h"
#include "crow/http_server.h" #include "crow/http_server.h"
#include "crow/dumb_timer_queue.h"
#ifdef CROW_MSVC_WORKAROUND #ifdef CROW_MSVC_WORKAROUND
@ -27,6 +28,7 @@
namespace crow namespace crow
{ {
int detail::dumb_timer_queue::tick = 5;
#ifdef CROW_ENABLE_SSL #ifdef CROW_ENABLE_SSL
using ssl_context_t = boost::asio::ssl::context; using ssl_context_t = boost::asio::ssl::context;
#endif #endif
@ -43,12 +45,14 @@ namespace crow
{ {
} }
template <typename Adaptor> template <typename Adaptor>
void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) void handle_upgrade(const request& req, response& res, Adaptor&& adaptor)
{ {
router_.handle_upgrade(req, res, adaptor); router_.handle_upgrade(req, res, adaptor);
} }
///Process the request and generate a response for it
void handle(const request& req, response& res) void handle(const request& req, response& res)
{ {
router_.handle(req, res); router_.handle(req, res);
@ -66,23 +70,34 @@ namespace crow
return router_.new_rule_tagged<Tag>(std::move(rule)); return router_.new_rule_tagged<Tag>(std::move(rule));
} }
///Set the port that Crow will hadnle requests on
self_t& port(std::uint16_t port) self_t& port(std::uint16_t port)
{ {
port_ = port; port_ = port;
return *this; 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) self_t& bindaddr(std::string bindaddr)
{ {
bindaddr_ = bindaddr; bindaddr_ = bindaddr;
return *this; return *this;
} }
///Run the server on multiple threads using all available threads
self_t& multithreaded() self_t& multithreaded()
{ {
return concurrency(std::thread::hardware_concurrency()); return concurrency(std::thread::hardware_concurrency());
} }
///Run the server on multiple threads using a specific number
self_t& concurrency(std::uint16_t concurrency) self_t& concurrency(std::uint16_t concurrency)
{ {
if (concurrency < 1) if (concurrency < 1)
@ -103,6 +118,7 @@ namespace crow
cv_started_.notify_all(); cv_started_.notify_all();
} }
///Run the server
void run() void run()
{ {
validate(); validate();
@ -124,6 +140,7 @@ namespace crow
} }
} }
///Stop the server
void stop() void stop()
{ {
#ifdef CROW_ENABLE_SSL #ifdef CROW_ENABLE_SSL

View File

@ -12,10 +12,11 @@ namespace crow
{ {
namespace detail namespace detail
{ {
// fast timer queue for fixed tick value. ///Fast timer queue for fixed tick value.
class dumb_timer_queue class dumb_timer_queue
{ {
public: public:
static int tick;
using key = std::pair<dumb_timer_queue*, int>; using key = std::pair<dumb_timer_queue*, int>;
void cancel(key& k) void cancel(key& k)
@ -48,7 +49,7 @@ namespace crow
while(!dq_.empty()) while(!dq_.empty())
{ {
auto& x = dq_.front(); auto& x = dq_.front();
if (now - x.first < std::chrono::seconds(tick)) if (now - x.first < std::chrono::seconds(dumb_timer_queue::tick))
break; break;
if (x.second) if (x.second)
{ {
@ -71,8 +72,6 @@ namespace crow
} }
private: private:
int tick{5};
boost::asio::io_service* io_service_{}; boost::asio::io_service* io_service_{};
std::deque<std::pair<decltype(std::chrono::steady_clock::now()), std::function<void()>>> dq_; std::deque<std::pair<decltype(std::chrono::steady_clock::now()), std::function<void()>>> dq_;
int step_{}; int step_{};