Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/ClientInformation.cc @ 1431

Last change on this file since 1431 was 1431, checked in by scheusso, 16 years ago

another one bites the dust: solved that problem with zeros in the gamestate

File size: 8.7 KB
Line 
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
45namespace network
46{
47  boost::recursive_mutex ClientInformation::mutex_;
48 
49  ClientInformation::ClientInformation() {
50    gamestateID_=GAMESTATEID_INITIAL;
51    preve=0;
52    nexte=0;
53    partialGamestateID_=GAMESTATEID_INITIAL-1;
54    this->head_=false;
55    synched_=false;
56  }
57
58  ClientInformation::ClientInformation(bool head) {
59    gamestateID_=GAMESTATEID_INITIAL;
60    preve=0;
61    nexte=0;
62    partialGamestateID_=GAMESTATEID_INITIAL-1;
63    this->head_=head;
64    synched_=false;
65  }
66
67  // ClientInformation::ClientInformation(ClientInformation *prev) {
68  //   if(prev->next()!=0){
69  //     this->nexte=prev->next();
70  //     this->nexte->setPrev(this);
71  //   }
72  //   else
73  //     this->nexte = 0;
74  //   prev->setNext(this);
75  //   this->preve = pref;
76  // }
77  //
78  // ClientInformation::ClientInformation(ClientInformation *prev, ClientInformation *next){
79  //   this->nexte = next;
80  //   this->preve = prev;
81  //   this->preve->setNext(this);
82  //   this->nexte->setPrev(this);
83  // }
84
85  ClientInformation::~ClientInformation() {
86    boost::recursive_mutex::scoped_lock lock(mutex_);
87    if(prev()!=0)
88      prev()->setNext(this->next());
89    if(next()!=0)
90      next()->setPrev(this->prev());
91  }
92
93  ClientInformation *ClientInformation::next() {
94    boost::recursive_mutex::scoped_lock lock(mutex_);
95    if(this!=0)
96      return this->nexte;
97    else
98      return 0;
99  }
100  ClientInformation *ClientInformation::prev() {
101    boost::recursive_mutex::scoped_lock lock(mutex_);
102    if(this!=0)
103      return this->preve;
104    else
105      return 0;
106  }
107
108  bool ClientInformation::setPrev(ClientInformation *prev) {
109    boost::recursive_mutex::scoped_lock lock(mutex_);
110    if(!head_)
111      this->preve = prev;
112    else
113      return false;
114    return true;
115  }
116
117  bool ClientInformation::setNext(ClientInformation *next) {
118    boost::recursive_mutex::scoped_lock lock(mutex_);
119    this->nexte = next;
120    return true;
121  }
122
123  ClientInformation *ClientInformation::insertAfter(ClientInformation *ins) {
124    boost::recursive_mutex::scoped_lock lock(mutex_);
125    this->next()->setPrev(ins);
126    ins->setNext(this->next());
127    ins->setPrev(this);
128    this->nexte = ins;
129    return ins;
130  }
131
132  ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){
133    boost::recursive_mutex::scoped_lock lock(mutex_);
134    if(!this)
135      return NULL;
136    this->prev()->setNext(ins);
137    ins->setPrev(this->prev());
138    this->preve=ins;
139    ins->setNext(this);
140    return ins;
141  }
142
143  void ClientInformation::setID(int clientID){
144    if(!this)
145      return;
146    boost::recursive_mutex::scoped_lock lock(mutex_);
147    clientID_ = clientID;
148  }
149
150  bool ClientInformation::setPeer(ENetPeer *peer){
151    boost::recursive_mutex::scoped_lock lock(mutex_);
152    if(!this)
153      return false;
154    peer_ = peer;
155    return true;
156  }
157
158  bool ClientInformation::setGameStateID(int id){
159    boost::recursive_mutex::scoped_lock lock(mutex_);
160    if(!this)
161      return false;
162    gamestateID_=id;
163    return true;
164  }
165 
166  bool ClientInformation::setPartialGamestateID(int id){
167    boost::recursive_mutex::scoped_lock lock(mutex_);
168    if(!this)
169      return false;
170    partialGamestateID_=id;
171    return true;
172  }
173
174  int ClientInformation::getID() {
175    boost::recursive_mutex::scoped_lock lock(mutex_);
176    if(!this)
177      return CLIENTID_UNKNOWN;
178    else
179      return clientID_;
180  }
181
182  ENetPeer *ClientInformation::getPeer() {
183    boost::recursive_mutex::scoped_lock lock(mutex_);
184    if(this)
185      return peer_;
186    else
187      return NULL;
188  }
189 
190  bool ClientInformation::getHead(){
191    boost::recursive_mutex::scoped_lock lock(mutex_);
192    return head_;
193  }
194 
195  void ClientInformation::setHead(bool h){
196    boost::recursive_mutex::scoped_lock lock(mutex_);
197    head_=h;
198  }
199 
200  int ClientInformation::getFailures(){
201    boost::recursive_mutex::scoped_lock lock(mutex_);
202    return failures_;
203  }
204  void ClientInformation::addFailure(){
205    boost::recursive_mutex::scoped_lock lock(mutex_);
206    failures_++;
207  }
208  void ClientInformation::resetFailures(){
209    boost::recursive_mutex::scoped_lock lock(mutex_);
210    failures_=0;
211  }
212
213  int ClientInformation::getGamestateID() {
214    boost::recursive_mutex::scoped_lock lock(mutex_);
215    if(this)
216      return gamestateID_;
217    else
218      return -1;
219  }
220 
221  int ClientInformation::getPartialGamestateID() {
222    boost::recursive_mutex::scoped_lock lock(mutex_);
223    if(this)
224      return partialGamestateID_;
225    else
226      return -1;
227  }
228
229  ClientInformation *ClientInformation::insertBack(ClientInformation *ins) {
230    boost::recursive_mutex::scoped_lock lock(mutex_);
231    if(!this)
232      return NULL;
233    ClientInformation *temp = this;
234    while(temp->next()!=0){
235      temp = temp->next();
236    }
237    temp->setNext(ins);
238    ins->setPrev(temp);
239    return ins;
240  }
241
242  bool ClientInformation::removeClient(int clientID) {
243    boost::recursive_mutex::scoped_lock lock(mutex_);
244    if(!this || clientID==CLIENTID_UNKNOWN)
245      return false;
246    ClientInformation *temp = this;
247    while(temp!=0 && temp->getID()!=clientID)
248      temp = temp->next();
249    if(temp==0)
250      return false;
251    delete temp;
252    return true;
253  }
254
255  bool ClientInformation::removeClient(ENetPeer *peer) {
256    boost::recursive_mutex::scoped_lock lock(mutex_);
257    if(!this || !peer)
258      return false;
259    ClientInformation *temp = this;
260    while(temp!=0){
261      if(!temp->head_)
262        if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port)
263          break;
264      temp = temp->next();
265    }
266    if(temp==0)
267      return false;
268    delete temp;
269    return true;
270  }
271
272  /**
273  * This function goes forward through the list and looks for an element with clientID
274  * This function should only be applied to the head of the list
275  * @param clientID id to look for
276  * @return pointer to the last element in the list or 0 if the search was unsuccessfull
277  */
278  ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) {
279    boost::recursive_mutex::scoped_lock lock(mutex_);
280    ClientInformation *temp = this;
281    if (temp->head_)
282      temp=temp->next();
283    while(temp!=0 && temp->getID()!=clientID){
284      temp = temp->next();
285    }
286    // returns 0 if nothing has been found
287    return temp;
288  }
289
290  /**
291  * This function goes forward through the list and looks for an element with clientID
292  * This function should only be applied to the head of the list
293  * @param peer peer to look for
294  * @return pointer to the element in the list
295  */
296  ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) {
297    boost::recursive_mutex::scoped_lock lock(mutex_);
298    ClientInformation *temp = this;
299    while(temp!=0){
300      if(temp->head_){
301        temp = temp->next();
302        continue;
303      }
304      if(temp->getPeer()->address.host==address->host && temp->getPeer()->address.port == address->port)
305        break;
306      temp = temp->next();
307    }
308    // returns 0 if nothing has been found
309    return temp;
310  }
311
312  bool ClientInformation::setSynched(bool s) {
313    boost::recursive_mutex::scoped_lock lock(mutex_);
314    if(!this)
315      return false;
316    synched_=s;
317    return true;
318  }
319
320  bool ClientInformation::getSynched() {
321    boost::recursive_mutex::scoped_lock lock(mutex_);
322    if(this)
323      return synched_;
324    else
325      return false;
326  }
327
328}
Note: See TracBrowser for help on using the repository browser.