Move more connection logic out of main

This commit is contained in:
Eric Mertens 2025-01-26 14:51:44 -08:00
parent bb7f09f2e9
commit 135f5aa47d
3 changed files with 38 additions and 33 deletions

View File

@ -387,7 +387,6 @@ auto build_ssl_context(
} }
auto Connection::connect( auto Connection::connect(
boost::asio::io_context &io,
ConnectSettings settings ConnectSettings settings
) -> boost::asio::awaitable<void> ) -> boost::asio::awaitable<void>
{ {
@ -397,10 +396,9 @@ auto Connection::connect(
const auto self = shared_from_this(); const auto self = shared_from_this();
const size_t irc_buffer_size = 32'768; const size_t irc_buffer_size = 32'768;
{ {
// Name resolution // Name resolution
auto resolver = boost::asio::ip::tcp::resolver{io}; auto resolver = boost::asio::ip::tcp::resolver{stream_.get_executor()};
const auto endpoints = co_await resolver.async_resolve(settings.host, std::to_string(settings.port), boost::asio::use_awaitable); const auto endpoints = co_await resolver.async_resolve(settings.host, std::to_string(settings.port), boost::asio::use_awaitable);
// Connect to the IRC server // Connect to the IRC server
@ -460,3 +458,24 @@ auto Connection::connect(
watchdog_timer_.cancel(); watchdog_timer_.cancel();
stream_.close(); stream_.close();
} }
auto Connection::start(ConnectSettings settings) -> void
{
boost::asio::co_spawn(
stream_.get_executor(), connect(std::move(settings)),
[self = shared_from_this()](std::exception_ptr e) {
try
{
if (e)
std::rethrow_exception(e);
BOOST_LOG_TRIVIAL(debug) << "DISCONNECTED";
}
catch (const std::exception &e)
{
BOOST_LOG_TRIVIAL(debug) << "TERMINATED: " << e.what();
}
self->sig_disconnect();
});
}

View File

@ -61,6 +61,8 @@ private:
auto watchdog() -> void; auto watchdog() -> void;
auto watchdog_activity() -> void; auto watchdog_activity() -> void;
auto connect(ConnectSettings settings) -> boost::asio::awaitable<void>;
public: public:
/// Build and send well-formed IRC message from individual parameters /// Build and send well-formed IRC message from individual parameters
auto write_irc(std::string) -> void; auto write_irc(std::string) -> void;
@ -84,10 +86,7 @@ public:
return stream_.get_executor(); return stream_.get_executor();
} }
auto connect( auto start(ConnectSettings) -> void;
boost::asio::io_context &io,
ConnectSettings settings
) -> boost::asio::awaitable<void>;
auto close() -> void; auto close() -> void;

View File

@ -31,34 +31,21 @@ auto start(boost::asio::io_context &io, const Settings &settings) -> void
} }
}); });
boost::asio::co_spawn( connection->sig_disconnect.connect_extended(
io, connection->connect(io, ConnectSettings{ [&io, &settings](auto &slot) {
.tls = settings.use_tls, slot.disconnect();
.host = settings.host,
.port = settings.service,
.verify = settings.tls_hostname,
}),
[&io, &settings, connection](std::exception_ptr e) {
try
{
if (e)
std::rethrow_exception(e);
BOOST_LOG_TRIVIAL(debug) << "DISCONNECTED";
}
catch (const std::exception &e)
{
BOOST_LOG_TRIVIAL(debug) << "TERMINATED: " << e.what();
}
connection->sig_disconnect();
auto timer = std::make_shared<boost::asio::steady_timer>(io); auto timer = std::make_shared<boost::asio::steady_timer>(io);
timer->expires_after(5s); timer->expires_after(5s);
timer->async_wait( timer->async_wait([&io, &settings, timer](auto) { start(io, settings); });
[&io, &settings, timer](auto) { start(io, settings); }); }
}); );
connection->start(ConnectSettings{
.tls = settings.use_tls,
.host = settings.host,
.port = settings.service,
.verify = settings.tls_hostname,
});
} }
auto get_settings() -> Settings auto get_settings() -> Settings