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