Move more connection logic out of main
This commit is contained in:
parent
bb7f09f2e9
commit
135f5aa47d
@ -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();
|
||||||
|
});
|
||||||
|
}
|
@ -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;
|
||||||
|
|
||||||
|
33
main.cpp
33
main.cpp
@ -31,33 +31,20 @@ 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) {
|
||||||
|
slot.disconnect();
|
||||||
|
auto timer = std::make_shared<boost::asio::steady_timer>(io);
|
||||||
|
timer->expires_after(5s);
|
||||||
|
timer->async_wait([&io, &settings, timer](auto) { start(io, settings); });
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
connection->start(ConnectSettings{
|
||||||
.tls = settings.use_tls,
|
.tls = settings.use_tls,
|
||||||
.host = settings.host,
|
.host = settings.host,
|
||||||
.port = settings.service,
|
.port = settings.service,
|
||||||
.verify = settings.tls_hostname,
|
.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);
|
|
||||||
|
|
||||||
timer->expires_after(5s);
|
|
||||||
timer->async_wait(
|
|
||||||
[&io, &settings, timer](auto) { start(io, settings); });
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user