]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/timer.cpp
Include tweaks
[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 "users.h"
34 #include "ctables.h"
35 #include "typedefs.h"
36 #include "commands.h"
37 #include "globals.h"
38 #include "hashcomp.h"
39 #include "inspircd.h"
40 #include "inspircd_io.h"
41 #include "inspstring.h"
42 #include "helperfuncs.h"
43
44 extern InspIRCd* ServerInstance;
45 extern ServerConfig* Config;
46 extern time_t TIME;
47
48 typedef std::vector<InspTimer*> timergroup;
49 typedef std::map<time_t, timergroup*> timerlist;
50 timerlist Timers;
51
52 void TickTimers(time_t TIME)
53 {
54         timerlist::iterator found = Timers.find(TIME);
55
56         if (found != Timers.end())
57         {
58                 timergroup* x = found->second;
59                 /*
60                  * There are pending timers to trigger
61                  */
62                 for (timergroup::iterator y = x->begin(); y != x->end(); y++)
63                 {
64                         InspTimer* n = (InspTimer*)*y;
65                         n->Tick(TIME);
66                         delete n;
67                 }
68
69                 Timers.erase(found);
70                 delete x;
71         }
72 }
73
74 /*
75  * Because some muppets may do odd things, and their ircd may lock up due
76  * to crappy 3rd party modules, or they may change their system time a bit,
77  * this accounts for shifts of up to 120 secs by looking behind for missed
78  * timers and executing them. This is only executed once every 5 secs.
79  * If you move your clock BACK, and your timers move further ahead as a result,
80  * then tough titty you'll just have to wait.
81  */
82 void TickMissedTimers(time_t TIME)
83 {
84         for (time_t n = TIME-1; n > TIME-120; n--)
85         {
86                 timerlist::iterator found = Timers.find(n);
87                 if (found != Timers.end())
88                 {
89                         timergroup* x = found->second;
90                         for (timergroup::iterator y = x->begin(); y != x->end(); y++)
91                         {
92                                 InspTimer* z = (InspTimer*)*y;
93                                 z->Tick(TIME);
94                                 delete z;
95                         }
96
97                         Timers.erase(found);
98                         delete x;
99                 }
100         }
101 }
102
103 void AddTimer(InspTimer* T)
104 {
105         timergroup* x = NULL;
106
107         timerlist::iterator found = Timers.find(T->GetTimer());
108         
109         if (found != Timers.end())
110         {
111                 x = found->second;
112         }
113         else
114         {
115                 x = new timergroup;
116                 Timers[T->GetTimer()] = x;
117         }
118
119         x->push_back(T);
120 }