#include "watchdog_thread.hpp" #include "connection.hpp" #include "irc_parse_thread.hpp" #include "write_irc.hpp" #include #include #include using namespace std::chrono_literals; WatchdogThread::WatchdogThread(Connection& connection) : connection_{connection} , timer_{connection.get_executor()} , tried_ping{false} { } auto WatchdogThread::on_activity() -> void { tried_ping = false; timer_.expires_from_now(30s); } auto WatchdogThread::timeout_token() { return [weak = weak_from_this()](auto const& error) { if (not error) { if (auto self = weak.lock()) { self->on_timeout(); } } }; } auto WatchdogThread::on_timeout() -> void { if (tried_ping) { connection_.close(); } else { send_ping(connection_, "watchdog"); tried_ping = true; timer_.expires_from_now(30s); timer_.async_wait(timeout_token()); } } auto WatchdogThread::on_connect() -> void { on_activity(); timer_.async_wait(timeout_token()); } auto WatchdogThread::on_disconnect() -> void { timer_.cancel(); } auto WatchdogThread::start(Connection& connection) -> void { auto const thread = std::make_shared(connection); connection.add_listener([thread](auto&) { thread->on_connect(); }); connection.add_listener([thread](auto&) { thread->on_disconnect(); }); connection.add_listener([thread](auto&) { thread->on_activity(); }); }