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