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