Debian Codegraph @

src/bnetd/timer.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999,2000 Ross Combs ([email protected])
3 * Copyright (C) 2004 Dizzy
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #define TIMER_INTERNAL_ACCESS
20 #include "common/setup_before.h"
21 #include "timer.h"
22 #include <cstdlib>
23 #include "common/elist.h"
24 #include "connection.h"
25 #include "common/eventlog.h"
26 #include "common/xalloc.h"
27 #include "common/setup_after.h"
28
29 namespace pvpgn
30 {
31
32 namespace bnetd
33 {
34
35 static t_elist timerlist_head;
36
37
38 extern int timerlist_add_timer(t_connection * owner, std::time_t when, t_timer_cb cb, t_timer_data data)
39 {
40 t_timer * timer, *ctimer;
41 t_elist * curr;
42
43 if (!owner)
44 {
45 eventlog(eventlog_level_error, __FUNCTION__, "got NULL owner");
46 return -1;
47 }
48
49 timer = (t_timer*)xmalloc(sizeof(t_timer));
50 timer->owner = owner;
51 timer->when = when;
52 timer->cb = cb;
53 timer->data = data;
54
55 /* try to preserve a when based order */
56 elist_for_each(curr, &timerlist_head)
57 {
58 ctimer = elist_entry(curr, t_timer, timers);
59 if (ctimer->when > when) break;
60 }
61 elist_add_tail(curr, &timer->timers);
62
63 /* add it to the t_conn timers list */
64 elist_add_tail(conn_get_timer(owner), &timer->owners);
65
66 return 0;
67 }
68
69
70 extern int timerlist_del_all_timers(t_connection * owner)
71 {
72 t_elist * curr, *save;
73 t_timer * timer;
74
75 if (!owner)
76 {
77 eventlog(eventlog_level_error, __FUNCTION__, "got NULL owner");
78 return -1;
79 }
80
81 elist_for_each_safe(curr, conn_get_timer(owner), save)
82 {
83 timer = elist_entry(curr, t_timer, owners);
84 if (timer->cb)
85 timer->cb(timer->owner, (std::time_t)0, timer->data);
86 elist_del(&timer->owners);
87 elist_del(&timer->timers);
88 xfree((void*)timer);
89 }
90
91 return 0;
92 }
93
94
95 extern int timerlist_check_timers(std::time_t when)
96 {
97 t_elist * curr, *save;
98 t_timer * timer;
99
100 elist_for_each_safe(curr, &timerlist_head, save)
101 {
102 timer = elist_entry(curr, t_timer, timers);
103 if (timer->owner && timer->when < when)
104 {
105 if (timer->cb)
106 timer->cb(timer->owner, timer->when, timer->data);
107 elist_del(&timer->owners);
108 elist_del(&timer->timers);
109 xfree((void*)timer);
110 }
111 else break; /* beeing sorted there is no need to go beyond this point */
112 }
113
114 return 0;
115 }
116
117 extern int timerlist_create(void)
118 {
119 elist_init(&timerlist_head);
120 return 0;
121 }
122
123
124 extern int timerlist_destroy(void)
125 {
126 t_elist * curr, *save;
127 t_timer * timer;
128
129 elist_for_each_safe(curr, &timerlist_head, save)
130 {
131 timer = elist_entry(curr, t_timer, timers);
132 elist_del(&timer->owners);
133 elist_del(&timer->timers);
134 xfree((void*)timer);
135 }
136 elist_init(&timerlist_head);
137
138 return 0;
139 }
140
141 }
142
143 }