Рефакторинг названий

This commit is contained in:
Антон 2024-11-11 20:01:43 +03:00
parent 8b703364b1
commit 62ce10bba9
7 changed files with 47 additions and 58 deletions

View File

@ -1,12 +1,8 @@
//
// Created by Антон on 11.11.2024.
//
#include "Listener.h"
namespace uad
{
listener::listener(boost::asio::io_context& ioc,
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)
@ -43,21 +39,21 @@ listener::listener(boost::asio::io_context& ioc,
}
}
void listener::run()
void Listener::Run()
{
do_accept();
}
void listener::do_accept()
void Listener::DoAccept()
{
acceptor_.async_accept(
net::make_strand(ioc_),
beast::bind_front_handler(
&listener::on_accept,
&Listener::on_accept,
shared_from_this()));
}
void listener::on_accept(beast::error_code ec, tcp::socket socket)
void Listener::OnAccept(beast::error_code ec, tcp::socket socket)
{
if (ec)
{

View File

@ -23,23 +23,23 @@ namespace http = beast::http;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
class listener : public std::enable_shared_from_this<listener>
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(
Listener(
net::io_context& ioc,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root);
void run();
void Run();
private:
void do_accept();
void DoAccept();
void on_accept(beast::error_code ec, tcp::socket socket);
void OnAccept(beast::error_code ec, tcp::socket socket);
};
}

View File

@ -2,20 +2,20 @@
namespace uad
{
session::session(
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()
void Session::Run()
{
net::dispatch(stream_.get_executor(),
beast::bind_front_handler(
&session::do_read,
&Session::DoRead,
shared_from_this()));
}
void session::do_read()
void Session::DoRead()
{
req_ = {};
@ -23,27 +23,27 @@ void session::do_read()
http::async_read(stream_, buffer_, req_,
beast::bind_front_handler(
&session::on_read,
&Session::OnRead,
shared_from_this()));
}
void session::on_read(
void Session::OnRead(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if (ec == http::error::end_of_stream)
return do_close();
return DoClose();
if (ec)
return fail(ec, "read");
return Fail(ec, "read");
send_response(
handle_request(*doc_root_, std::move(req_)));
SendResponse(
HandleRequest(*doc_root_, std::move(req_)));
}
void session::send_response(http::message_generator&& msg)
void Session::SendResponse(http::message_generator&& msg)
{
bool keep_alive = msg.keep_alive();
@ -51,10 +51,10 @@ void session::send_response(http::message_generator&& msg)
stream_,
std::move(msg),
beast::bind_front_handler(
&session::on_write, shared_from_this(), keep_alive));
&Session::OnWrite, shared_from_this(), keep_alive));
}
void session::on_write(
void Session::OnWrite(
bool keep_alive,
beast::error_code ec,
std::size_t bytes_transferred)
@ -62,17 +62,17 @@ void session::on_write(
boost::ignore_unused(bytes_transferred);
if (ec)
return uad::fail(ec, "write");
return uad::Fail(ec, "write");
if (!keep_alive)
{
return do_close();
return DoClose();
}
do_read();
DoRead();
}
void session::do_close()
void Session::DoClose()
{
beast::error_code ec;
stream_.socket().shutdown(tcp::socket::shutdown_send, ec);

View File

@ -17,7 +17,7 @@ namespace http = beast::http;
using tcp = boost::asio::ip::tcp;
namespace net = boost::asio;
class session : public std::enable_shared_from_this<session>
class Session : public std::enable_shared_from_this<Session>
{
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
@ -25,25 +25,25 @@ class session : public std::enable_shared_from_this<session>
http::request<http::string_body> req_;
public:
session(
Session(
tcp::socket&& socket,
std::shared_ptr<std::string const> const& doc_root);
void run();
void Run();
void do_read();
void DoRead();
void on_read(
void OnRead(
beast::error_code ec,
std::size_t bytes_transferred);
void send_response(http::message_generator&& msg);
void SendResponse(http::message_generator&& msg);
void on_write(
void OnWrite(
bool keep_alive,
beast::error_code ec,
std::size_t bytes_transferred);
void do_close();
void DoClose();
};
}

View File

@ -2,7 +2,7 @@
namespace uad
{
beast::string_view mime_type(beast::string_view path)
beast::string_view MimeType(beast::string_view path)
{
using beast::iequals;
auto const ext = [&path]
@ -36,7 +36,7 @@ beast::string_view mime_type(beast::string_view path)
return "application/text";
}
std::string path_cat(
std::string PathCat(
beast::string_view base,
beast::string_view path)
{
@ -60,7 +60,7 @@ std::string path_cat(
return result;
}
void fail(beast::error_code ec, char const* what)
void Fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}

View File

@ -22,14 +22,14 @@ namespace http = beast::http;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
beast::string_view mime_type(beast::string_view path);
beast::string_view MimeType(beast::string_view path);
std::string path_cat(
std::string PathCat(
beast::string_view base,
beast::string_view path);
template<class Body, class Allocator>
http::message_generator handle_request(
http::message_generator HandleRequest(
beast::string_view doc_root,
http::request<Body, http::basic_fields<Allocator>>&& req)
{
@ -78,7 +78,7 @@ http::message_generator handle_request(
req.target().find("..") != beast::string_view::npos)
return bad_request("Illegal request-target");
std::string path = path_cat(doc_root, req.target());
std::string path = PathCat(doc_root, req.target());
if (req.target().back() == '/')
path.append("index.html");
@ -98,7 +98,7 @@ http::message_generator handle_request(
{
http::response<http::empty_body> res {http::status::ok, req.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, mime_type(path));
res.set(http::field::content_type, MimeType(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
return res;
@ -109,11 +109,11 @@ http::message_generator handle_request(
std::make_tuple(std::move(body)),
std::make_tuple(http::status::ok, req.version())};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, mime_type(path));
res.set(http::field::content_type, MimeType(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
return res;
}
void fail(beast::error_code ec, char const* what);
void Fail(beast::error_code ec, char const* what);
}

View File

@ -1,22 +1,15 @@
#include "sdk.h"
#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"
#include "Listener.h"
namespace beast = boost::beast;
@ -42,10 +35,10 @@ int main(int argc, char* argv[])
net::io_context ioc {threads};
std::make_shared<listener>(
std::make_shared<Listener>(
ioc,
tcp::endpoint {address, port},
doc_root)->run();
doc_root)->Run();
std::vector<std::thread> v;
v.reserve(threads - 1);