xbot/watchdog_thread.hpp

48 lines
1.1 KiB
C++
Raw Normal View History

2023-11-25 09:22:55 -08:00
#pragma once
2023-11-27 14:12:20 -08:00
#include <boost/asio/steady_timer.hpp>
2024-03-03 12:27:36 -08:00
#include <chrono>
2023-11-27 14:12:20 -08:00
#include <memory>
2023-11-25 09:22:55 -08:00
class Connection;
2023-11-27 14:12:20 -08:00
2024-03-03 12:27:36 -08:00
/**
* @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.
*
*/
2023-11-27 14:12:20 -08:00
class WatchdogThread : std::enable_shared_from_this<WatchdogThread>
{
Connection& connection_;
boost::asio::steady_timer timer_;
2024-03-03 12:27:36 -08:00
/// @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
2023-11-27 14:12:20 -08:00
auto on_activity() -> void;
2024-03-03 12:27:36 -08:00
/// @brief
2023-11-27 14:12:20 -08:00
auto on_timeout() -> void;
2024-03-03 12:27:36 -08:00
/// @brief callback for ConnectEvent event
2023-11-27 14:12:20 -08:00
auto on_connect() -> void;
2024-03-03 12:27:36 -08:00
/// @brief callback for DisconnectEvent event
2023-11-27 14:12:20 -08:00
auto on_disconnect() -> void;
public:
WatchdogThread(Connection& connection);
static auto start(Connection& connection) -> void;
};