1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * ... |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | // |
---|
30 | // C++ Implementation: ClientInformation |
---|
31 | // |
---|
32 | // Description: |
---|
33 | // |
---|
34 | // |
---|
35 | // Author: <>, (C) 2007 |
---|
36 | // |
---|
37 | // Copyright: See COPYING file that comes with this distribution |
---|
38 | // |
---|
39 | // |
---|
40 | |
---|
41 | #include "ClientInformation.h" |
---|
42 | |
---|
43 | #include <iostream> //debug |
---|
44 | |
---|
45 | namespace network |
---|
46 | { |
---|
47 | boost::recursive_mutex ClientInformation::mutex_; |
---|
48 | |
---|
49 | ClientInformation::ClientInformation() { |
---|
50 | gamestateID_=GAMESTATEID_INITIAL; |
---|
51 | preve=0; |
---|
52 | nexte=0; |
---|
53 | this->head_=false; |
---|
54 | synched_=false; |
---|
55 | } |
---|
56 | |
---|
57 | ClientInformation::ClientInformation(bool head) { |
---|
58 | gamestateID_=GAMESTATEID_INITIAL; |
---|
59 | preve=0; |
---|
60 | nexte=0; |
---|
61 | this->head_=head; |
---|
62 | synched_=false; |
---|
63 | } |
---|
64 | |
---|
65 | // ClientInformation::ClientInformation(ClientInformation *prev) { |
---|
66 | // if(prev->next()!=0){ |
---|
67 | // this->nexte=prev->next(); |
---|
68 | // this->nexte->setPrev(this); |
---|
69 | // } |
---|
70 | // else |
---|
71 | // this->nexte = 0; |
---|
72 | // prev->setNext(this); |
---|
73 | // this->preve = pref; |
---|
74 | // } |
---|
75 | // |
---|
76 | // ClientInformation::ClientInformation(ClientInformation *prev, ClientInformation *next){ |
---|
77 | // this->nexte = next; |
---|
78 | // this->preve = prev; |
---|
79 | // this->preve->setNext(this); |
---|
80 | // this->nexte->setPrev(this); |
---|
81 | // } |
---|
82 | |
---|
83 | ClientInformation::~ClientInformation() { |
---|
84 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
85 | if(preve!=0) |
---|
86 | preve->setNext(this->nexte); |
---|
87 | if(nexte!=0) |
---|
88 | nexte->setPrev(this->preve); |
---|
89 | } |
---|
90 | |
---|
91 | ClientInformation *ClientInformation::next() { |
---|
92 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
93 | if(this!=0) |
---|
94 | return this->nexte; |
---|
95 | else |
---|
96 | return 0; |
---|
97 | } |
---|
98 | ClientInformation *ClientInformation::prev() { |
---|
99 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
100 | if(this!=0) |
---|
101 | return this->preve; |
---|
102 | else |
---|
103 | return 0; |
---|
104 | } |
---|
105 | |
---|
106 | bool ClientInformation::setPrev(ClientInformation *prev) { |
---|
107 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
108 | if(!head_) |
---|
109 | this->preve = prev; |
---|
110 | else |
---|
111 | return false; |
---|
112 | return true; |
---|
113 | } |
---|
114 | |
---|
115 | bool ClientInformation::setNext(ClientInformation *next) { |
---|
116 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
117 | this->nexte = next; |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | ClientInformation *ClientInformation::insertAfter(ClientInformation *ins) { |
---|
122 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
123 | this->nexte->setPrev(ins); |
---|
124 | ins->setNext(this->nexte); |
---|
125 | ins->setPrev(this); |
---|
126 | this->nexte = ins; |
---|
127 | return ins; |
---|
128 | } |
---|
129 | |
---|
130 | ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){ |
---|
131 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
132 | if(!this) |
---|
133 | return NULL; |
---|
134 | this->prev()->setNext(ins); |
---|
135 | ins->setPrev(this->preve); |
---|
136 | this->preve=ins; |
---|
137 | ins->setNext(this); |
---|
138 | return ins; |
---|
139 | } |
---|
140 | |
---|
141 | void ClientInformation::setID(int clientID){ |
---|
142 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
143 | clientID_ = clientID; |
---|
144 | } |
---|
145 | |
---|
146 | bool ClientInformation::setPeer(ENetPeer *peer){ |
---|
147 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
148 | if(!this) |
---|
149 | return false; |
---|
150 | peer_ = peer; |
---|
151 | return true; |
---|
152 | } |
---|
153 | |
---|
154 | bool ClientInformation::setGamestateID(int id){ |
---|
155 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
156 | if(!this) |
---|
157 | return false; |
---|
158 | gamestateID_=id; |
---|
159 | return true; |
---|
160 | } |
---|
161 | |
---|
162 | int ClientInformation::getID() { |
---|
163 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
164 | if(!this) |
---|
165 | return CLIENTID_UNKNOWN; |
---|
166 | else |
---|
167 | return clientID_; |
---|
168 | } |
---|
169 | |
---|
170 | ENetPeer *ClientInformation::getPeer() { |
---|
171 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
172 | if(this) |
---|
173 | return peer_; |
---|
174 | else |
---|
175 | return NULL; |
---|
176 | } |
---|
177 | |
---|
178 | bool ClientInformation::getHead(){ |
---|
179 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
180 | return head_; |
---|
181 | } |
---|
182 | |
---|
183 | void ClientInformation::setHead(bool h){ |
---|
184 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
185 | head_=h; |
---|
186 | } |
---|
187 | |
---|
188 | int ClientInformation::getFailures(){ |
---|
189 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
190 | return failures_; |
---|
191 | } |
---|
192 | void ClientInformation::addFailure(){ |
---|
193 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
194 | failures_++; |
---|
195 | } |
---|
196 | void ClientInformation::resetFailures(){ |
---|
197 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
198 | failures_=0; |
---|
199 | } |
---|
200 | |
---|
201 | int ClientInformation::getGamestateID() { |
---|
202 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
203 | if(this) |
---|
204 | return gamestateID_; |
---|
205 | else |
---|
206 | return -1; |
---|
207 | } |
---|
208 | |
---|
209 | ClientInformation *ClientInformation::insertBack(ClientInformation *ins) { |
---|
210 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
211 | if(!this) |
---|
212 | return NULL; |
---|
213 | ClientInformation *temp = this; |
---|
214 | while(temp->next()!=0){ |
---|
215 | temp = temp->next(); |
---|
216 | } |
---|
217 | temp->setNext(ins); |
---|
218 | ins->setPrev(temp); |
---|
219 | return ins; |
---|
220 | } |
---|
221 | |
---|
222 | bool ClientInformation::removeClient(int clientID) { |
---|
223 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
224 | if(!this || clientID==CLIENTID_UNKNOWN) |
---|
225 | return false; |
---|
226 | ClientInformation *temp = this; |
---|
227 | while(temp!=0 && temp->getID()!=clientID) |
---|
228 | temp = temp->next(); |
---|
229 | if(temp==0) |
---|
230 | return false; |
---|
231 | delete temp; |
---|
232 | return true; |
---|
233 | } |
---|
234 | |
---|
235 | bool ClientInformation::removeClient(ENetPeer *peer) { |
---|
236 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
237 | if(!this || !peer) |
---|
238 | return false; |
---|
239 | ClientInformation *temp = this; |
---|
240 | while(temp!=0){ |
---|
241 | if(!temp->head_) |
---|
242 | if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port) |
---|
243 | break; |
---|
244 | temp = temp->next(); |
---|
245 | } |
---|
246 | if(temp==0) |
---|
247 | return false; |
---|
248 | delete temp; |
---|
249 | return true; |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * This function goes forward through the list and looks for an element with clientID |
---|
254 | * This function should only be applied to the head of the list |
---|
255 | * @param clientID id to look for |
---|
256 | * @return pointer to the last element in the list or 0 if the search was unsuccessfull |
---|
257 | */ |
---|
258 | ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) { |
---|
259 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
260 | ClientInformation *temp = this; |
---|
261 | if (temp->head_) |
---|
262 | temp=temp->next(); |
---|
263 | while(temp!=0 && temp->getID()!=clientID){ |
---|
264 | temp = temp->next(); |
---|
265 | } |
---|
266 | // returns 0 if nothing has been found |
---|
267 | return temp; |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * This function goes forward through the list and looks for an element with clientID |
---|
272 | * This function should only be applied to the head of the list |
---|
273 | * @param peer peer to look for |
---|
274 | * @return pointer to the element in the list |
---|
275 | */ |
---|
276 | ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) { |
---|
277 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
278 | ClientInformation *temp = this; |
---|
279 | while(temp!=0){ |
---|
280 | if(temp->head_){ |
---|
281 | temp = temp->next(); |
---|
282 | continue; |
---|
283 | } |
---|
284 | if(temp->getPeer()->address.host==address->host && temp->getPeer()->address.port == address->port) |
---|
285 | break; |
---|
286 | temp = temp->next(); |
---|
287 | } |
---|
288 | // returns 0 if nothing has been found |
---|
289 | return temp; |
---|
290 | } |
---|
291 | |
---|
292 | bool ClientInformation::setSynched(bool s) { |
---|
293 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
294 | if(!this) |
---|
295 | return false; |
---|
296 | synched_=s; |
---|
297 | return true; |
---|
298 | } |
---|
299 | |
---|
300 | bool ClientInformation::getSynched() { |
---|
301 | boost::recursive_mutex::scoped_lock lock(mutex_); |
---|
302 | if(this) |
---|
303 | return synched_; |
---|
304 | else |
---|
305 | return false; |
---|
306 | } |
---|
307 | |
---|
308 | } |
---|