xbot/watchdog_thread.hpp
2024-03-03 12:27:36 -08:00

48 lines
1.1 KiB
C++

#pragma once
#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <memory>
class Connection;
/**
* @brief Watch for connection activity and disconnect on stall
*
* The thread will send a ping if no message is received in the
* last TIMEOUT seconds. After another period of no messages
* the thread will disconnect the connection.
*
*/
class WatchdogThread : std::enable_shared_from_this<WatchdogThread>
{
Connection& connection_;
boost::asio::steady_timer timer_;
/// @brief Set true and ping sent and false when reply received
bool stalled_;
const std::chrono::steady_clock::duration TIMEOUT = std::chrono::seconds{30};
/// @brief Start the timer
/// @return
auto start_timer();
/// @brief
auto on_activity() -> void;
/// @brief
auto on_timeout() -> void;
/// @brief callback for ConnectEvent event
auto on_connect() -> void;
/// @brief callback for DisconnectEvent event
auto on_disconnect() -> void;
public:
WatchdogThread(Connection& connection);
static auto start(Connection& connection) -> void;
};