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
19 /** Timer class for one-second resolution timers
20 * InspTimer 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 InspTimer, inherit a class from
24 * InspTimer, 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 InspTimer : 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 InspTimer(long secs_from_now,time_t now, bool repeating = false)
49 trigger = now + secs_from_now;
54 /** Default destructor, does nothing.
56 virtual ~InspTimer() { }
58 /** Retrieve the current triggering time
60 virtual time_t GetTimer()
65 /** Called when the timer ticks.
66 * You should override this method with some useful code to
67 * handle the tick event.
69 virtual void Tick(time_t TIME) = 0;
71 /** Returns true if this timer is set to repeat
78 /** Returns the interval (number of seconds between ticks)
79 * of this timer object.
86 /** Cancels the repeat state of a repeating timer.
87 * If you call this method, then the next time your
88 * timer ticks, it will be removed immediately after.
89 * You should use this method call to remove a recurring
90 * timer if you wish to do so within the timer's Tick
91 * event, as calling TimerManager::DelTimer() from within
92 * the InspTimer::Tick() method is dangerous and may
93 * cause a segmentation fault. Calling CancelRepeat()
94 * is safe in this case.
103 /** This class manages sets of InspTimers, and triggers them at their defined times.
104 * This will ensure timers are not missed, as well as removing timers that have
105 * expired and allowing the addition of new ones.
107 class CoreExport TimerManager : public Extensible
110 /** A group of timers all set to trigger at the same time
112 typedef std::vector<InspTimer*> timergroup;
113 /** A map of timergroups, each group has a specific trigger time
115 typedef std::map<time_t, timergroup*> timerlist;
116 /** Set when ticking timers, to prevent deletion while iterating
119 /** Creating server instance
121 InspIRCd* ServerInstance;
124 /** The current timer set, a map of timergroups
131 TimerManager(InspIRCd* Instance);
132 /** Tick all pending InspTimers
133 * @param TIME the current system time
135 void TickTimers(time_t TIME);
137 * @param T an InspTimer derived class to add
138 * @param secs_from_now You may set this to the number of seconds
139 * from the current time when the timer will tick, or you may just
140 * leave this unset and the values set by the InspTimers constructor
141 * will be used. This is used internally for re-triggering repeating
144 void AddTimer(InspTimer* T, long secs_from_now = 0);
145 /** Delete an InspTimer
146 * @param T an InspTimer derived class to delete
148 void DelTimer(InspTimer* T);
149 /** Tick any timers that have been missed due to lag
150 * @param TIME the current system time
152 void TickMissedTimers(time_t TIME);