[432] | 1 | // |
---|
| 2 | // C++ Implementation: ClientInformation |
---|
| 3 | // |
---|
| 4 | // Description: |
---|
| 5 | // |
---|
| 6 | // |
---|
| 7 | // Author: <>, (C) 2007 |
---|
| 8 | // |
---|
| 9 | // Copyright: See COPYING file that comes with this distribution |
---|
| 10 | // |
---|
| 11 | // |
---|
| 12 | #include "ClientInformation.h" |
---|
| 13 | |
---|
| 14 | namespace network { |
---|
| 15 | |
---|
| 16 | ClientInformation::ClientInformation() |
---|
| 17 | { |
---|
[436] | 18 | gamestateID_=GAMESTATEID_INITIAL; |
---|
[432] | 19 | preve=0; |
---|
| 20 | nexte=0; |
---|
[436] | 21 | this->head=false; |
---|
[432] | 22 | } |
---|
[436] | 23 | |
---|
| 24 | ClientInformation::ClientInformation(bool head) |
---|
| 25 | { |
---|
| 26 | gamestateID_=GAMESTATEID_INITIAL; |
---|
| 27 | preve=0; |
---|
| 28 | nexte=0; |
---|
| 29 | this->head=head; |
---|
| 30 | } |
---|
[432] | 31 | // |
---|
| 32 | |
---|
| 33 | // ClientInformation::ClientInformation(ClientInformation *prev){ |
---|
| 34 | // if(prev->next()!=0){ |
---|
| 35 | // this->nexte=prev->next(); |
---|
| 36 | // this->nexte->setPrev(this); |
---|
| 37 | // } |
---|
| 38 | // else |
---|
| 39 | // this->nexte = 0; |
---|
| 40 | // prev->setNext(this); |
---|
| 41 | // this->preve = pref; |
---|
| 42 | // } |
---|
| 43 | // |
---|
| 44 | // ClientInformation::ClientInformation(ClientInformation *prev, ClientInformation *next){ |
---|
| 45 | // this->nexte = next; |
---|
| 46 | // this->preve = prev; |
---|
| 47 | // this->preve->setNext(this); |
---|
| 48 | // this->nexte->setPrev(this); |
---|
| 49 | // } |
---|
| 50 | |
---|
| 51 | ClientInformation::~ClientInformation() |
---|
| 52 | { |
---|
| 53 | if(preve!=0) |
---|
| 54 | preve->setNext(this->nexte); |
---|
| 55 | if(nexte!=0) |
---|
| 56 | nexte->setPrev(this->preve); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | ClientInformation *ClientInformation::next(){ |
---|
[442] | 60 | if(this!=0) |
---|
| 61 | return this->nexte; |
---|
| 62 | else |
---|
| 63 | return 0; |
---|
[432] | 64 | } |
---|
| 65 | ClientInformation *ClientInformation::prev(){ |
---|
[442] | 66 | if(this!=0) |
---|
| 67 | return this->preve; |
---|
| 68 | else |
---|
| 69 | return 0; |
---|
[432] | 70 | } |
---|
| 71 | |
---|
[436] | 72 | bool ClientInformation::setPrev(ClientInformation *prev){ |
---|
| 73 | if(!head) |
---|
| 74 | this->preve = prev; |
---|
| 75 | else |
---|
| 76 | return false; |
---|
| 77 | return true; |
---|
[432] | 78 | } |
---|
| 79 | |
---|
[436] | 80 | bool ClientInformation::setNext(ClientInformation *next){ |
---|
[432] | 81 | this->nexte = next; |
---|
[436] | 82 | return true; |
---|
[432] | 83 | } |
---|
| 84 | |
---|
[436] | 85 | ClientInformation *ClientInformation::insertAfter(ClientInformation *ins){ |
---|
[432] | 86 | this->nexte->setPrev(ins); |
---|
| 87 | ins->setNext(this->nexte); |
---|
| 88 | ins->setPrev(this); |
---|
| 89 | this->nexte = ins; |
---|
[436] | 90 | return ins; |
---|
[432] | 91 | } |
---|
| 92 | |
---|
[436] | 93 | ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){ |
---|
[432] | 94 | this->prev()->setNext(ins); |
---|
| 95 | ins->setPrev(this->preve); |
---|
| 96 | this->preve=ins; |
---|
| 97 | ins->setNext(this); |
---|
[436] | 98 | return ins; |
---|
[432] | 99 | } |
---|
| 100 | |
---|
[436] | 101 | void ClientInformation::setID(int clientID){ |
---|
| 102 | clientID_ = clientID; |
---|
[432] | 103 | } |
---|
[436] | 104 | void ClientInformation::setPeer(ENetPeer *peer){ |
---|
| 105 | peer_ = peer; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void ClientInformation::setGamestateID(int id){ |
---|
| 109 | gamestateID_=id; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | int ClientInformation::getID(){ |
---|
| 113 | return clientID_; |
---|
| 114 | } |
---|
| 115 | ENetPeer *ClientInformation::getPeer(){ |
---|
| 116 | return peer_; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | int ClientInformation::getGamestateID(){ |
---|
| 120 | return gamestateID_; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | ClientInformation *ClientInformation::insertBack(ClientInformation *ins){ |
---|
| 124 | ClientInformation *temp = this; |
---|
| 125 | while(temp->next()!=0){ |
---|
| 126 | temp = temp->next(); |
---|
| 127 | } |
---|
| 128 | temp->setNext(ins); |
---|
| 129 | ins->setPrev(temp); |
---|
| 130 | return ins; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | bool ClientInformation::removeClient(int clientID){ |
---|
| 134 | ClientInformation *temp = this; |
---|
| 135 | while(temp!=0 && temp->getID()!=clientID) |
---|
| 136 | temp = temp->next(); |
---|
| 137 | if(temp==0) |
---|
| 138 | return false; |
---|
| 139 | delete temp; |
---|
| 140 | return true; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | bool ClientInformation::removeClient(ENetPeer *peer){ |
---|
| 144 | ClientInformation *temp = this; |
---|
[446] | 145 | while(temp!=0){ |
---|
| 146 | if(!temp->head) |
---|
| 147 | if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port) |
---|
| 148 | break; |
---|
[436] | 149 | temp = temp->next(); |
---|
[446] | 150 | } |
---|
[436] | 151 | if(temp==0) |
---|
| 152 | return false; |
---|
| 153 | delete temp; |
---|
| 154 | return true; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * This function goes forward through the list and looks for an element with clientID |
---|
| 159 | * This function should only be applied to the head of the list |
---|
| 160 | * @param clientID id to look for |
---|
| 161 | * @return pointer to the element in the list or 0 if the search was unsuccessfull |
---|
| 162 | */ |
---|
| 163 | ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards){ |
---|
| 164 | ClientInformation *temp = this; |
---|
[446] | 165 | while(temp!=0 && temp->getID()!=clientID){ |
---|
| 166 | if (temp->head) |
---|
| 167 | continue; |
---|
[436] | 168 | temp = temp->next(); |
---|
[446] | 169 | } |
---|
[436] | 170 | // returns 0 if nothing has been found |
---|
| 171 | return temp; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * This function goes forward through the list and looks for an element with clientID |
---|
| 176 | * This function should only be applied to the head of the list |
---|
| 177 | * @param peer peer to look for |
---|
| 178 | * @return pointer to the element in the list |
---|
| 179 | */ |
---|
| 180 | ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards){ |
---|
| 181 | ClientInformation *temp = this; |
---|
[444] | 182 | while(temp!=0){ |
---|
| 183 | if(temp->head){ |
---|
| 184 | temp = temp->next(); |
---|
| 185 | continue; |
---|
| 186 | } |
---|
| 187 | if(temp->getPeer()->address.host==address->host && temp->getPeer()->address.port == address->port) |
---|
| 188 | break; |
---|
[436] | 189 | temp = temp->next(); |
---|
[444] | 190 | } |
---|
[436] | 191 | // returns 0 if nothing has been found |
---|
| 192 | return temp; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | } |
---|