| [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 |  | 
|---|
|  | 43 | #include <iostream> //debug | 
|---|
|  | 44 |  | 
|---|
| [2112] | 45 | namespace orxonox | 
|---|
| [1505] | 46 | { | 
|---|
| [1953] | 47 |  | 
|---|
| [1735] | 48 | ClientInformation *ClientInformation::head_=0; | 
|---|
| [1953] | 49 |  | 
|---|
| [1505] | 50 | ClientInformation::ClientInformation() { | 
|---|
| [1735] | 51 | if(!head_) | 
|---|
|  | 52 | head_=this; | 
|---|
| [1505] | 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()); | 
|---|
| [1990] | 65 | if(this==head_) | 
|---|
|  | 66 | head_=next(); | 
|---|
| [1505] | 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) { | 
|---|
| [1735] | 83 | if(this==head_) | 
|---|
|  | 84 | head_=prev; | 
|---|
|  | 85 | this->preve = prev; | 
|---|
| [1505] | 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 |  | 
|---|
| [1735] | 125 | bool ClientInformation::setGamestateID(int id){ | 
|---|
| [1505] | 126 | if(!this) | 
|---|
|  | 127 | return false; | 
|---|
|  | 128 | gamestateID_=id; | 
|---|
|  | 129 | return true; | 
|---|
|  | 130 | } | 
|---|
| [1953] | 131 |  | 
|---|
| [1505] | 132 | bool ClientInformation::setPartialGamestateID(int id){ | 
|---|
|  | 133 | if(!this) | 
|---|
|  | 134 | return false; | 
|---|
|  | 135 | partialGamestateID_=id; | 
|---|
|  | 136 | return true; | 
|---|
|  | 137 | } | 
|---|
|  | 138 |  | 
|---|
| [1955] | 139 | unsigned int ClientInformation::getID() { | 
|---|
| [1505] | 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 | } | 
|---|
| [1953] | 152 |  | 
|---|
| [1505] | 153 | int ClientInformation::getFailures(){ | 
|---|
|  | 154 | return failures_; | 
|---|
|  | 155 | } | 
|---|
|  | 156 | void ClientInformation::addFailure(){ | 
|---|
|  | 157 | failures_++; | 
|---|
|  | 158 | } | 
|---|
|  | 159 | void ClientInformation::resetFailures(){ | 
|---|
|  | 160 | failures_=0; | 
|---|
|  | 161 | } | 
|---|
| [1953] | 162 |  | 
|---|
| [1534] | 163 | enet_uint32 ClientInformation::getRTT(){ | 
|---|
| [2009] | 164 | return this->peer_->roundTripTime; | 
|---|
| [1534] | 165 | } | 
|---|
| [1953] | 166 |  | 
|---|
| [2009] | 167 | double ClientInformation::getPacketLoss(){ | 
|---|
|  | 168 | return ((double)this->peer_->packetLoss)/ENET_PEER_PACKET_LOSS_SCALE; | 
|---|
| [1534] | 169 | } | 
|---|
| [1505] | 170 |  | 
|---|
| [1955] | 171 | unsigned int ClientInformation::getGamestateID() { | 
|---|
| [1505] | 172 | if(this) | 
|---|
|  | 173 | return gamestateID_; | 
|---|
|  | 174 | else | 
|---|
| [1960] | 175 | return (unsigned int)-1; | 
|---|
| [1505] | 176 | } | 
|---|
| [1953] | 177 |  | 
|---|
| [1955] | 178 | unsigned int ClientInformation::getPartialGamestateID() { | 
|---|
| [1505] | 179 | if(this) | 
|---|
|  | 180 | return partialGamestateID_; | 
|---|
|  | 181 | else | 
|---|
| [1960] | 182 | return (unsigned int)-1; | 
|---|
| [1505] | 183 | } | 
|---|
|  | 184 |  | 
|---|
|  | 185 | ClientInformation *ClientInformation::insertBack(ClientInformation *ins) { | 
|---|
| [1735] | 186 | ClientInformation *temp = head_; | 
|---|
| [1952] | 187 | if(temp==ins){ | 
|---|
| [1735] | 188 | return head_; | 
|---|
|  | 189 | } | 
|---|
| [1505] | 190 | while(temp->next()!=0){ | 
|---|
|  | 191 | temp = temp->next(); | 
|---|
|  | 192 | } | 
|---|
|  | 193 | temp->setNext(ins); | 
|---|
|  | 194 | ins->setPrev(temp); | 
|---|
|  | 195 | return ins; | 
|---|
|  | 196 | } | 
|---|
|  | 197 |  | 
|---|
| [1955] | 198 | bool ClientInformation::removeClient(unsigned int clientID) { | 
|---|
| [1953] | 199 | if((unsigned int)clientID==CLIENTID_UNKNOWN) | 
|---|
| [1505] | 200 | return false; | 
|---|
| [1735] | 201 | ClientInformation *temp = head_; | 
|---|
| [1505] | 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) { | 
|---|
| [1735] | 211 | if(!peer) | 
|---|
| [1505] | 212 | return false; | 
|---|
| [1735] | 213 | ClientInformation *temp = head_; | 
|---|
| [1505] | 214 | while(temp!=0){ | 
|---|
| [1735] | 215 | if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port) | 
|---|
|  | 216 | break; | 
|---|
| [1505] | 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 | */ | 
|---|
| [1955] | 231 | ClientInformation *ClientInformation::findClient(unsigned int clientID, bool look_backwards) { | 
|---|
| [1735] | 232 | ClientInformation *temp = head_; | 
|---|
| [1505] | 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) { | 
|---|
| [1735] | 247 | ClientInformation *temp = head_; | 
|---|
| [1505] | 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 | } | 
|---|