Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merger/src/network/ClientInformation.cc @ 489

Last change on this file since 489 was 446, checked in by scheusso, 17 years ago

corrected clientinformation::removeClient(enet event)

File size: 4.2 KB
Line 
1//
2// C++ Implementation: ClientInformation
3//
4// Description:
5//
6//
7// Author:  <>, (C) 2007
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12#include "ClientInformation.h"
13
14namespace network {
15
16ClientInformation::ClientInformation()
17{
18  gamestateID_=GAMESTATEID_INITIAL;
19  preve=0;
20  nexte=0;
21  this->head=false;
22}
23
24ClientInformation::ClientInformation(bool head)
25{
26  gamestateID_=GAMESTATEID_INITIAL;
27  preve=0;
28  nexte=0;
29  this->head=head;
30}
31//
32
33// ClientInformation::ClientInformation(ClientInformation *prev){
34//   if(prev->next()!=0){
35//     this->nexte=prev->next();
36//     this->nexte->setPrev(this);
37//   }
38//   else
39//     this->nexte = 0;
40//   prev->setNext(this);
41//   this->preve = pref;
42// }
43//
44// ClientInformation::ClientInformation(ClientInformation *prev, ClientInformation *next){
45//   this->nexte = next;
46//   this->preve = prev;
47//   this->preve->setNext(this);
48//   this->nexte->setPrev(this);
49// }
50
51ClientInformation::~ClientInformation()
52{
53  if(preve!=0)
54    preve->setNext(this->nexte);
55  if(nexte!=0)
56    nexte->setPrev(this->preve);
57}
58
59ClientInformation *ClientInformation::next(){
60  if(this!=0)
61    return this->nexte;
62  else
63    return 0;
64}
65ClientInformation *ClientInformation::prev(){
66  if(this!=0)
67    return this->preve;
68  else
69    return 0;
70}
71
72bool ClientInformation::setPrev(ClientInformation *prev){
73  if(!head)
74    this->preve = prev;
75  else
76    return false;
77  return true;
78}
79
80bool ClientInformation::setNext(ClientInformation *next){
81  this->nexte = next;
82  return true;
83}
84
85ClientInformation *ClientInformation::insertAfter(ClientInformation *ins){
86  this->nexte->setPrev(ins);
87  ins->setNext(this->nexte);
88  ins->setPrev(this);
89  this->nexte = ins;
90  return ins;
91}
92
93ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){
94  this->prev()->setNext(ins);
95  ins->setPrev(this->preve);
96  this->preve=ins;
97  ins->setNext(this);
98  return ins;
99}
100
101void ClientInformation::setID(int clientID){
102  clientID_ = clientID;
103}
104void ClientInformation::setPeer(ENetPeer *peer){
105  peer_ = peer;
106}
107
108void ClientInformation::setGamestateID(int id){
109  gamestateID_=id;
110}
111
112int ClientInformation::getID(){
113  return clientID_;
114}
115ENetPeer *ClientInformation::getPeer(){
116  return peer_;
117}
118
119int ClientInformation::getGamestateID(){
120  return gamestateID_;
121}
122
123ClientInformation *ClientInformation::insertBack(ClientInformation *ins){
124  ClientInformation *temp = this;
125  while(temp->next()!=0){
126    temp = temp->next();
127  }
128  temp->setNext(ins);
129  ins->setPrev(temp);
130  return ins;
131}
132
133bool ClientInformation::removeClient(int clientID){
134  ClientInformation *temp = this;
135  while(temp!=0 && temp->getID()!=clientID)
136    temp = temp->next();
137  if(temp==0)
138    return false;
139  delete temp;
140  return true;
141}
142
143bool ClientInformation::removeClient(ENetPeer *peer){
144  ClientInformation *temp = this;
145  while(temp!=0){
146    if(!temp->head)
147      if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port)
148        break;
149    temp = temp->next();
150  }
151  if(temp==0)
152    return false;
153  delete temp;
154  return true;
155}
156
157/**
158 * This function goes forward through the list and looks for an element with clientID
159 * This function should only be applied to the head of the list
160 * @param clientID id to look for
161 * @return pointer to the element in the list or 0 if the search was unsuccessfull
162 */
163ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards){
164  ClientInformation *temp = this;
165  while(temp!=0 && temp->getID()!=clientID){
166    if (temp->head)
167      continue;
168    temp = temp->next();
169  }
170  // returns 0 if nothing has been found
171  return temp;
172}
173
174/**
175 * This function goes forward through the list and looks for an element with clientID
176 * This function should only be applied to the head of the list
177 * @param peer peer to look for
178 * @return pointer to the element in the list
179 */
180ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards){
181  ClientInformation *temp = this;
182  while(temp!=0){
183    if(temp->head){
184      temp = temp->next();
185      continue;
186    }
187    if(temp->getPeer()->address.host==address->host && temp->getPeer()->address.port == address->port)
188      break;
189    temp = temp->next();
190  }
191  // returns 0 if nothing has been found
192  return temp;
193}
194
195}
Note: See TracBrowser for help on using the repository browser.