Распил сервера - вынесение helpers
This commit is contained in:
parent
e166deda11
commit
315c72c0a6
@ -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)
|
@ -1,3 +1,80 @@
|
||||
//
|
||||
// Created by Антон on 08.11.2024.
|
||||
//
|
||||
#include "Session.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
session::session(
|
||||
tcp::socket&& socket,
|
||||
std::shared_ptr<std::string const> 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);
|
||||
}
|
||||
}
|
||||
|
@ -8,11 +8,14 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#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<session>
|
||||
{
|
||||
@ -24,81 +27,24 @@ class session : public std::enable_shared_from_this<session>
|
||||
public:
|
||||
session(
|
||||
tcp::socket&& socket,
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: stream_(std::move(socket)), doc_root_(doc_root)
|
||||
{
|
||||
}
|
||||
std::shared_ptr<std::string const> 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();
|
||||
};
|
||||
}
|
||||
|
97
src/main.cpp
97
src/main.cpp
@ -16,106 +16,13 @@
|
||||
#include <vector>
|
||||
|
||||
#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<session>
|
||||
{
|
||||
beast::tcp_stream stream_;
|
||||
beast::flat_buffer buffer_;
|
||||
std::shared_ptr<std::string const> doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
|
||||
public:
|
||||
session(
|
||||
tcp::socket&& socket,
|
||||
std::shared_ptr<std::string const> 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<listener>
|
||||
{
|
||||
net::io_context& ioc_;
|
||||
@ -188,7 +95,7 @@ class listener : public std::enable_shared_from_this<listener>
|
||||
}
|
||||
else
|
||||
{
|
||||
std::make_shared<session>(
|
||||
std::make_shared<uad::session>(
|
||||
std::move(socket),
|
||||
doc_root_)->run();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user