diff options
author | ipknHama <ipknhama@gmail.com> | 2014-08-15 11:06:00 +0900 |
---|---|---|
committer | ipknHama <ipknhama@gmail.com> | 2014-08-15 11:06:00 +0900 |
commit | c5cbb3ff54119855e63dff9666fd490a92f0db76 (patch) | |
tree | 8738295afc8e3effd3a631731489ebd0cfdd8832 /include | |
parent | f91af402f7bb2a3e3cae724512181cbf92d5a918 (diff) | |
download | crow-c5cbb3ff54119855e63dff9666fd490a92f0db76.tar.gz crow-c5cbb3ff54119855e63dff9666fd490a92f0db76.zip |
remove shared_ptr for performance
Diffstat (limited to 'include')
-rw-r--r-- | include/http_connection.h | 51 | ||||
-rw-r--r-- | include/http_server.h | 2 | ||||
-rw-r--r-- | include/parser.h | 24 |
3 files changed, 48 insertions, 29 deletions
diff --git a/include/http_connection.h b/include/http_connection.h index 04ffa3f..5de6ec8 100644 --- a/include/http_connection.h +++ b/include/http_connection.h @@ -22,7 +22,7 @@ namespace crow static int connectionCount; #endif template <typename Handler> - class Connection : public std::enable_shared_from_this<Connection<Handler>> + class Connection { public: Connection(tcp::socket&& socket, Handler* handler, const std::string& server_name) @@ -30,8 +30,7 @@ namespace crow handler_(handler), parser_(this), server_name_(server_name), - deadline_(socket_.get_io_service()), - address_str_(boost::lexical_cast<std::string>(socket_.remote_endpoint())) + deadline_(socket_.get_io_service()) { #ifdef CROW_ENABLE_DEBUG connectionCount ++; @@ -50,7 +49,7 @@ namespace crow void start() { - auto self = this->shared_from_this(); + //auto self = this->shared_from_this(); start_deadline(); do_read(); @@ -91,15 +90,15 @@ namespace crow } } - CROW_LOG_INFO << "Request: " << address_str_ << " " << this << " HTTP/" << parser_.http_major << "." << parser_.http_minor << ' ' + CROW_LOG_INFO << "Request: " << boost::lexical_cast<std::string>(socket_.remote_endpoint()) << " " << this << " HTTP/" << parser_.http_major << "." << parser_.http_minor << ' ' << method_name(req.method) << " " << req.url; if (!is_invalid_request) { deadline_.cancel(); - auto self = this->shared_from_this(); - res.complete_request_handler_ = [self]{ self->complete_request(); }; + //auto self = this->shared_from_this(); + res.complete_request_handler_ = [this]{ this->complete_request(); }; res.is_alive_helper_ = [this]()->bool{ return socket_.is_open(); }; handler_->handle(req, res); } @@ -113,11 +112,15 @@ namespace crow { CROW_LOG_INFO << "Response: " << this << ' ' << res.code << ' ' << close_connection_; - auto self = this->shared_from_this(); + //auto self = this->shared_from_this(); res.complete_request_handler_ = nullptr; if (!socket_.is_open()) + { + CROW_LOG_DEBUG << this << " delete (socket is closed) " << is_reading << ' ' << is_writing; + delete this; return; + } static std::unordered_map<int, std::string> statusCodes = { {200, "HTTP/1.1 200 OK\r\n"}, @@ -229,9 +232,10 @@ namespace crow void do_read() { - auto self = this->shared_from_this(); + //auto self = this->shared_from_this(); + is_reading = true; socket_.async_read_some(boost::asio::buffer(buffer_), - [self, this](const boost::system::error_code& ec, std::size_t bytes_transferred) + [this](const boost::system::error_code& ec, std::size_t bytes_transferred) { bool error_while_reading = true; if (!ec) @@ -249,6 +253,8 @@ namespace crow deadline_.cancel(); parser_.done(); socket_.close(); + is_reading = false; + check_destory(); } else { @@ -259,10 +265,12 @@ namespace crow void do_write() { - auto self = this->shared_from_this(); + //auto self = this->shared_from_this(); + is_writing = true; boost::asio::async_write(socket_, buffers_, - [&, self](const boost::system::error_code& ec, std::size_t bytes_transferred) + [&](const boost::system::error_code& ec, std::size_t bytes_transferred) { + is_writing = false; if (!ec) { start_deadline(); @@ -271,14 +279,25 @@ namespace crow socket_.close(); } } + else + check_destory(); }); } + void check_destory() + { + if (!is_reading && !is_writing) + { + CROW_LOG_DEBUG << this << " delete (idle) "; + delete this; + } + } + void start_deadline(int timeout = 5) { deadline_.expires_from_now(boost::posix_time::seconds(timeout)); - auto self = this->shared_from_this(); - deadline_.async_wait([self, this](const boost::system::error_code& ec) + //auto self = this->shared_from_this(); + deadline_.async_wait([this](const boost::system::error_code& ec) { if (ec || !socket_.is_open()) { @@ -310,7 +329,9 @@ namespace crow std::string date_str_; boost::asio::deadline_timer deadline_; - std::string address_str_; + + bool is_reading{}; + bool is_writing{}; }; } diff --git a/include/http_server.h b/include/http_server.h index 6a3c109..228cc8c 100644 --- a/include/http_server.h +++ b/include/http_server.h @@ -61,7 +61,7 @@ namespace crow { if (!ec) { - auto p = std::make_shared<Connection<Handler>>(std::move(socket_), handler_, server_name_); + auto p = new Connection<Handler>(std::move(socket_), handler_, server_name_); p->start(); } do_accept(); diff --git a/include/parser.h b/include/parser.h index f504248..1b8240f 100644 --- a/include/parser.h +++ b/include/parser.h @@ -82,7 +82,15 @@ namespace crow return 0; } HTTPParser(Handler* handler) : - settings_ { + handler_(handler) + { + http_parser_init(this, HTTP_REQUEST); + } + + // return false on error + bool feed(const char* buffer, int length) + { + const static http_parser_settings settings_{ on_message_begin, on_url, nullptr, @@ -91,23 +99,15 @@ namespace crow on_headers_complete, on_body, on_message_complete, - }, - handler_(handler) - { - http_parser_init(this, HTTP_REQUEST); - } + }; - // return false on error - bool feed(const char* buffer, int length) - { int nparsed = http_parser_execute(this, &settings_, buffer, length); return nparsed == length; } bool done() { - int nparsed = http_parser_execute(this, &settings_, nullptr, 0); - return nparsed == 0; + return feed(nullptr, 0); } void clear() @@ -147,8 +147,6 @@ namespace crow std::unordered_map<std::string, std::string> headers; std::string body; - http_parser_settings settings_; - Handler* handler_; }; } |