4#include <boost/asio/detached.hpp>
5#include <boost/asio/co_spawn.hpp>
6#include <boost/asio/read.hpp>
7#include <boost/asio/write.hpp>
15 logger.log(endpoint_path +
": " + message);
25 boost::asio::local::stream_protocol::socket &&socket
28 id(server.session_id++),
29 socket(std::move(socket)),
30 strand(server.thread_pool.get_executor())
42 const size_t result =
co_await boost::asio::async_read
45 boost::asio::buffer(buffer.data + offset, size),
46 boost::asio::use_awaitable
49 buffer.index = offset;
56 co_await boost::asio::async_write
59 boost::asio::buffer(buffer.data, buffer.index),
60 boost::asio::use_awaitable
68 server.log(std::to_string(
id) +
": " + message);
77 boost::asio::awaitable<void> Server::listener()
83 co_await acceptor.async_accept(boost::asio::use_awaitable)
86 auto*
const session_ptr = session.get();
92 [ending_session = std::move(session),
this]
94 std::exception_ptr exception_ptr
97 ending_session->cleanup();
99 ending_session->log(
"stop");
100 if (exception_ptr && log_level > 1)
104 std::rethrow_exception(exception_ptr);
106 catch (const std::exception &e)
109 ending_session->log(std::string(
"exception: ") + e.what());
125 std::string endpoint_path
128 log_level(log_level),
129 thread_count(thread_count),
130 thread_pool(thread_count),
132 endpoint_path(std::move(endpoint_path)),
133 endpoint(this->endpoint_path),
134 acceptor(thread_pool, endpoint, false),
135 interrupt_signals(thread_pool, SIGINT, SIGTERM)
142 [
this](
const boost::system::error_code &error,
int signal)
146 if (this->log_level > 0)
148 if (signal == SIGINT)
149 this->
log(
"interrupted by SIGINT");
150 else if (signal == SIGTERM)
151 this->
log(
"interrupted by SIGTERM");
158 boost::asio::co_spawn
162 boost::asio::detached
static void warning(const std::string &message) noexcept
boost::asio::awaitable< void > write_buffer()
Session(Server &server, boost::asio::local::stream_protocol::socket &&socket)
boost::asio::awaitable< size_t > read_buffer(size_t offset, size_t size)
void log(const std::string &message) noexcept override
Superclass for asio servers.
const std::string endpoint_path
virtual std::unique_ptr< Session > new_session(boost::asio::local::stream_protocol::socket &&socket)=0
boost::asio::local::stream_protocol::acceptor acceptor
boost::asio::signal_set interrupt_signals
boost::asio::thread_pool thread_pool
Server(Logger &logger, int log_level, int thread_count, std::string endpoint_path)
void log(const std::string &message) noexcept override
virtual void cleanup_after_join()