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