1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef INSPIRCD_TIMER_H
15 #define INSPIRCD_TIMER_H
19 /** Timer class for one-second resolution timers
20 * Timer provides a facility which allows module
21 * developers to create one-shot timers. The timer
22 * can be made to trigger at any time up to a one-second
23 * resolution. To use Timer, inherit a class from
24 * Timer, then insert your inherited class into the
25 * queue using Server::AddTimer(). The Tick() method of
26 * your object (which you should override) will be called
29 class CoreExport Timer : public Extensible
32 /** The triggering time
35 /** Number of seconds between triggers
38 /** True if this is a repeating timer
42 /** Default constructor, initializes the triggering time
43 * @param secs_from_now The number of seconds from now to trigger the timer
44 * @param now The time now
45 * @param repeating Repeat this timer every secs_from_now seconds if set to true
47 Timer(long secs_from_now, time_t now, bool repeating = false)
49 trigger = now + secs_from_now;
54 /** Default destructor, does nothing.
58 /** Retrieve the current triggering time
60 virtual time_t GetTimer()
65 /** Sets the trigger timeout to a new value
67 virtual void SetTimer(time_t t)
72 /** Called when the timer ticks.
73 * You should override this method with some useful code to
74 * handle the tick event.
76 virtual void Tick(time_t TIME) = 0;
78 /** Returns true if this timer is set to repeat
85 /** Returns the interval (number of seconds between ticks)
86 * of this timer object.
93 /** Cancels the repeat state of a repeating timer.
94 * If you call this method, then the next time your
95 * timer ticks, it will be removed immediately after.
96 * You should use this method call to remove a recurring
97 * timer if you wish to do so within the timer's Tick
98 * event, as calling TimerManager::DelTimer() from within
99 * the Timer::Tick() method is dangerous and may
100 * cause a segmentation fault. Calling CancelRepeat()
101 * is safe in this case.
110 /** This class manages sets of Timers, and triggers them at their defined times.
111 * This will ensure timers are not missed, as well as removing timers that have
112 * expired and allowing the addition of new ones.
114 class CoreExport TimerManager : public Extensible
117 /** A list of all pending timers
119 std::vector<Timer *> Timers;
121 /** Creating server instance
123 InspIRCd* ServerInstance;
127 TimerManager(InspIRCd* Instance);
129 /** Tick all pending Timers
130 * @param TIME the current system time
132 void TickTimers(time_t TIME);
135 * @param T an Timer derived class to add
136 * @param secs_from_now You may set this to the number of seconds
137 * from the current time when the timer will tick, or you may just
138 * leave this unset and the values set by the Timers constructor
139 * will be used. This is used internally for re-triggering repeating
142 void AddTimer(Timer *T);
145 * @param T an Timer derived class to delete
147 void DelTimer(Timer* T);
149 /** Compares two timers
151 static bool TimerComparison( Timer *one, Timer*two);