Add support for long timeouts in timers
This commit is contained in:
parent
034020e152
commit
73ec4446ea
1 changed files with 29 additions and 6 deletions
|
@ -3,19 +3,29 @@
|
|||
#include <ev++.h>
|
||||
#include <mutex>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
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();
|
||||
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<std::mutex> 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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue