diff --git a/CMakeLists.txt b/CMakeLists.txt index 9fd7daa..b1faffa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,5 +19,7 @@ add_executable(application src/main.cpp src/helper.cpp src/sdk.h src/Session.h - src/Session.cpp) + src/Session.cpp + src/Listener.h + src/Listener.cpp) target_link_libraries(application PRIVATE Threads::Threads) \ No newline at end of file diff --git a/src/Listener.cpp b/src/Listener.cpp new file mode 100644 index 0000000..1ac1092 --- /dev/null +++ b/src/Listener.cpp @@ -0,0 +1,76 @@ +// +// Created by Антон on 11.11.2024. +// + +#include "Listener.h" + +namespace uad +{ +listener::listener(boost::asio::io_context& ioc, + boost::asio::ip::tcp::endpoint endpoint, + const std::shared_ptr& doc_root) + : ioc_(ioc), acceptor_(net::make_strand(ioc)), doc_root_(doc_root) +{ + beast::error_code ec; + + acceptor_.open(endpoint.protocol(), ec); + if (ec) + { + uad::fail(ec, "open"); + return; + } + + acceptor_.set_option(net::socket_base::reuse_address(true), ec); + if (ec) + { + uad::fail(ec, "set_option"); + return; + } + + acceptor_.bind(endpoint, ec); + if (ec) + { + uad::fail(ec, "bind"); + return; + } + + acceptor_.listen( + net::socket_base::max_listen_connections, ec); + if (ec) + { + uad::fail(ec, "listen"); + return; + } +} + +void listener::run() +{ + do_accept(); +} + +void listener::do_accept() +{ + acceptor_.async_accept( + net::make_strand(ioc_), + beast::bind_front_handler( + &listener::on_accept, + shared_from_this())); +} + +void listener::on_accept(beast::error_code ec, tcp::socket socket) +{ + if (ec) + { + uad::fail(ec, "accept"); + return; + } + else + { + std::make_shared( + std::move(socket), + doc_root_)->run(); + } + + do_accept(); +} +} diff --git a/src/Listener.h b/src/Listener.h new file mode 100644 index 0000000..ed300e9 --- /dev/null +++ b/src/Listener.h @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "helpers.h" +#include "Session.h" + +namespace uad +{ +namespace beast = boost::beast; +namespace http = beast::http; +namespace net = boost::asio; +using tcp = boost::asio::ip::tcp; + +class listener : public std::enable_shared_from_this +{ + net::io_context& ioc_; + tcp::acceptor acceptor_; + std::shared_ptr doc_root_; + + public: + listener( + net::io_context& ioc, + tcp::endpoint endpoint, + std::shared_ptr const& doc_root); + + void run(); + + private: + void do_accept(); + + void on_accept(beast::error_code ec, tcp::socket socket); +}; +} diff --git a/src/Session.h b/src/Session.h index 463061f..b786078 100644 --- a/src/Session.h +++ b/src/Session.h @@ -37,7 +37,6 @@ class session : public std::enable_shared_from_this beast::error_code ec, std::size_t bytes_transferred); - void send_response(http::message_generator&& msg); void on_write( diff --git a/src/main.cpp b/src/main.cpp index 871fd38..ecb5a08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,92 +17,13 @@ #include "helpers.h" #include "Session.h" +#include "Listener.h" namespace beast = boost::beast; namespace http = beast::http; namespace net = boost::asio; using tcp = boost::asio::ip::tcp; - -class listener : public std::enable_shared_from_this -{ - net::io_context& ioc_; - tcp::acceptor acceptor_; - std::shared_ptr doc_root_; - - public: - listener( - net::io_context& ioc, - tcp::endpoint endpoint, - std::shared_ptr const& doc_root) - : ioc_(ioc), acceptor_(net::make_strand(ioc)), doc_root_(doc_root) - { - beast::error_code ec; - - acceptor_.open(endpoint.protocol(), ec); - if (ec) - { - uad::fail(ec, "open"); - return; - } - - acceptor_.set_option(net::socket_base::reuse_address(true), ec); - if (ec) - { - uad::fail(ec, "set_option"); - return; - } - - acceptor_.bind(endpoint, ec); - if (ec) - { - uad::fail(ec, "bind"); - return; - } - - acceptor_.listen( - net::socket_base::max_listen_connections, ec); - if (ec) - { - uad::fail(ec, "listen"); - return; - } - } - - void - run() - { - do_accept(); - } - - private: - void - do_accept() - { - acceptor_.async_accept( - net::make_strand(ioc_), - beast::bind_front_handler( - &listener::on_accept, - shared_from_this())); - } - - void - on_accept(beast::error_code ec, tcp::socket socket) - { - if (ec) - { - uad::fail(ec, "accept"); - return; - } - else - { - std::make_shared( - std::move(socket), - doc_root_)->run(); - } - - do_accept(); - } -}; +using namespace uad; int main(int argc, char* argv[]) {