]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/timer.cpp
Added comment
[user/henk/code/inspircd.git] / src / timer.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <string>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include <sstream>
30 #include <iostream>
31 #include <fstream>
32 #include <stdexcept>
33 #include "inspircd.h"
34 #include "inspircd_io.h"
35 #include "inspstring.h"
36 #include "helperfuncs.h"
37
38 extern InspIRCd* ServerInstance;
39 extern ServerConfig* Config;
40 extern time_t TIME;
41
42 typedef std::vector<InspTimer*> timergroup;
43 typedef std::map<time_t, timergroup*> timerlist;
44 timerlist Timers;
45
46 void TickTimers(time_t TIME)
47 {
48         timerlist::iterator found = Timers.find(TIME);
49
50         if (found != Timers.end())
51         {
52                 timergroup* x = found->second;
53                 /*
54                  * There are pending timers to trigger
55                  */
56                 for (timergroup::iterator y = x->begin(); y != x->end(); y++)
57                 {
58                         InspTimer* n = (InspTimer*)*y;
59                         n->Tick(TIME);
60                         delete n;
61                 }
62
63                 Timers.erase(found);
64                 delete x;
65         }
66 }
67
68 /*
69  * Because some muppets may do odd things, and their ircd may lock up due
70  * to crappy 3rd party modules, or they may change their system time a bit,
71  * this accounts for shifts of up to 120 secs by looking behind for missed
72  * timers and executing them. This is only executed once every 5 secs.
73  * If you move your clock BACK, and your timers move further ahead as a result,
74  * then tough titty you'll just have to wait.
75  */
76 void TickMissedTimers(time_t TIME)
77 {
78         for (time_t n = TIME-1; n > TIME-120; n--)
79         {
80                 timerlist::iterator found = Timers.find(n);
81                 if (found != Timers.end())
82                 {
83                         timergroup* x = found->second;
84                         for (timergroup::iterator y = x->begin(); y != x->end(); y++)
85                         {
86                                 InspTimer* z = (InspTimer*)*y;
87                                 z->Tick(TIME);
88                                 delete z;
89                         }
90
91                         Timers.erase(found);
92                         delete x;
93                 }
94         }
95 }
96
97 void AddTimer(InspTimer* T)
98 {
99         timergroup* x = NULL;
100
101         timerlist::iterator found = Timers.find(T->GetTimer());
102         
103         if (found != Timers.end())
104         {
105                 x = found->second;
106         }
107         else
108         {
109                 x = new timergroup;
110                 Timers[T->GetTimer()] = x;
111         }
112
113         x->push_back(T);
114 }