Use callback function instead of class

This commit is contained in:
badaix 2025-02-16 22:04:33 +01:00 committed by Johannes Pohl
parent efd050a716
commit f0985cbce4
4 changed files with 40 additions and 33 deletions

View file

@ -36,7 +36,7 @@ using namespace std;
namespace streamreader
{
Watchdog::Watchdog(const boost::asio::any_io_executor& executor, WatchdogListener* listener) : timer_(executor), listener_(listener)
Watchdog::Watchdog(const boost::asio::any_io_executor& executor) : timer_(executor)
{
}
@ -47,9 +47,10 @@ Watchdog::~Watchdog()
}
void Watchdog::start(const std::chrono::milliseconds& timeout)
void Watchdog::start(const std::chrono::milliseconds& timeout, TimeoutHandler&& handler)
{
LOG(INFO, LOG_TAG) << "Starting watchdog, timeout: " << std::chrono::duration_cast<std::chrono::seconds>(timeout).count() << "s\n";
handler_ = std::move(handler);
timeout_ms_ = timeout;
trigger();
}
@ -70,7 +71,8 @@ void Watchdog::trigger()
if (!ec)
{
LOG(INFO, LOG_TAG) << "Timed out: " << std::chrono::duration_cast<std::chrono::seconds>(timeout_ms_).count() << "s\n";
listener_->onTimeout(*this, timeout_ms_);
if (handler_)
handler_(timeout_ms_);
}
});
}