Debian Codegraph @

src/d2cs/serverqueue.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2000,2001 Onlyer ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"
19 #include "setup.h"
20 #include "serverqueue.h"
21
22 #include <ctime>
23
24 #include "common/eventlog.h"
25 #include "common/xalloc.h"
26 /*
27 #include "common/packet.h"
28 #include "common/list.h"
29 */
30 #include "prefs.h"
31 #include "common/setup_after.h"
32
33 namespace pvpgn
34 {
35
36 namespace d2cs
37 {
38
39 static t_list * sqlist_head=NULL;
40 static unsigned int sqlist_seqno=0;
41
42 extern t_list * sqlist(void)
43 {
44 return sqlist_head;
45 }
46
47 extern int sqlist_create(void)
48 {
49 sqlist_head=list_create();
50 return 0;
51 }
52
53 extern int sqlist_destroy(void)
54 {
55 t_sq * sq;
56
57 BEGIN_LIST_TRAVERSE_DATA_CONST(sqlist_head,sq,t_sq)
58 {
59 sq_destroy(sq,(t_elem **)curr_elem_);
60 }
61 END_LIST_TRAVERSE_DATA_CONST()
62
63 if (list_destroy(sqlist_head)<0) {
64 eventlog(eventlog_level_error,__FUNCTION__,"error destroy server queue list");
65 return -1;
66 }
67 sqlist_head=NULL;
68 return 0;
69 }
70
71 extern int sqlist_check_timeout(void)
72 {
73 t_sq * sq;
74 std::time_t now;
75
76 now=std::time(NULL);
77 BEGIN_LIST_TRAVERSE_DATA(sqlist_head, sq, t_sq)
78 {
79 if (now - sq->ctime > prefs_get_sq_timeout()) {
80 eventlog(eventlog_level_info,__FUNCTION__,"destroying expired server queue {}",sq->seqno);
81 sq_destroy(sq,&curr_elem_);
82 }
83 }
84 END_LIST_TRAVERSE_DATA()
85 return 0;
86 }
87
88 extern t_sq * sqlist_find_sq(unsigned int seqno)
89 {
90 t_sq * sq;
91
92 BEGIN_LIST_TRAVERSE_DATA_CONST(sqlist_head,sq,t_sq)
93 {
94 if (sq->seqno==seqno) return sq;
95 }
96 END_LIST_TRAVERSE_DATA_CONST()
97 return NULL;
98 }
99
100 extern t_sq * sq_create(unsigned int clientid, t_packet * packet,unsigned int gameid )
101 {
102 t_sq * sq;
103
104 sq=(t_sq*)xmalloc(sizeof(t_sq));
105 sq->seqno=++sqlist_seqno;
106 sq->ctime=std::time(NULL);
107 sq->clientid=clientid;
108 sq->gameid=gameid;
109 sq->packet=packet;
110 sq->gametoken=0;
111 if (packet) packet_add_ref(packet);
112 list_append_data(sqlist_head,sq);
113 return sq;
114 }
115
116 extern int sq_destroy(t_sq * sq,t_elem ** curr)
117 {
118 ASSERT(sq,-1);
119 if (list_remove_data(sqlist_head,sq,curr)<0) {
120 eventlog(eventlog_level_error,__FUNCTION__,"error remove server queue from list");
121 return -1;
122 }
123 if (sq->packet) packet_del_ref(sq->packet);
124 xfree(sq);
125 return 0;
126 }
127
128 extern unsigned int sq_get_clientid(t_sq const * sq)
129 {
130 ASSERT(sq,0);
131 return sq->clientid;
132 }
133
134 extern t_packet * sq_get_packet(t_sq const * sq)
135 {
136 ASSERT(sq,NULL);
137 return sq->packet;
138 }
139
140 extern unsigned int sq_get_gameid(t_sq const * sq)
141 {
142 ASSERT(sq,0);
143 return sq->gameid;
144 }
145
146 extern unsigned int sq_get_seqno(t_sq const * sq)
147 {
148 ASSERT(sq,0);
149 return sq->seqno;
150 }
151
152 extern int sq_set_gametoken(t_sq * sq, unsigned int gametoken)
153 {
154 ASSERT(sq,-1);
155 sq->gametoken=gametoken;
156 return 0;
157 }
158
159 extern unsigned int sq_get_gametoken(t_sq const * sq)
160 {
161 ASSERT(sq,0);
162 return sq->gametoken;
163 }
164
165 }
166
167 }