diff --git a/src/eventia/timer.cpp b/src/eventia/timer.cpp index 61854eb..34edd55 100644 --- a/src/eventia/timer.cpp +++ b/src/eventia/timer.cpp @@ -3,19 +3,29 @@ #include #include #include -#include +#include namespace eve { +namespace { + constexpr const double g_timer_max = 3600.0; +} //unnamed namespace + struct Timer::LocalData { explicit LocalData (const Context& ctx); ev::timer timer; Context context; + double elapsed; + double timeout; + double timer_value; }; Timer::LocalData::LocalData (const Context& ctx) : - context(ctx) + context(ctx), + elapsed(0.0), + timeout(0.0), + timer_value(0.0) { } @@ -32,15 +42,28 @@ Timer::Timer (double delay, const Context& ctx) : Timer::~Timer() noexcept = default; void Timer::on_timer_ev() { - std::cout << "Timer::on_timer_ev()\n"; - m_local->timer.stop(); - this->on_timer(); + double& elapsed = m_local->elapsed; + const double& timer_value = m_local->timer_value; + const double& timeout = m_local->timeout; + + elapsed += timer_value; + if (elapsed >= timeout) { + this->on_timer(); + } + else { + m_local->timer.start(std::min(timeout - elapsed, g_timer_max), 0.0); + } } void Timer::set_timer (double delay) { std::unique_lock lock(*m_local->context.ev_mutex); + m_local->elapsed = 0.0; + m_local->timeout = delay; + m_local->timer_value = std::min(g_timer_max, m_local->timeout); + + m_local->timer.stop(); ev_now_update(*m_local->context.loop); - m_local->timer.start(delay, 0.0); + m_local->timer.start(m_local->timer_value, 0.0); m_local->context.async->send(); }