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