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