| 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 |    | 
|---|
| 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 |   int ClientInformation::getGamestateID() { | 
|---|
| 195 |     if(this) | 
|---|
| 196 |       return gamestateID_; | 
|---|
| 197 |     else | 
|---|
| 198 |       return -1; | 
|---|
| 199 |   } | 
|---|
| 200 |    | 
|---|
| 201 |   int ClientInformation::getPartialGamestateID() { | 
|---|
| 202 |     if(this) | 
|---|
| 203 |       return partialGamestateID_; | 
|---|
| 204 |     else | 
|---|
| 205 |       return -1; | 
|---|
| 206 |   } | 
|---|
| 207 |  | 
|---|
| 208 |   ClientInformation *ClientInformation::insertBack(ClientInformation *ins) { | 
|---|
| 209 |     if(!this) | 
|---|
| 210 |       return NULL; | 
|---|
| 211 |     ClientInformation *temp = this; | 
|---|
| 212 |     while(temp->next()!=0){ | 
|---|
| 213 |       temp = temp->next(); | 
|---|
| 214 |     } | 
|---|
| 215 |     temp->setNext(ins); | 
|---|
| 216 |     ins->setPrev(temp); | 
|---|
| 217 |     return ins; | 
|---|
| 218 |   } | 
|---|
| 219 |  | 
|---|
| 220 |   bool ClientInformation::removeClient(int clientID) { | 
|---|
| 221 |     if(!this || clientID==CLIENTID_UNKNOWN) | 
|---|
| 222 |       return false; | 
|---|
| 223 |     ClientInformation *temp = this; | 
|---|
| 224 |     while(temp!=0 && temp->getID()!=clientID) | 
|---|
| 225 |       temp = temp->next(); | 
|---|
| 226 |     if(temp==0) | 
|---|
| 227 |       return false; | 
|---|
| 228 |     delete temp; | 
|---|
| 229 |     return true; | 
|---|
| 230 |   } | 
|---|
| 231 |  | 
|---|
| 232 |   bool ClientInformation::removeClient(ENetPeer *peer) { | 
|---|
| 233 |     if(!this || !peer) | 
|---|
| 234 |       return false; | 
|---|
| 235 |     ClientInformation *temp = this; | 
|---|
| 236 |     while(temp!=0){ | 
|---|
| 237 |       if(!temp->head_) | 
|---|
| 238 |         if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port) | 
|---|
| 239 |           break; | 
|---|
| 240 |       temp = temp->next(); | 
|---|
| 241 |     } | 
|---|
| 242 |     if(temp==0) | 
|---|
| 243 |       return false; | 
|---|
| 244 |     delete temp; | 
|---|
| 245 |     return true; | 
|---|
| 246 |   } | 
|---|
| 247 |  | 
|---|
| 248 |   /** | 
|---|
| 249 |   * This function goes forward through the list and looks for an element with clientID | 
|---|
| 250 |   * This function should only be applied to the head of the list | 
|---|
| 251 |   * @param clientID id to look for | 
|---|
| 252 |   * @return pointer to the last element in the list or 0 if the search was unsuccessfull | 
|---|
| 253 |   */ | 
|---|
| 254 |   ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) { | 
|---|
| 255 |     ClientInformation *temp = this; | 
|---|
| 256 |     if (temp->head_) | 
|---|
| 257 |       temp=temp->next(); | 
|---|
| 258 |     while(temp!=0 && temp->getID()!=clientID){ | 
|---|
| 259 |       temp = temp->next(); | 
|---|
| 260 |     } | 
|---|
| 261 |     // returns 0 if nothing has been found | 
|---|
| 262 |     return temp; | 
|---|
| 263 |   } | 
|---|
| 264 |  | 
|---|
| 265 |   /** | 
|---|
| 266 |   * This function goes forward through the list and looks for an element with clientID | 
|---|
| 267 |   * This function should only be applied to the head of the list | 
|---|
| 268 |   * @param peer peer to look for | 
|---|
| 269 |   * @return pointer to the element in the list | 
|---|
| 270 |   */ | 
|---|
| 271 |   ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) { | 
|---|
| 272 |     ClientInformation *temp = this; | 
|---|
| 273 |     while(temp!=0){ | 
|---|
| 274 |       if(temp->head_){ | 
|---|
| 275 |         temp = temp->next(); | 
|---|
| 276 |         continue; | 
|---|
| 277 |       } | 
|---|
| 278 |       if(temp->getPeer()->address.host==address->host && temp->getPeer()->address.port == address->port) | 
|---|
| 279 |         break; | 
|---|
| 280 |       temp = temp->next(); | 
|---|
| 281 |     } | 
|---|
| 282 |     // returns 0 if nothing has been found | 
|---|
| 283 |     return temp; | 
|---|
| 284 |   } | 
|---|
| 285 |  | 
|---|
| 286 |   bool ClientInformation::setSynched(bool s) { | 
|---|
| 287 |     if(!this) | 
|---|
| 288 |       return false; | 
|---|
| 289 |     synched_=s; | 
|---|
| 290 |     return true; | 
|---|
| 291 |   } | 
|---|
| 292 |  | 
|---|
| 293 |   bool ClientInformation::getSynched() { | 
|---|
| 294 |     if(this) | 
|---|
| 295 |       return synched_; | 
|---|
| 296 |     else | 
|---|
| 297 |       return false; | 
|---|
| 298 |   } | 
|---|
| 299 |  | 
|---|
| 300 | } | 
|---|