5#include <boost/asio/detached.hpp>
6#include <boost/asio/co_spawn.hpp>
7#include <boost/asio/read.hpp>
8#include <boost/asio/write.hpp>
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
66 server.log(std::to_string(
id) +
": " + std::string(s));
71 boost::asio::awaitable<void> Server::listener()
77 co_await acceptor.async_accept(boost::asio::use_awaitable)
80 auto*
const session_ptr = session.get();
86 [ending_session = std::move(session),
this]
88 std::exception_ptr exception_ptr
91 ending_session->cleanup();
93 ending_session->log(
"stop");
94 if (exception_ptr && log_level > 1)
98 std::rethrow_exception(exception_ptr);
100 catch (const std::exception &e)
103 ending_session->log(std::string(
"exception: ") + e.what());
119 std::string endpoint_path
122 log_level(log_level),
123 thread_count(thread_count),
124 thread_pool(thread_count),
126 endpoint_path(std::move(endpoint_path)),
127 endpoint(this->endpoint_path),
128 acceptor(thread_pool, endpoint, false),
129 interrupt_signals(thread_pool, SIGINT, SIGTERM)
136 [
this](
const boost::system::error_code &error,
int signal)
140 if (this->log_level > 0)
142 if (signal == SIGINT)
143 this->
log(
"interrupted by SIGINT");
144 else if (signal == SIGTERM)
145 this->
log(
"interrupted by SIGTERM");
152 boost::asio::co_spawn
156 boost::asio::detached
static void warning(std::string_view message) noexcept
virtual void write(std::string_view message) noexcept
void log(std::string_view s)
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)
Superclass for asio servers.
const std::string endpoint_path
virtual std::unique_ptr< Session > new_session(boost::asio::local::stream_protocol::socket &&socket)=0
void log(std::string_view s)
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)
virtual void cleanup_after_join()
std::string get_time_string_of_now()