1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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
17 /** Timer class for one-second resolution timers
18 * InspTimer provides a facility which allows module
19 * developers to create one-shot timers. The timer
20 * can be made to trigger at any time up to a one-second
21 * resolution. To use InspTimer, inherit a class from
22 * InspTimer, then insert your inherited class into the
23 * queue using Server::AddTimer(). The Tick() method of
24 * your object (which you should override) will be called
27 class InspTimer : public Extensible
30 /** The triggering time
33 /** Number of seconds between triggers
36 /** True if this is a repeating timer
40 /** Default constructor, initializes the triggering time
41 * @param secs_from_now The number of seconds from now to trigger the timer
42 * @param now The time now
43 * @param repeating Repeat this timer every secs_from_now seconds if set to true
45 InspTimer(long secs_from_now,time_t now, bool repeating = false)
47 trigger = now + secs_from_now;
52 /** Default destructor, does nothing.
54 virtual ~InspTimer() { }
56 /** Retrieve the current triggering time
58 virtual time_t GetTimer()
63 /** Called when the timer ticks.
64 * You should override this method with some useful code to
65 * handle the tick event.
67 virtual void Tick(time_t TIME) = 0;
69 /** Returns true if this timer is set to repeat
76 /** Returns the interval (number of seconds between ticks)
77 * of this timer object.
84 /** Cancels the repeat state of a repeating timer.
85 * If you call this method, then the next time your
86 * timer ticks, it will be removed immediately after.
87 * You should use this method call to remove a recurring
88 * timer if you wish to do so within the timer's Tick
89 * event, as calling TimerManager::DelTimer() from within
90 * the InspTimer::Tick() method is dangerous and may
91 * cause a segmentation fault. Calling CancelRepeat()
92 * is safe in this case.
101 /** This class manages sets of InspTimers, and triggers them at their defined times.
102 * This will ensure timers are not missed, as well as removing timers that have
103 * expired and allowing the addition of new ones.
105 class TimerManager : public Extensible
108 /** A group of timers all set to trigger at the same time
110 typedef std::vector<InspTimer*> timergroup;
111 /** A map of timergroups, each group has a specific trigger time
113 typedef std::map<time_t, timergroup*> timerlist;
117 /** The current timer set, a map of timergroups
122 /** Tick all pending InspTimers
123 * @param TIME the current system time
125 void TickTimers(time_t TIME);
127 * @param T an InspTimer derived class to add
128 * @param secs_from_now You may set this to the number of seconds
129 * from the current time when the timer will tick, or you may just
130 * leave this unset and the values set by the InspTimers constructor
131 * will be used. This is used internally for re-triggering repeating
134 void AddTimer(InspTimer* T, long secs_from_now = 0);
135 /** Delete an InspTimer
136 * @param T an InspTimer derived class to delete
138 void DelTimer(InspTimer* T);
139 /** Tick any timers that have been missed due to lag
140 * @param TIME the current system time
142 void TickMissedTimers(time_t TIME);