#include #include #include #include #include #include namespace smo { namespace device { DeviceReattacher::DeviceReattacher( DeviceManager& parent, std::shared_ptr ioThread) : parent(parent), ioThread(ioThread), shouldContinue(false), timer(ioThread->getIoService()) { } void DeviceReattacher::start() { shouldContinue.store(true); scheduleNextTimeout(); } void DeviceReattacher::stop() { shouldContinue.store(false); timer.cancel(); } void DeviceReattacher::scheduleNextTimeout() { if (!shouldContinue.load()) { return; } // Schedule the next timeout using the configured period timer.expires_from_now( boost::posix_time::milliseconds( CONFIG_MRNTT_DEVMGR_REATTACHER_PERIOD_MS)); timer.async_wait( std::bind(&DeviceReattacher::onTimeout, this, std::placeholders::_1)); } void DeviceReattacher::onTimeout(const boost::system::error_code& error) { // Timer was cancelled, which is expected when stopping if (error == boost::asio::error::operation_aborted) { return; } if (error) { std::cerr << "DeviceReattacher: Timer error: " << error.message() << std::endl; return; } if (!shouldContinue.load()) { return; } // Schedule the next timeout scheduleNextTimeout(); } } // namespace device } // namespace smo