| [1505] | 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 |  | 
|---|
| [2773] | 43 | #include <enet/enet.h> | 
|---|
| [1505] | 44 | #include <iostream> //debug | 
|---|
 | 45 |  | 
|---|
| [2171] | 46 | namespace orxonox | 
|---|
| [1505] | 47 | { | 
|---|
| [2662] | 48 |    | 
|---|
| [2087] | 49 |  | 
|---|
| [1735] | 50 |   ClientInformation *ClientInformation::head_=0; | 
|---|
| [2087] | 51 |  | 
|---|
| [1505] | 52 |   ClientInformation::ClientInformation() { | 
|---|
| [1735] | 53 |     if(!head_) | 
|---|
 | 54 |       head_=this; | 
|---|
| [1505] | 55 |     gamestateID_=GAMESTATEID_INITIAL; | 
|---|
 | 56 |     preve=0; | 
|---|
 | 57 |     nexte=0; | 
|---|
 | 58 |     partialGamestateID_=GAMESTATEID_INITIAL-1; | 
|---|
 | 59 |     synched_=false; | 
|---|
 | 60 |   } | 
|---|
 | 61 |  | 
|---|
 | 62 |   ClientInformation::~ClientInformation() { | 
|---|
 | 63 |     if(prev()!=0) | 
|---|
 | 64 |       prev()->setNext(this->next()); | 
|---|
 | 65 |     if(next()!=0) | 
|---|
 | 66 |       next()->setPrev(this->prev()); | 
|---|
| [2087] | 67 |     if(this==head_) | 
|---|
 | 68 |       head_=next(); | 
|---|
| [1505] | 69 |   } | 
|---|
 | 70 |  | 
|---|
 | 71 |   ClientInformation *ClientInformation::next() { | 
|---|
 | 72 |     if(this!=0) | 
|---|
 | 73 |       return this->nexte; | 
|---|
 | 74 |     else | 
|---|
 | 75 |       return 0; | 
|---|
 | 76 |   } | 
|---|
 | 77 |   ClientInformation *ClientInformation::prev() { | 
|---|
 | 78 |     if(this!=0) | 
|---|
 | 79 |       return this->preve; | 
|---|
 | 80 |     else | 
|---|
 | 81 |       return 0; | 
|---|
 | 82 |   } | 
|---|
 | 83 |  | 
|---|
 | 84 |   bool ClientInformation::setPrev(ClientInformation *prev) { | 
|---|
| [1735] | 85 |     if(this==head_) | 
|---|
 | 86 |       head_=prev; | 
|---|
 | 87 |     this->preve = prev; | 
|---|
| [1505] | 88 |     return true; | 
|---|
 | 89 |   } | 
|---|
 | 90 |  | 
|---|
 | 91 |   bool ClientInformation::setNext(ClientInformation *next) { | 
|---|
 | 92 |     this->nexte = next; | 
|---|
 | 93 |     return true; | 
|---|
 | 94 |   } | 
|---|
 | 95 |  | 
|---|
 | 96 |   ClientInformation *ClientInformation::insertAfter(ClientInformation *ins) { | 
|---|
 | 97 |     this->next()->setPrev(ins); | 
|---|
 | 98 |     ins->setNext(this->next()); | 
|---|
 | 99 |     ins->setPrev(this); | 
|---|
 | 100 |     this->nexte = ins; | 
|---|
 | 101 |     return ins; | 
|---|
 | 102 |   } | 
|---|
 | 103 |  | 
|---|
 | 104 |   ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){ | 
|---|
 | 105 |     if(!this) | 
|---|
 | 106 |       return NULL; | 
|---|
 | 107 |     this->prev()->setNext(ins); | 
|---|
 | 108 |     ins->setPrev(this->prev()); | 
|---|
 | 109 |     this->preve=ins; | 
|---|
 | 110 |     ins->setNext(this); | 
|---|
 | 111 |     return ins; | 
|---|
 | 112 |   } | 
|---|
 | 113 |  | 
|---|
 | 114 |   void ClientInformation::setID(int clientID){ | 
|---|
 | 115 |     if(!this) | 
|---|
 | 116 |       return; | 
|---|
 | 117 |     clientID_ = clientID; | 
|---|
 | 118 |   } | 
|---|
 | 119 |  | 
|---|
 | 120 |   bool ClientInformation::setPeer(ENetPeer *peer){ | 
|---|
 | 121 |     if(!this) | 
|---|
 | 122 |       return false; | 
|---|
 | 123 |     peer_ = peer; | 
|---|
 | 124 |     return true; | 
|---|
 | 125 |   } | 
|---|
 | 126 |  | 
|---|
| [1735] | 127 |   bool ClientInformation::setGamestateID(int id){ | 
|---|
| [1505] | 128 |     if(!this) | 
|---|
 | 129 |       return false; | 
|---|
 | 130 |     gamestateID_=id; | 
|---|
 | 131 |     return true; | 
|---|
 | 132 |   } | 
|---|
| [2087] | 133 |  | 
|---|
| [1505] | 134 |   bool ClientInformation::setPartialGamestateID(int id){ | 
|---|
 | 135 |     if(!this) | 
|---|
 | 136 |       return false; | 
|---|
 | 137 |     partialGamestateID_=id; | 
|---|
 | 138 |     return true; | 
|---|
 | 139 |   } | 
|---|
 | 140 |  | 
|---|
| [2087] | 141 |   unsigned int ClientInformation::getID() { | 
|---|
| [1505] | 142 |     if(!this) | 
|---|
 | 143 |       return CLIENTID_UNKNOWN; | 
|---|
 | 144 |     else | 
|---|
 | 145 |       return clientID_; | 
|---|
 | 146 |   } | 
|---|
 | 147 |  | 
|---|
 | 148 |   ENetPeer *ClientInformation::getPeer() { | 
|---|
 | 149 |     if(this) | 
|---|
 | 150 |       return peer_; | 
|---|
 | 151 |     else | 
|---|
 | 152 |       return NULL; | 
|---|
 | 153 |   } | 
|---|
| [2087] | 154 |  | 
|---|
| [1505] | 155 |   int ClientInformation::getFailures(){ | 
|---|
 | 156 |     return failures_; | 
|---|
 | 157 |   } | 
|---|
 | 158 |   void ClientInformation::addFailure(){ | 
|---|
 | 159 |     failures_++; | 
|---|
 | 160 |   } | 
|---|
 | 161 |   void ClientInformation::resetFailures(){ | 
|---|
 | 162 |     failures_=0; | 
|---|
 | 163 |   } | 
|---|
| [2087] | 164 |  | 
|---|
| [2773] | 165 |   uint32_t ClientInformation::getRTT(){ | 
|---|
| [2087] | 166 |     return this->peer_->roundTripTime; | 
|---|
| [1534] | 167 |   } | 
|---|
| [2087] | 168 |  | 
|---|
 | 169 |   double ClientInformation::getPacketLoss(){ | 
|---|
 | 170 |     return ((double)this->peer_->packetLoss)/ENET_PEER_PACKET_LOSS_SCALE; | 
|---|
| [1534] | 171 |   } | 
|---|
| [1505] | 172 |  | 
|---|
| [2087] | 173 |   unsigned int ClientInformation::getGamestateID() { | 
|---|
| [1505] | 174 |     if(this) | 
|---|
 | 175 |       return gamestateID_; | 
|---|
 | 176 |     else | 
|---|
| [2087] | 177 |       return (unsigned int)-1; | 
|---|
| [1505] | 178 |   } | 
|---|
| [2087] | 179 |  | 
|---|
 | 180 |   unsigned int ClientInformation::getPartialGamestateID() { | 
|---|
| [1505] | 181 |     if(this) | 
|---|
 | 182 |       return partialGamestateID_; | 
|---|
 | 183 |     else | 
|---|
| [2087] | 184 |       return (unsigned int)-1; | 
|---|
| [1505] | 185 |   } | 
|---|
 | 186 |  | 
|---|
 | 187 |   ClientInformation *ClientInformation::insertBack(ClientInformation *ins) { | 
|---|
| [1735] | 188 |     ClientInformation *temp = head_; | 
|---|
| [2087] | 189 |     if(temp==ins){ | 
|---|
| [1735] | 190 |       return head_; | 
|---|
 | 191 |     } | 
|---|
| [1505] | 192 |     while(temp->next()!=0){ | 
|---|
 | 193 |       temp = temp->next(); | 
|---|
 | 194 |     } | 
|---|
 | 195 |     temp->setNext(ins); | 
|---|
 | 196 |     ins->setPrev(temp); | 
|---|
 | 197 |     return ins; | 
|---|
 | 198 |   } | 
|---|
 | 199 |  | 
|---|
| [2087] | 200 |   bool ClientInformation::removeClient(unsigned int clientID) { | 
|---|
 | 201 |     if((unsigned int)clientID==CLIENTID_UNKNOWN) | 
|---|
| [1505] | 202 |       return false; | 
|---|
| [1735] | 203 |     ClientInformation *temp = head_; | 
|---|
| [1505] | 204 |     while(temp!=0 && temp->getID()!=clientID) | 
|---|
 | 205 |       temp = temp->next(); | 
|---|
 | 206 |     if(temp==0) | 
|---|
 | 207 |       return false; | 
|---|
 | 208 |     delete temp; | 
|---|
 | 209 |     return true; | 
|---|
 | 210 |   } | 
|---|
 | 211 |  | 
|---|
 | 212 |   bool ClientInformation::removeClient(ENetPeer *peer) { | 
|---|
| [1735] | 213 |     if(!peer) | 
|---|
| [1505] | 214 |       return false; | 
|---|
| [1735] | 215 |     ClientInformation *temp = head_; | 
|---|
| [1505] | 216 |     while(temp!=0){ | 
|---|
| [1735] | 217 |       if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port) | 
|---|
 | 218 |         break; | 
|---|
| [1505] | 219 |       temp = temp->next(); | 
|---|
 | 220 |     } | 
|---|
 | 221 |     if(temp==0) | 
|---|
 | 222 |       return false; | 
|---|
 | 223 |     delete temp; | 
|---|
 | 224 |     return true; | 
|---|
 | 225 |   } | 
|---|
 | 226 |  | 
|---|
 | 227 |   /** | 
|---|
 | 228 |   * This function goes forward through the list and looks for an element with clientID | 
|---|
 | 229 |   * This function should only be applied to the head of the list | 
|---|
 | 230 |   * @param clientID id to look for | 
|---|
 | 231 |   * @return pointer to the last element in the list or 0 if the search was unsuccessfull | 
|---|
 | 232 |   */ | 
|---|
| [2087] | 233 |   ClientInformation *ClientInformation::findClient(unsigned int clientID, bool look_backwards) { | 
|---|
| [1735] | 234 |     ClientInformation *temp = head_; | 
|---|
| [1505] | 235 |     while(temp!=0 && temp->getID()!=clientID){ | 
|---|
 | 236 |       temp = temp->next(); | 
|---|
 | 237 |     } | 
|---|
 | 238 |     // returns 0 if nothing has been found | 
|---|
 | 239 |     return temp; | 
|---|
 | 240 |   } | 
|---|
 | 241 |  | 
|---|
 | 242 |   /** | 
|---|
 | 243 |   * This function goes forward through the list and looks for an element with clientID | 
|---|
 | 244 |   * This function should only be applied to the head of the list | 
|---|
 | 245 |   * @param peer peer to look for | 
|---|
 | 246 |   * @return pointer to the element in the list | 
|---|
 | 247 |   */ | 
|---|
 | 248 |   ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) { | 
|---|
| [1735] | 249 |     ClientInformation *temp = head_; | 
|---|
| [1505] | 250 |     while(temp!=0){ | 
|---|
 | 251 |       if(temp->getPeer()->address.host==address->host && temp->getPeer()->address.port == address->port) | 
|---|
 | 252 |         break; | 
|---|
 | 253 |       temp = temp->next(); | 
|---|
 | 254 |     } | 
|---|
 | 255 |     // returns 0 if nothing has been found | 
|---|
 | 256 |     return temp; | 
|---|
 | 257 |   } | 
|---|
 | 258 |  | 
|---|
 | 259 |   bool ClientInformation::setSynched(bool s) { | 
|---|
 | 260 |     if(!this) | 
|---|
 | 261 |       return false; | 
|---|
 | 262 |     synched_=s; | 
|---|
 | 263 |     return true; | 
|---|
 | 264 |   } | 
|---|
 | 265 |  | 
|---|
 | 266 |   bool ClientInformation::getSynched() { | 
|---|
 | 267 |     if(this) | 
|---|
 | 268 |       return synched_; | 
|---|
 | 269 |     else | 
|---|
 | 270 |       return false; | 
|---|
 | 271 |   } | 
|---|
 | 272 |  | 
|---|
 | 273 | } | 
|---|