Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/network/ClientInformation.cc @ 1996

Last change on this file since 1996 was 1534, checked in by rgrieder, 16 years ago

merged network branch back to trunk

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