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