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 std::string s = endpoint_path +
": ";
27 boost::asio::local::stream_protocol::socket &&socket
30 id(server.session_id++),
31 socket(std::move(socket)),
32 strand(server.thread_pool.get_executor())
44 const size_t result =
co_await boost::asio::async_read
47 boost::asio::buffer(buffer.data + offset, size),
48 boost::asio::use_awaitable
51 buffer.index = offset;
58 co_await boost::asio::async_write
61 boost::asio::buffer(buffer.data, buffer.index),
62 boost::asio::use_awaitable
70 std::string s = std::to_string(
id) +
": ";
81 boost::asio::awaitable<void> Server::listener()
87 co_await acceptor.async_accept(boost::asio::use_awaitable)
90 auto*
const session_ptr = session.get();
96 [ending_session = std::move(session),
this]
98 std::exception_ptr exception_ptr
101 ending_session->cleanup();
103 ending_session->log(
"stop");
104 if (exception_ptr && log_level > 1)
108 std::rethrow_exception(exception_ptr);
110 catch (const std::exception &e)
113 ending_session->log(std::string(
"exception: ") + e.what());
129 std::string endpoint_path
132 log_level(log_level),
133 thread_count(thread_count),
134 thread_pool(thread_count),
136 endpoint_path(std::move(endpoint_path)),
137 endpoint(this->endpoint_path),
138 acceptor(thread_pool, endpoint, false),
139 interrupt_signals(thread_pool, SIGINT, SIGTERM)
146 [
this](
const boost::system::error_code &error,
int signal)
150 if (this->log_level > 0)
152 if (signal == SIGINT)
153 this->
log(
"interrupted by SIGINT");
154 else if (signal == SIGTERM)
155 this->
log(
"interrupted by SIGTERM");
162 boost::asio::co_spawn
166 boost::asio::detached
static void warning(beman::cstring_view 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(beman::cstring_view 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(beman::cstring_view message) noexcept override
virtual void cleanup_after_join()