Распил listeners

This commit is contained in:
Антон 2024-11-11 19:49:47 +03:00
parent 315c72c0a6
commit 8b703364b1
5 changed files with 126 additions and 83 deletions

View File

@ -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)

76
src/Listener.cpp Normal file
View File

@ -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<const std::string>& 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<uad::session>(
std::move(socket),
doc_root_)->run();
}
do_accept();
}
}

45
src/Listener.h Normal file
View File

@ -0,0 +1,45 @@
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/strand.hpp>
#include <boost/config.hpp>
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#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<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
std::shared_ptr<std::string const> doc_root_;
public:
listener(
net::io_context& ioc,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root);
void run();
private:
void do_accept();
void on_accept(beast::error_code ec, tcp::socket socket);
};
}

View File

@ -37,7 +37,6 @@ class session : public std::enable_shared_from_this<session>
beast::error_code ec,
std::size_t bytes_transferred);
void send_response(http::message_generator&& msg);
void on_write(

View File

@ -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<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
std::shared_ptr<std::string const> doc_root_;
public:
listener(
net::io_context& ioc,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> 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<uad::session>(
std::move(socket),
doc_root_)->run();
}
do_accept();
}
};
using namespace uad;
int main(int argc, char* argv[])
{