repetitive_task.cpp 642 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <network/repetitive_task.hpp>
#include <network/ioservice.hpp>
#include <logging/logging.hpp>
#include <boost/chrono.hpp>

Repeater::Repeater(const FuncType& f,
                   const DurationType& duration):
  f(f),
  timer(IoService::get()),
  duration(duration)
{
  this->repeat_forever();
}

void Repeater::repeat_forever()
{
  this->timer.expires_at(std::chrono::steady_clock::now() + this->duration);

  auto cb = [this](const boost::system::error_code& ec)
    {
      if (ec)
        log_error("Async wait failed: " << ec);
      else if (f() == true)
        this->repeat_forever();
    };
  this->timer.async_wait(cb);
}