[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 | */ |
---|
| 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 | ClientInformation::ClientInformation() { |
---|
| 48 | gamestateID_=GAMESTATEID_INITIAL; |
---|
| 49 | preve=0; |
---|
| 50 | nexte=0; |
---|
| 51 | this->head=false; |
---|
| 52 | synched_=false; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | ClientInformation::ClientInformation(bool head) { |
---|
| 56 | gamestateID_=GAMESTATEID_INITIAL; |
---|
| 57 | preve=0; |
---|
| 58 | nexte=0; |
---|
| 59 | this->head=head; |
---|
| 60 | synched_=false; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | // ClientInformation::ClientInformation(ClientInformation *prev) { |
---|
| 64 | // if(prev->next()!=0){ |
---|
| 65 | // this->nexte=prev->next(); |
---|
| 66 | // this->nexte->setPrev(this); |
---|
| 67 | // } |
---|
| 68 | // else |
---|
| 69 | // this->nexte = 0; |
---|
| 70 | // prev->setNext(this); |
---|
| 71 | // this->preve = pref; |
---|
| 72 | // } |
---|
| 73 | // |
---|
| 74 | // ClientInformation::ClientInformation(ClientInformation *prev, ClientInformation *next){ |
---|
| 75 | // this->nexte = next; |
---|
| 76 | // this->preve = prev; |
---|
| 77 | // this->preve->setNext(this); |
---|
| 78 | // this->nexte->setPrev(this); |
---|
| 79 | // } |
---|
| 80 | |
---|
| 81 | ClientInformation::~ClientInformation() { |
---|
| 82 | if(preve!=0) |
---|
| 83 | preve->setNext(this->nexte); |
---|
| 84 | if(nexte!=0) |
---|
| 85 | nexte->setPrev(this->preve); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | ClientInformation *ClientInformation::next() { |
---|
| 89 | if(this!=0) |
---|
| 90 | return this->nexte; |
---|
| 91 | else |
---|
| 92 | return 0; |
---|
| 93 | } |
---|
| 94 | ClientInformation *ClientInformation::prev() { |
---|
| 95 | if(this!=0) |
---|
| 96 | return this->preve; |
---|
| 97 | else |
---|
| 98 | return 0; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | bool ClientInformation::setPrev(ClientInformation *prev) { |
---|
| 102 | if(!head) |
---|
| 103 | this->preve = prev; |
---|
| 104 | else |
---|
| 105 | return false; |
---|
| 106 | return true; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | bool ClientInformation::setNext(ClientInformation *next) { |
---|
| 110 | this->nexte = next; |
---|
| 111 | return true; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | ClientInformation *ClientInformation::insertAfter(ClientInformation *ins) { |
---|
| 115 | this->nexte->setPrev(ins); |
---|
| 116 | ins->setNext(this->nexte); |
---|
| 117 | ins->setPrev(this); |
---|
| 118 | this->nexte = ins; |
---|
| 119 | return ins; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){ |
---|
| 123 | this->prev()->setNext(ins); |
---|
| 124 | ins->setPrev(this->preve); |
---|
| 125 | this->preve=ins; |
---|
| 126 | ins->setNext(this); |
---|
| 127 | return ins; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void ClientInformation::setID(int clientID){ |
---|
| 131 | clientID_ = clientID; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | void ClientInformation::setPeer(ENetPeer *peer){ |
---|
| 135 | peer_ = peer; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void ClientInformation::setGamestateID(int id){ |
---|
| 139 | gamestateID_=id; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | int ClientInformation::getID() { |
---|
| 143 | return clientID_; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | ENetPeer *ClientInformation::getPeer() { |
---|
| 147 | return peer_; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | int ClientInformation::getGamestateID() { |
---|
| 151 | return gamestateID_; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | ClientInformation *ClientInformation::insertBack(ClientInformation *ins) { |
---|
| 155 | ClientInformation *temp = this; |
---|
| 156 | while(temp->next()!=0){ |
---|
| 157 | temp = temp->next(); |
---|
| 158 | } |
---|
| 159 | temp->setNext(ins); |
---|
| 160 | ins->setPrev(temp); |
---|
| 161 | return ins; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | bool ClientInformation::removeClient(int clientID) { |
---|
| 165 | ClientInformation *temp = this; |
---|
| 166 | while(temp!=0 && temp->getID()!=clientID) |
---|
| 167 | temp = temp->next(); |
---|
| 168 | if(temp==0) |
---|
| 169 | return false; |
---|
| 170 | delete temp; |
---|
| 171 | return true; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | bool ClientInformation::removeClient(ENetPeer *peer) { |
---|
| 175 | ClientInformation *temp = this; |
---|
| 176 | while(temp!=0){ |
---|
| 177 | if(!temp->head) |
---|
| 178 | if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port) |
---|
| 179 | break; |
---|
| 180 | temp = temp->next(); |
---|
| 181 | } |
---|
| 182 | if(temp==0) |
---|
| 183 | return false; |
---|
| 184 | delete temp; |
---|
| 185 | return true; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | * This function goes forward through the list and looks for an element with clientID |
---|
| 190 | * This function should only be applied to the head of the list |
---|
| 191 | * @param clientID id to look for |
---|
| 192 | * @return pointer to the last element in the list or 0 if the search was unsuccessfull |
---|
| 193 | */ |
---|
| 194 | ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) { |
---|
| 195 | ClientInformation *temp = this; |
---|
| 196 | if (temp->head) |
---|
| 197 | temp=temp->next(); |
---|
| 198 | //bugfix: temp to temp->next(), get last elem if not found, not segflt |
---|
| 199 | while(temp->next()!=0 && temp->getID()!=clientID){ |
---|
| 200 | temp = temp->next(); |
---|
| 201 | } |
---|
| 202 | // returns 0 if nothing has been found |
---|
| 203 | return temp; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * This function goes forward through the list and looks for an element with clientID |
---|
| 208 | * This function should only be applied to the head of the list |
---|
| 209 | * @param peer peer to look for |
---|
| 210 | * @return pointer to the element in the list |
---|
| 211 | */ |
---|
| 212 | ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) { |
---|
| 213 | ClientInformation *temp = this; |
---|
| 214 | //bugfix: temp to temp->next(), get last elem if not found, not segflt |
---|
| 215 | while(temp->next()!=0){ |
---|
| 216 | if(temp->head){ |
---|
| 217 | temp = temp->next(); |
---|
| 218 | continue; |
---|
| 219 | } |
---|
| 220 | if(temp->getPeer()->address.host==address->host && temp->getPeer()->address.port == address->port) |
---|
| 221 | break; |
---|
| 222 | temp = temp->next(); |
---|
| 223 | } |
---|
| 224 | // returns 0 if nothing has been found |
---|
| 225 | return temp; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void ClientInformation::setSynched(bool s) { |
---|
| 229 | synched_=s; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | bool ClientInformation::getSynched() { |
---|
| 233 | return synched_; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | } |
---|