From 135f5aa47dfd416dec14356665292a7a9ae7d387 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sun, 26 Jan 2025 14:51:44 -0800 Subject: [PATCH] Move more connection logic out of main --- connection.cpp | 25 ++++++++++++++++++++++--- connection.hpp | 7 +++---- main.cpp | 39 +++++++++++++-------------------------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/connection.cpp b/connection.cpp index 1baf876..9c4fb7e 100644 --- a/connection.cpp +++ b/connection.cpp @@ -387,7 +387,6 @@ auto build_ssl_context( } auto Connection::connect( - boost::asio::io_context &io, ConnectSettings settings ) -> boost::asio::awaitable { @@ -397,10 +396,9 @@ auto Connection::connect( const auto self = shared_from_this(); const size_t irc_buffer_size = 32'768; - { // 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); // Connect to the IRC server @@ -460,3 +458,24 @@ auto Connection::connect( watchdog_timer_.cancel(); 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(); + }); +} \ No newline at end of file diff --git a/connection.hpp b/connection.hpp index 1ce2b8e..a74f8e6 100644 --- a/connection.hpp +++ b/connection.hpp @@ -61,6 +61,8 @@ private: auto watchdog() -> void; auto watchdog_activity() -> void; + auto connect(ConnectSettings settings) -> boost::asio::awaitable; + public: /// Build and send well-formed IRC message from individual parameters auto write_irc(std::string) -> void; @@ -84,10 +86,7 @@ public: return stream_.get_executor(); } - auto connect( - boost::asio::io_context &io, - ConnectSettings settings - ) -> boost::asio::awaitable; + auto start(ConnectSettings) -> void; auto close() -> void; diff --git a/main.cpp b/main.cpp index 874de48..9936887 100644 --- a/main.cpp +++ b/main.cpp @@ -31,34 +31,21 @@ auto start(boost::asio::io_context &io, const Settings &settings) -> void } }); - boost::asio::co_spawn( - io, connection->connect(io, ConnectSettings{ - .tls = settings.use_tls, - .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(); - + connection->sig_disconnect.connect_extended( + [&io, &settings](auto &slot) { + slot.disconnect(); auto timer = std::make_shared(io); - timer->expires_after(5s); - timer->async_wait( - [&io, &settings, timer](auto) { start(io, settings); }); - }); + timer->async_wait([&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