|
|
@ -1,10 +1,12 @@ |
|
|
|
#ifndef _ASYNCO_TIMERS_ |
|
|
|
#ifndef _TIMERS_ |
|
|
|
#define _ASYNCO_TIMERS_ |
|
|
|
#define _TIMERS_ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "asynco.hpp" |
|
|
|
#include <chrono> |
|
|
|
#include <chrono> |
|
|
|
using namespace std; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "asynco.hpp" |
|
|
|
using namespace std; |
|
|
|
|
|
|
|
using namespace marcelb; |
|
|
|
|
|
|
|
using namespace asynco; |
|
|
|
|
|
|
|
|
|
|
|
namespace marcelb { |
|
|
|
namespace marcelb { |
|
|
|
namespace asynco { |
|
|
|
namespace asynco { |
|
|
@ -13,13 +15,21 @@ namespace asynco { |
|
|
|
* Get the time in ms from the epoch |
|
|
|
* Get the time in ms from the epoch |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
int64_t rtime_ms(); |
|
|
|
int64_t rtime_ms() { |
|
|
|
|
|
|
|
return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now() |
|
|
|
|
|
|
|
.time_since_epoch()) |
|
|
|
|
|
|
|
.count(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get the time in us from the epoch |
|
|
|
* Get the time in us from the epoch |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
int64_t rtime_us(); |
|
|
|
int64_t rtime_us() { |
|
|
|
|
|
|
|
return chrono::duration_cast<chrono::microseconds>(chrono::system_clock::now() |
|
|
|
|
|
|
|
.time_since_epoch()) |
|
|
|
|
|
|
|
.count(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Core timer class for construct time async functions |
|
|
|
* Core timer class for construct time async functions |
|
|
@ -35,40 +45,71 @@ class timer { |
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* A method to assign a callback wrapper and a reinitialization algorithm |
|
|
|
* A method to assign a callback wrapper and a reinitialization algorithm |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void init(); |
|
|
|
void init() { |
|
|
|
|
|
|
|
st.async_wait( [this] (const boost::system::error_code&) { |
|
|
|
|
|
|
|
if (!_stop) { |
|
|
|
|
|
|
|
callback(); |
|
|
|
|
|
|
|
if (repeate) { |
|
|
|
|
|
|
|
st = boost::asio::steady_timer(_asynco_engine.io_context, boost::asio::chrono::milliseconds(time)); |
|
|
|
|
|
|
|
init(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ticks++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The constructor creates the steady_timer and accompanying variables and runs a method to initialize the timer |
|
|
|
* The constructor creates the steady_timer and accompanying variables and runs a method to initialize the timer |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
timer (function<void()> _callback, uint64_t _time, bool _repeate); |
|
|
|
timer (function<void()> _callback, uint64_t _time, bool _repeate) : |
|
|
|
|
|
|
|
st(_asynco_engine.io_context, boost::asio::chrono::milliseconds(_time)), |
|
|
|
|
|
|
|
_stop(false), |
|
|
|
|
|
|
|
repeate(_repeate), |
|
|
|
|
|
|
|
callback(_callback), |
|
|
|
|
|
|
|
time(_time) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
init(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Stop timer |
|
|
|
* Stop timer |
|
|
|
* The stop flag is set and timer remove it from the queue |
|
|
|
* The stop flag is set and timer remove it from the queue |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void stop(); |
|
|
|
void stop() { |
|
|
|
|
|
|
|
_stop = true; |
|
|
|
|
|
|
|
st.cancel(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Run callback now |
|
|
|
* Run callback now |
|
|
|
* Forces the callback function to run independently of the timer |
|
|
|
* Forces the callback function to run independently of the timer |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void now(); |
|
|
|
void now() { |
|
|
|
|
|
|
|
st.cancel(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get the number of times the timer callback was runned |
|
|
|
* Get the number of times the timer callback was runned |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
uint64_t ticks(); |
|
|
|
uint64_t ticks() { |
|
|
|
|
|
|
|
return _ticks; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The logic status of the timer stop state |
|
|
|
* The logic status of the timer stop state |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
bool stoped(); |
|
|
|
bool stoped() { |
|
|
|
|
|
|
|
return _stop; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The destructor stops the timer |
|
|
|
* The destructor stops the timer |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
~timer(); |
|
|
|
~timer() { |
|
|
|
|
|
|
|
stop(); |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -78,37 +119,49 @@ class periodic { |
|
|
|
shared_ptr<timer> _timer; |
|
|
|
shared_ptr<timer> _timer; |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Constructor initializes a shared pointer of type timer |
|
|
|
* Constructor initializes a shared pointer of type timer |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
periodic(function<void()> callback, uint64_t time); |
|
|
|
periodic(function<void()> callback, uint64_t time) :
|
|
|
|
|
|
|
|
_timer(make_shared<timer> (callback, time, true)) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Stop periodic |
|
|
|
* Stop periodic |
|
|
|
* The stop flag is set and periodic remove it from the queue |
|
|
|
* The stop flag is set and periodic remove it from the queue |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void stop(); |
|
|
|
void stop() { |
|
|
|
|
|
|
|
_timer->stop(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Run callback now |
|
|
|
* Run callback now |
|
|
|
* Forces the callback function to run independently of the periodic |
|
|
|
* Forces the callback function to run independently of the periodic |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void now(); |
|
|
|
void now() { |
|
|
|
|
|
|
|
_timer->now(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get the number of times the periodic callback was runned |
|
|
|
* Get the number of times the periodic callback was runned |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
uint64_t ticks(); |
|
|
|
uint64_t ticks() { |
|
|
|
|
|
|
|
return _timer->ticks(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The logic status of the periodic stop state |
|
|
|
* The logic status of the periodic stop state |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
bool stoped(); |
|
|
|
bool stoped() { |
|
|
|
|
|
|
|
return _timer->stoped(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The destructor stops the periodic |
|
|
|
* The destructor stops the periodic |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
~periodic(); |
|
|
|
~periodic() { |
|
|
|
|
|
|
|
stop(); |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -118,43 +171,52 @@ class delayed { |
|
|
|
shared_ptr<timer> _timer; |
|
|
|
shared_ptr<timer> _timer; |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Constructor initializes a shared pointer of type timer |
|
|
|
* Constructor initializes a shared pointer of type timer |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
delayed(function<void()> callback, uint64_t time); |
|
|
|
delayed(function<void()> callback, uint64_t time) :
|
|
|
|
|
|
|
|
_timer(make_shared<timer> (callback, time, false)) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Stop delayed |
|
|
|
* Stop delayed |
|
|
|
* The stop flag is set and delayed remove it from the queue |
|
|
|
* The stop flag is set and delayed remove it from the queue |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void stop(); |
|
|
|
void stop() { |
|
|
|
|
|
|
|
_timer->stop(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Run callback now |
|
|
|
* Run callback now |
|
|
|
* Forces the callback function to run independently of the delayed |
|
|
|
* Forces the callback function to run independently of the delayed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void now(); |
|
|
|
void now() { |
|
|
|
|
|
|
|
_timer->now(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get is the delayed callback runned |
|
|
|
* Get is the delayed callback runned |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
bool expired(); |
|
|
|
bool expired() { |
|
|
|
|
|
|
|
return bool(_timer->ticks()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The logic status of the delayed stop state |
|
|
|
* The logic status of the delayed stop state |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
bool stoped(); |
|
|
|
bool stoped() { |
|
|
|
|
|
|
|
return _timer->stoped(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The destructor stops the delayed |
|
|
|
* The destructor stops the delayed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
~delayed(); |
|
|
|
~delayed() { |
|
|
|
|
|
|
|
stop(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
shared_ptr<periodic> Periodic(function<void()> callback, uint64_t time); |
|
|
|
|
|
|
|
shared_ptr<delayed> Delayed(function<void()> callback, uint64_t time); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|