Первая часть успешно завершена
This commit is contained in:
parent
211869c631
commit
67e5fd080b
2
main.cpp
2
main.cpp
@ -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);
|
||||
|
@ -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;
|
||||
@ -36,5 +39,33 @@ class Listener : public std::enable_shared_from_this<Listener<RequestHandler>>
|
||||
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)
|
||||
{
|
||||
return ReportError(ec, "accept"sv);
|
||||
}
|
||||
|
||||
AsyncRunSession(std::move(socket));
|
||||
|
||||
DoAccept();
|
||||
}
|
||||
|
||||
void AsyncRunSession(tcp::socket&& socket)
|
||||
{}
|
||||
};
|
||||
} // namespace http_server
|
@ -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
|
||||
|
@ -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 {
|
||||
// Напишите недостающий код, используя информацию из урока
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user