///////////////////////////////////////////////////////////
///////////////// Have an itch? Scratch it! ///////////////
///////////////////////// SCRATCH /////////////////////////
///////////////////// A MUD Server ////////////////////
///////////////////// By: Jared Devall ////////////////////
///////////////////// Thanks: ////////////////////
///////////////////// DIKU/Merc/ROM ////////////////////
///////////////////// Aetas/Deus Gang ////////////////////
///////////////////// Beej ////////////////////
///////////////////////////////////////////////////////////
#include <sys/time.h>
#include <time.h>
#include "timer.h"
using namespace std;
Timer::Timer( int interval, int times ) {
_interval = interval;
_times = times;
_enabled = true;
Reset();
}
bool Timer::Fire() {
// Always stop firing if execution fails.
if ( !Execute() )
return false;
// If we start with a negative or 0 time, we will fire
// indefinately.
if ( _times <= 0 ) {
Reset();
return true;
}
// Not an indefinite loop, so only stop firing when
// our time counter has reached 0.
if ( --_times == 0 )
return false;
// We will fire again!
Reset();
return true;
}
void Timer::Reset() {
_when = GetTime() + _interval;;
}
ustime_t Timer::GetTime() {
timeval t;
gettimeofday( &t, NULL );
return ustime_t( t.tv_sec) << 32 | t.tv_usec;
}