Первая часть успешно завершена

This commit is contained in:
Sithas777 2023-09-13 17:01:44 +03:00
parent 211869c631
commit 67e5fd080b
4 changed files with 51 additions and 6 deletions

View File

@ -15,6 +15,7 @@ using namespace std::literals;
namespace sys = boost::system;
namespace http = boost::beast::http;
using namespace std;
using net::ip::tcp;
// Запрос, тело которого представлено в виде строки
using StringRequest = http::request<http::string_body>;
@ -70,6 +71,7 @@ int main()
const unsigned num_threads = std::thread::hardware_concurrency();
net::io_context ioc(num_threads);
tcp::acceptor acceptor(net::make_strand(ioc));
// Подписываемся на сигналы и при их получении завершаем работу сервера
net::signal_set signals(ioc, SIGINT, SIGTERM);

View File

@ -8,9 +8,12 @@
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include "session.h"
namespace http_server
{
namespace net = boost::asio;
namespace sys = boost::system;
using tcp = net::ip::tcp;
namespace beast = boost::beast;
namespace http = beast::http;
@ -28,13 +31,41 @@ class Listener : public std::enable_shared_from_this<Listener<RequestHandler>>
const tcp::endpoint& endpoint,
Handler&& request_handler)
: ioc_(ioc),
acceptor_(net::make_strand(ioc)),
request_handler_(std::forward<Handler>(request_handler))
acceptor_(net::make_strand(ioc)),
request_handler_(std::forward<Handler>(request_handler))
{
acceptor_.open(endpoint.protocol());
acceptor_.set_option(net::socket_base::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen(net::socket_base::max_listen_connections);
}
void Run()
{
DoAccept();
}
private:
void DoAccept()
{
acceptor_.async_accept(net::make_strand(ioc_),
beast::bind_front_handler(&Listener::DoAccept,
this->shared_from_this()));
}
void OnAccept(sys::error_code ec, tcp::socket socket)
{
if (ec)
{
acceptor_.open(endpoint.protocol());
acceptor_.set_option(net::socket_base::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen(net::socket_base::max_listen_connections);
return ReportError(ec, "accept"sv);
}
AsyncRunSession(std::move(socket));
DoAccept();
}
void AsyncRunSession(tcp::socket&& socket)
{}
};
} // namespace http_server

View File

@ -1,8 +1,12 @@
#include "http_server.h"
#include "session_base.h"
#include <boost/asio/dispatch.hpp>
#include <iostream>
namespace http_server
{
void ReportError(beast::error_code ec, std::string_view what) {
std::cerr << what << ": "sv << ec.message() << std::endl;
}
} // namespace http_server

View File

@ -3,6 +3,9 @@
// boost.beast будет использовать std::string_view вместо boost::string_view
#define BOOST_BEAST_USE_STD_STRING_VIEW
#include <iostream>
#include <string_view>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/strand.hpp>
#include <boost/beast/core.hpp>
@ -14,6 +17,11 @@ namespace net = boost::asio;
using tcp = net::ip::tcp;
namespace beast = boost::beast;
namespace http = beast::http;
using namespace std::literals;
void ReportError(beast::error_code ec, std::string_view what);
class SessionBase {
// Напишите недостающий код, используя информацию из урока
};