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