[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(){ |
---|
| 60 | return this->nexte; |
---|
| 61 | } |
---|
| 62 | ClientInformation *ClientInformation::prev(){ |
---|
| 63 | return this->preve; |
---|
| 64 | } |
---|
| 65 | |
---|
[436] | 66 | bool ClientInformation::setPrev(ClientInformation *prev){ |
---|
| 67 | if(!head) |
---|
| 68 | this->preve = prev; |
---|
| 69 | else |
---|
| 70 | return false; |
---|
| 71 | return true; |
---|
[432] | 72 | } |
---|
| 73 | |
---|
[436] | 74 | bool ClientInformation::setNext(ClientInformation *next){ |
---|
[432] | 75 | this->nexte = next; |
---|
[436] | 76 | return true; |
---|
[432] | 77 | } |
---|
| 78 | |
---|
[436] | 79 | ClientInformation *ClientInformation::insertAfter(ClientInformation *ins){ |
---|
[432] | 80 | this->nexte->setPrev(ins); |
---|
| 81 | ins->setNext(this->nexte); |
---|
| 82 | ins->setPrev(this); |
---|
| 83 | this->nexte = ins; |
---|
[436] | 84 | return ins; |
---|
[432] | 85 | } |
---|
| 86 | |
---|
[436] | 87 | ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){ |
---|
[432] | 88 | this->prev()->setNext(ins); |
---|
| 89 | ins->setPrev(this->preve); |
---|
| 90 | this->preve=ins; |
---|
| 91 | ins->setNext(this); |
---|
[436] | 92 | return ins; |
---|
[432] | 93 | } |
---|
| 94 | |
---|
[436] | 95 | void ClientInformation::setID(int clientID){ |
---|
| 96 | clientID_ = clientID; |
---|
[432] | 97 | } |
---|
[436] | 98 | void ClientInformation::setPeer(ENetPeer *peer){ |
---|
| 99 | peer_ = peer; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | void ClientInformation::setGamestateID(int id){ |
---|
| 103 | gamestateID_=id; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | int ClientInformation::getID(){ |
---|
| 107 | return clientID_; |
---|
| 108 | } |
---|
| 109 | ENetPeer *ClientInformation::getPeer(){ |
---|
| 110 | return peer_; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | int ClientInformation::getGamestateID(){ |
---|
| 114 | return gamestateID_; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | ClientInformation *ClientInformation::insertBack(ClientInformation *ins){ |
---|
| 118 | ClientInformation *temp = this; |
---|
| 119 | while(temp->next()!=0){ |
---|
| 120 | temp = temp->next(); |
---|
| 121 | } |
---|
| 122 | temp->setNext(ins); |
---|
| 123 | ins->setPrev(temp); |
---|
| 124 | return ins; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | bool ClientInformation::removeClient(int clientID){ |
---|
| 128 | ClientInformation *temp = this; |
---|
| 129 | while(temp!=0 && temp->getID()!=clientID) |
---|
| 130 | temp = temp->next(); |
---|
| 131 | if(temp==0) |
---|
| 132 | return false; |
---|
| 133 | delete temp; |
---|
| 134 | return true; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | bool ClientInformation::removeClient(ENetPeer *peer){ |
---|
| 138 | ClientInformation *temp = this; |
---|
| 139 | while(temp!=0 && (temp->getPeer()->address.host!=peer->address.host || temp->getPeer()->address.port!=peer->address.port)) |
---|
| 140 | temp = temp->next(); |
---|
| 141 | if(temp==0) |
---|
| 142 | return false; |
---|
| 143 | delete temp; |
---|
| 144 | return true; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * This function goes forward through the list and looks for an element with clientID |
---|
| 149 | * This function should only be applied to the head of the list |
---|
| 150 | * @param clientID id to look for |
---|
| 151 | * @return pointer to the element in the list or 0 if the search was unsuccessfull |
---|
| 152 | */ |
---|
| 153 | ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards){ |
---|
| 154 | ClientInformation *temp = this; |
---|
| 155 | while(temp!=0 && temp->getID()!=clientID) |
---|
| 156 | temp = temp->next(); |
---|
| 157 | // returns 0 if nothing has been found |
---|
| 158 | return temp; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * This function goes forward through the list and looks for an element with clientID |
---|
| 163 | * This function should only be applied to the head of the list |
---|
| 164 | * @param peer peer to look for |
---|
| 165 | * @return pointer to the element in the list |
---|
| 166 | */ |
---|
| 167 | ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards){ |
---|
| 168 | ClientInformation *temp = this; |
---|
| 169 | while(temp!=0 && (temp->getPeer()->address.host!=address->host || temp->getPeer()->address.port != address->port)) |
---|
| 170 | temp = temp->next(); |
---|
| 171 | // returns 0 if nothing has been found |
---|
| 172 | return temp; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | } |
---|