diff options
-rw-r--r-- | amalgamate/crow_all.h | 183 | ||||
-rw-r--r-- | include/dumb_timer_queue.h | 2 | ||||
-rw-r--r-- | include/http_connection.h | 3 | ||||
-rw-r--r-- | include/http_request.h | 13 |
4 files changed, 108 insertions, 93 deletions
diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h index de3b1c9..9e40013 100644 --- a/amalgamate/crow_all.h +++ b/amalgamate/crow_all.h @@ -1941,94 +1941,6 @@ namespace crow -#pragma once - -#include <boost/asio.hpp> -#include <deque> -#include <functional> -#include <chrono> -#include <thread> - -namespace crow -{ - namespace detail - { - // fast timer queue for fixed tick value. - class dumb_timer_queue - { - public: - // tls based queue to avoid locking - static dumb_timer_queue& get_current_dumb_timer_queue() - { - thread_local dumb_timer_queue q; - return q; - } - - using key = std::pair<dumb_timer_queue*, int>; - - void cancel(key& k) - { - auto self = k.first; - k.first = nullptr; - if (!self) - return; - - unsigned int index = (unsigned int)(k.second - self->step_); - if (index < self->dq_.size()) - self->dq_[index].second = nullptr; - } - - key add(std::function<void()> f) - { - dq_.emplace_back(std::chrono::steady_clock::now(), std::move(f)); - int ret = step_+dq_.size()-1; - - CROW_LOG_DEBUG << "timer add inside: " << this << ' ' << ret ; - return {this, ret}; - } - - void process() - { - if (!io_service_) - return; - - auto now = std::chrono::steady_clock::now(); - while(!dq_.empty()) - { - auto& x = dq_.front(); - if (now - x.first < std::chrono::seconds(tick)) - break; - if (x.second) - { - CROW_LOG_DEBUG << "timer call: " << this << ' ' << step_; - // we know that timer handlers are very simple currenty; call here - x.second(); - } - dq_.pop_front(); - step_++; - } - } - - void set_io_service(boost::asio::io_service& io_service) - { - io_service_ = &io_service; - } - - private: - dumb_timer_queue() noexcept - { - } - - int tick{5}; - boost::asio::io_service* io_service_{}; - std::deque<std::pair<decltype(std::chrono::steady_clock::now()), std::function<void()>>> dq_; - int step_{}; - }; - } -} - - - /* merged revision: 5b951d74bd66ec9d38448e0a85b1cf8b85d97db3 */ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. * @@ -4803,8 +4715,10 @@ namespace crow WARNING = 2 ERROR = 3 CRITICAL = 4 + + default to INFO */ -#define CROW_LOG_LEVEL 0 +#define CROW_LOG_LEVEL 1 @@ -4941,6 +4855,97 @@ namespace crow #pragma once +#include <boost/asio.hpp> +#include <deque> +#include <functional> +#include <chrono> +#include <thread> + + + + +namespace crow +{ + namespace detail + { + // fast timer queue for fixed tick value. + class dumb_timer_queue + { + public: + // tls based queue to avoid locking + static dumb_timer_queue& get_current_dumb_timer_queue() + { + thread_local dumb_timer_queue q; + return q; + } + + using key = std::pair<dumb_timer_queue*, int>; + + void cancel(key& k) + { + auto self = k.first; + k.first = nullptr; + if (!self) + return; + + unsigned int index = (unsigned int)(k.second - self->step_); + if (index < self->dq_.size()) + self->dq_[index].second = nullptr; + } + + key add(std::function<void()> f) + { + dq_.emplace_back(std::chrono::steady_clock::now(), std::move(f)); + int ret = step_+dq_.size()-1; + + CROW_LOG_DEBUG << "timer add inside: " << this << ' ' << ret ; + return {this, ret}; + } + + void process() + { + if (!io_service_) + return; + + auto now = std::chrono::steady_clock::now(); + while(!dq_.empty()) + { + auto& x = dq_.front(); + if (now - x.first < std::chrono::seconds(tick)) + break; + if (x.second) + { + CROW_LOG_DEBUG << "timer call: " << this << ' ' << step_; + // we know that timer handlers are very simple currenty; call here + x.second(); + } + dq_.pop_front(); + step_++; + } + } + + void set_io_service(boost::asio::io_service& io_service) + { + io_service_ = &io_service; + } + + private: + dumb_timer_queue() noexcept + { + } + + int tick{5}; + boost::asio::io_service* io_service_{}; + std::deque<std::pair<decltype(std::chrono::steady_clock::now()), std::function<void()>>> dq_; + int step_{}; + }; + } +} + + + +#pragma once + #include <cstdint> #include <stdexcept> #include <tuple> diff --git a/include/dumb_timer_queue.h b/include/dumb_timer_queue.h index 86ec7c1..88bb661 100644 --- a/include/dumb_timer_queue.h +++ b/include/dumb_timer_queue.h @@ -6,6 +6,8 @@ #include <chrono> #include <thread> +#include "logging.h" + namespace crow { namespace detail diff --git a/include/http_connection.h b/include/http_connection.h index 54d0860..877eb21 100644 --- a/include/http_connection.h +++ b/include/http_connection.h @@ -52,7 +52,6 @@ namespace crow bool middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx) { using parent_context_t = typename Context::template partial<N-1>; - using current_context_t = typename Context::template partial<N>; before_handler_call<CurrentMW, Context, parent_context_t>(std::get<N>(middlewares), req, res, ctx, static_cast<parent_context_t&>(ctx)); if (res.is_completed()) @@ -86,7 +85,6 @@ namespace crow typename std::enable_if<(N==0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res) { using parent_context_t = typename Context::template partial<N-1>; - using current_context_t = typename Context::template partial<N>; using CurrentMW = typename std::tuple_element<N, typename std::remove_reference<Container>::type>::type; after_handler_call<CurrentMW, Context, parent_context_t>(std::get<N>(middlewares), req, res, ctx, static_cast<parent_context_t&>(ctx)); } @@ -95,7 +93,6 @@ namespace crow typename std::enable_if<(N>0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res) { using parent_context_t = typename Context::template partial<N-1>; - using current_context_t = typename Context::template partial<N>; using CurrentMW = typename std::tuple_element<N, typename std::remove_reference<Container>::type>::type; after_handler_call<CurrentMW, Context, parent_context_t>(std::get<N>(middlewares), req, res, ctx, static_cast<parent_context_t&>(ctx)); after_handlers_call_helper<N-1, Context, Container>(middlewares, ctx, req, res); diff --git a/include/http_request.h b/include/http_request.h index 7d2da67..331bc80 100644 --- a/include/http_request.h +++ b/include/http_request.h @@ -23,6 +23,18 @@ namespace crow ci_map headers; std::string body; + void* middleware_context{}; + + request() + : method(HTTPMethod::GET) + { + } + + request(HTTPMethod method, std::string url, ci_map headers, std::string body) + : method(method), url(std::move(url)), headers(std::move(headers)), body(std::move(body)) + { + } + void add_header(std::string key, std::string value) { headers.emplace(std::move(key), std::move(value)); @@ -33,6 +45,5 @@ namespace crow return crow::get_header_value(headers, key); } - void* middleware_context{}; }; } |