From 315c72c0a6ee101014504b7540f88cb1a0f11e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 8 Nov 2024 20:12:10 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D1=81=D0=BF=D0=B8=D0=BB=20=D1=81?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D0=B0=20-=20=D0=B2=D1=8B=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=D0=B5=D0=BD=D0=B8=D0=B5=20helpers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 7 +++- src/Session.cpp | 83 ++++++++++++++++++++++++++++++++++++++++-- src/Session.h | 74 +++++-------------------------------- src/main.cpp | 97 +------------------------------------------------ 4 files changed, 98 insertions(+), 163 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e66ed1..9fd7daa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,5 +14,10 @@ endif() set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) -add_executable(application src/main.cpp src/helpers.h src/helper.cpp src/sdk.h) +add_executable(application src/main.cpp + src/helpers.h + src/helper.cpp + src/sdk.h + src/Session.h + src/Session.cpp) target_link_libraries(application PRIVATE Threads::Threads) \ No newline at end of file diff --git a/src/Session.cpp b/src/Session.cpp index b433e1d..9c6f5b9 100644 --- a/src/Session.cpp +++ b/src/Session.cpp @@ -1,3 +1,80 @@ -// -// Created by Антон on 08.11.2024. -// +#include "Session.h" + +namespace uad +{ +session::session( + tcp::socket&& socket, + std::shared_ptr const& doc_root) + : stream_(std::move(socket)), doc_root_(doc_root) {} + +void session::run() +{ + net::dispatch(stream_.get_executor(), + beast::bind_front_handler( + &session::do_read, + shared_from_this())); +} + +void session::do_read() +{ + req_ = {}; + + stream_.expires_after(std::chrono::seconds(30)); + + http::async_read(stream_, buffer_, req_, + beast::bind_front_handler( + &session::on_read, + shared_from_this())); +} + +void session::on_read( + beast::error_code ec, + std::size_t bytes_transferred) +{ + boost::ignore_unused(bytes_transferred); + + if (ec == http::error::end_of_stream) + return do_close(); + + if (ec) + return fail(ec, "read"); + + send_response( + handle_request(*doc_root_, std::move(req_))); +} + +void session::send_response(http::message_generator&& msg) +{ + bool keep_alive = msg.keep_alive(); + + beast::async_write( + stream_, + std::move(msg), + beast::bind_front_handler( + &session::on_write, shared_from_this(), keep_alive)); +} + +void session::on_write( + bool keep_alive, + beast::error_code ec, + std::size_t bytes_transferred) +{ + boost::ignore_unused(bytes_transferred); + + if (ec) + return uad::fail(ec, "write"); + + if (!keep_alive) + { + return do_close(); + } + + do_read(); +} + +void session::do_close() +{ + beast::error_code ec; + stream_.socket().shutdown(tcp::socket::shutdown_send, ec); +} +} diff --git a/src/Session.h b/src/Session.h index cdd80db..463061f 100644 --- a/src/Session.h +++ b/src/Session.h @@ -8,11 +8,14 @@ #include #include +#include "helpers.h" + namespace uad { namespace beast = boost::beast; namespace http = beast::http; using tcp = boost::asio::ip::tcp; +namespace net = boost::asio; class session : public std::enable_shared_from_this { @@ -24,81 +27,24 @@ class session : public std::enable_shared_from_this public: session( tcp::socket&& socket, - std::shared_ptr const& doc_root) - : stream_(std::move(socket)), doc_root_(doc_root) - { - } + std::shared_ptr const& doc_root); - void run() - { - net::dispatch(stream_.get_executor(), - beast::bind_front_handler( - &session::do_read, - shared_from_this())); - } + void run(); - void do_read() - { - req_ = {}; - - stream_.expires_after(std::chrono::seconds(30)); - - http::async_read(stream_, buffer_, req_, - beast::bind_front_handler( - &session::on_read, - shared_from_this())); - } + void do_read(); void on_read( beast::error_code ec, - std::size_t bytes_transferred) - { - boost::ignore_unused(bytes_transferred); + std::size_t bytes_transferred); - if (ec == http::error::end_of_stream) - return do_close(); - if (ec) - return fail(ec, "read"); - - send_response( - handle_request(*doc_root_, std::move(req_))); - } - - void send_response(http::message_generator&& msg) - { - bool keep_alive = msg.keep_alive(); - - beast::async_write( - stream_, - std::move(msg), - beast::bind_front_handler( - &session::on_write, shared_from_this(), keep_alive)); - } + void send_response(http::message_generator&& msg); void on_write( bool keep_alive, beast::error_code ec, - std::size_t bytes_transferred) - { - boost::ignore_unused(bytes_transferred); + std::size_t bytes_transferred); - if (ec) - return fail(ec, "write"); - - if (!keep_alive) - { - return do_close(); - } - - do_read(); - } - - void do_close() - { - beast::error_code ec; - stream_.socket().shutdown(tcp::socket::shutdown_send, ec); - - } + void do_close(); }; } diff --git a/src/main.cpp b/src/main.cpp index 59fec4d..871fd38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,106 +16,13 @@ #include #include "helpers.h" +#include "Session.h" namespace beast = boost::beast; namespace http = beast::http; namespace net = boost::asio; using tcp = boost::asio::ip::tcp; -class session : public std::enable_shared_from_this -{ - beast::tcp_stream stream_; - beast::flat_buffer buffer_; - std::shared_ptr doc_root_; - http::request req_; - - public: - session( - tcp::socket&& socket, - std::shared_ptr const& doc_root) - : stream_(std::move(socket)), doc_root_(doc_root) - { - } - - void - run() - { - net::dispatch(stream_.get_executor(), - beast::bind_front_handler( - &session::do_read, - shared_from_this())); - } - - void - do_read() - { - req_ = {}; - - stream_.expires_after(std::chrono::seconds(30)); - - http::async_read(stream_, buffer_, req_, - beast::bind_front_handler( - &session::on_read, - shared_from_this())); - } - - void - on_read( - beast::error_code ec, - std::size_t bytes_transferred) - { - boost::ignore_unused(bytes_transferred); - - if (ec == http::error::end_of_stream) - return do_close(); - - if (ec) - return uad::fail(ec, "read"); - - send_response( - uad::handle_request(*doc_root_, std::move(req_))); - } - - void - send_response(http::message_generator&& msg) - { - bool keep_alive = msg.keep_alive(); - - beast::async_write( - stream_, - std::move(msg), - beast::bind_front_handler( - &session::on_write, shared_from_this(), keep_alive)); - } - - void - on_write( - bool keep_alive, - beast::error_code ec, - std::size_t bytes_transferred) - { - boost::ignore_unused(bytes_transferred); - - if (ec) - return uad::fail(ec, "write"); - - if (!keep_alive) - { - return do_close(); - } - - do_read(); - } - - void - do_close() - { - beast::error_code ec; - stream_.socket().shutdown(tcp::socket::shutdown_send, ec); - - } -}; - class listener : public std::enable_shared_from_this { net::io_context& ioc_; @@ -188,7 +95,7 @@ class listener : public std::enable_shared_from_this } else { - std::make_shared( + std::make_shared( std::move(socket), doc_root_)->run(); }