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 | * Oliver Scheuss |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | // |
---|
30 | // C++ Implementation: Server |
---|
31 | // |
---|
32 | // Description: |
---|
33 | // |
---|
34 | // |
---|
35 | // Author: Oliver Scheuss, (C) 2007 |
---|
36 | // |
---|
37 | // Copyright: See COPYING file that comes with this distribution |
---|
38 | // |
---|
39 | // |
---|
40 | |
---|
41 | #include "Server.h" |
---|
42 | |
---|
43 | #include <enet/enet.h> |
---|
44 | #include <iostream> |
---|
45 | #include <cassert> |
---|
46 | |
---|
47 | |
---|
48 | #include "ConnectionManager.h" |
---|
49 | #include "ClientConnectionListener.h" |
---|
50 | #include "GamestateManager.h" |
---|
51 | #include "ClientInformation.h" |
---|
52 | #include "util/Sleep.h" |
---|
53 | #include "core/Clock.h" |
---|
54 | #include "core/ConsoleCommand.h" |
---|
55 | #include "core/CoreIncludes.h" |
---|
56 | #include "core/ObjectList.h" |
---|
57 | #include "packet/Chat.h" |
---|
58 | #include "packet/Packet.h" |
---|
59 | #include "packet/Welcome.h" |
---|
60 | #include "packet/DeleteObjects.h" |
---|
61 | #include "util/Convert.h" |
---|
62 | #include "ChatListener.h" |
---|
63 | #include "FunctionCallManager.h" |
---|
64 | #include "packet/FunctionIDs.h" |
---|
65 | |
---|
66 | |
---|
67 | namespace orxonox |
---|
68 | { |
---|
69 | const unsigned int MAX_FAILURES = 20; |
---|
70 | |
---|
71 | /** |
---|
72 | * Constructor for default values (bindaddress is set to ENET_HOST_ANY |
---|
73 | * |
---|
74 | */ |
---|
75 | Server::Server() { |
---|
76 | timeSinceLastUpdate_=0; |
---|
77 | connection = new ConnectionManager(); |
---|
78 | gamestates_ = new GamestateManager(); |
---|
79 | } |
---|
80 | |
---|
81 | Server::Server(int port){ |
---|
82 | timeSinceLastUpdate_=0; |
---|
83 | connection = new ConnectionManager(port); |
---|
84 | gamestates_ = new GamestateManager(); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Constructor |
---|
89 | * @param port Port to listen on |
---|
90 | * @param bindAddress Address to listen on |
---|
91 | */ |
---|
92 | Server::Server(int port, const std::string& bindAddress) { |
---|
93 | timeSinceLastUpdate_=0; |
---|
94 | connection = new ConnectionManager(port, bindAddress); |
---|
95 | gamestates_ = new GamestateManager(); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Constructor |
---|
100 | * @param port Port to listen on |
---|
101 | * @param bindAddress Address to listen on |
---|
102 | */ |
---|
103 | Server::Server(int port, const char *bindAddress) { |
---|
104 | timeSinceLastUpdate_=0; |
---|
105 | connection = new ConnectionManager(port, bindAddress); |
---|
106 | gamestates_ = new GamestateManager(); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * @brief Destructor |
---|
111 | */ |
---|
112 | Server::~Server(){ |
---|
113 | if(connection) |
---|
114 | delete connection; |
---|
115 | if(gamestates_) |
---|
116 | delete gamestates_; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * This function opens the server by creating the listener thread |
---|
121 | */ |
---|
122 | void Server::open() { |
---|
123 | connection->createListener(); |
---|
124 | return; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * This function closes the server |
---|
129 | */ |
---|
130 | void Server::close() { |
---|
131 | connection->quitListener(); |
---|
132 | return; |
---|
133 | } |
---|
134 | |
---|
135 | bool Server::processChat(const std::string& message, unsigned int playerID){ |
---|
136 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
137 | packet::Chat *chat; |
---|
138 | while(temp){ |
---|
139 | chat = new packet::Chat(message, playerID); |
---|
140 | chat->setClientID(temp->getID()); |
---|
141 | if(!chat->send()) |
---|
142 | COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; |
---|
143 | temp = temp->next(); |
---|
144 | } |
---|
145 | // COUT(1) << "Player " << playerID << ": " << message << std::endl; |
---|
146 | return true; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | /** |
---|
151 | * Run this function once every tick |
---|
152 | * calls processQueue and updateGamestate |
---|
153 | * @param time time since last tick |
---|
154 | */ |
---|
155 | void Server::update(const Clock& time) { |
---|
156 | processQueue(); |
---|
157 | gamestates_->processGamestates(); |
---|
158 | //this steers our network frequency |
---|
159 | timeSinceLastUpdate_+=time.getDeltaTime(); |
---|
160 | if(timeSinceLastUpdate_>=NETWORK_PERIOD){ |
---|
161 | timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD; |
---|
162 | updateGamestate(); |
---|
163 | FunctionCallManager::sendCalls(); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | bool Server::queuePacket(ENetPacket *packet, int clientID){ |
---|
168 | return connection->addPacket(packet, clientID); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * @brief: returns ping time to client in milliseconds |
---|
173 | */ |
---|
174 | unsigned int Server::getPing(unsigned int clientID){ |
---|
175 | assert(ClientInformation::findClient(clientID)); |
---|
176 | return ClientInformation::findClient(clientID)->getRTT(); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * @brief: return packet loss ratio to client (scales from 0 to 1) |
---|
181 | */ |
---|
182 | double Server::getPacketLoss(unsigned int clientID){ |
---|
183 | assert(ClientInformation::findClient(clientID)); |
---|
184 | return ClientInformation::findClient(clientID)->getPacketLoss(); |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * processes all the packets waiting in the queue |
---|
189 | */ |
---|
190 | void Server::processQueue() { |
---|
191 | ENetEvent *event; |
---|
192 | while(!connection->queueEmpty()){ |
---|
193 | //std::cout << "Client " << clientID << " sent: " << std::endl; |
---|
194 | //clientID here is a reference to grab clientID from ClientInformation |
---|
195 | event = connection->getEvent(); |
---|
196 | if(!event) |
---|
197 | continue; |
---|
198 | assert(event->type != ENET_EVENT_TYPE_NONE); |
---|
199 | switch( event->type ) { |
---|
200 | case ENET_EVENT_TYPE_CONNECT: |
---|
201 | COUT(3) << "processing event_Type_connect" << std::endl; |
---|
202 | addClient(event); |
---|
203 | break; |
---|
204 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
205 | if(ClientInformation::findClient(&event->peer->address)) |
---|
206 | disconnectClient(event); |
---|
207 | break; |
---|
208 | case ENET_EVENT_TYPE_RECEIVE: |
---|
209 | if(!processPacket(event->packet, event->peer)) |
---|
210 | COUT(3) << "processing incoming packet failed" << std::endl; |
---|
211 | break; |
---|
212 | default: |
---|
213 | break; |
---|
214 | } |
---|
215 | delete event; |
---|
216 | //if statement to catch case that packetbuffer is empty |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * takes a new snapshot of the gamestate and sends it to the clients |
---|
222 | */ |
---|
223 | void Server::updateGamestate() { |
---|
224 | // if( ClientInformation::getBegin()==NULL ) |
---|
225 | //no client connected |
---|
226 | // return; |
---|
227 | gamestates_->update(); |
---|
228 | COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl; |
---|
229 | //std::cout << "updated gamestate, sending it" << std::endl; |
---|
230 | //if(clients->getGamestateID()!=GAMESTATEID_INITIAL) |
---|
231 | sendGameState(); |
---|
232 | sendObjectDeletes(); |
---|
233 | COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl; |
---|
234 | //std::cout << "sent gamestate" << std::endl; |
---|
235 | } |
---|
236 | |
---|
237 | bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){ |
---|
238 | packet::Packet *p = packet::Packet::createPacket(packet, peer); |
---|
239 | return p->process(); |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * sends the gamestate |
---|
244 | */ |
---|
245 | bool Server::sendGameState() { |
---|
246 | COUT(5) << "Server: starting function sendGameState" << std::endl; |
---|
247 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
248 | bool added=false; |
---|
249 | while(temp != NULL){ |
---|
250 | if( !(temp->getSynched()) ){ |
---|
251 | COUT(5) << "Server: not sending gamestate" << std::endl; |
---|
252 | temp=temp->next(); |
---|
253 | if(!temp) |
---|
254 | break; |
---|
255 | //think this works without continue |
---|
256 | continue; |
---|
257 | } |
---|
258 | COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl; |
---|
259 | COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; |
---|
260 | int gid = temp->getGamestateID(); //get gamestate id |
---|
261 | int cid = temp->getID(); //get client id |
---|
262 | COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl; |
---|
263 | packet::Gamestate *gs = gamestates_->popGameState(cid); |
---|
264 | if(gs==NULL){ |
---|
265 | COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; |
---|
266 | temp = temp->next(); |
---|
267 | continue; |
---|
268 | } |
---|
269 | //std::cout << "adding gamestate" << std::endl; |
---|
270 | gs->setClientID(cid); |
---|
271 | if ( !gs->send() ){ |
---|
272 | COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; |
---|
273 | temp->addFailure(); |
---|
274 | }else |
---|
275 | temp->resetFailures(); |
---|
276 | added=true; |
---|
277 | temp=temp->next(); |
---|
278 | // gs gets automatically deleted by enet callback |
---|
279 | } |
---|
280 | return true; |
---|
281 | } |
---|
282 | |
---|
283 | bool Server::sendObjectDeletes(){ |
---|
284 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
285 | if( temp == NULL ) |
---|
286 | //no client connected |
---|
287 | return true; |
---|
288 | packet::DeleteObjects *del = new packet::DeleteObjects(); |
---|
289 | if(!del->fetchIDs()) |
---|
290 | return true; //everything ok (no deletes this tick) |
---|
291 | // COUT(3) << "sending DeleteObjects" << std::endl; |
---|
292 | while(temp != NULL){ |
---|
293 | if( !(temp->getSynched()) ){ |
---|
294 | COUT(5) << "Server: not sending gamestate" << std::endl; |
---|
295 | temp=temp->next(); |
---|
296 | continue; |
---|
297 | } |
---|
298 | int cid = temp->getID(); //get client id |
---|
299 | packet::DeleteObjects *cd = new packet::DeleteObjects(*del); |
---|
300 | assert(cd); |
---|
301 | cd->setClientID(cid); |
---|
302 | if ( !cd->send() ) |
---|
303 | COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; |
---|
304 | temp=temp->next(); |
---|
305 | // gs gets automatically deleted by enet callback |
---|
306 | } |
---|
307 | return true; |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | bool Server::addClient(ENetEvent *event){ |
---|
312 | static unsigned int newid=1; |
---|
313 | |
---|
314 | COUT(2) << "Server: adding client" << std::endl; |
---|
315 | ClientInformation *temp = ClientInformation::insertBack(new ClientInformation); |
---|
316 | if(!temp){ |
---|
317 | COUT(2) << "Server: could not add client" << std::endl; |
---|
318 | return false; |
---|
319 | } |
---|
320 | /*if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack |
---|
321 | newid=1; |
---|
322 | } |
---|
323 | else |
---|
324 | newid=temp->prev()->getID()+1;*/ |
---|
325 | temp->setID(newid); |
---|
326 | temp->setPeer(event->peer); |
---|
327 | |
---|
328 | // inform all the listeners |
---|
329 | ObjectList<ClientConnectionListener>::iterator listener = ObjectList<ClientConnectionListener>::begin(); |
---|
330 | while(listener){ |
---|
331 | listener->clientConnected(newid); |
---|
332 | listener++; |
---|
333 | } |
---|
334 | |
---|
335 | newid++; |
---|
336 | |
---|
337 | COUT(3) << "Server: added client id: " << temp->getID() << std::endl; |
---|
338 | return createClient(temp->getID()); |
---|
339 | } |
---|
340 | |
---|
341 | bool Server::createClient(int clientID){ |
---|
342 | ClientInformation *temp = ClientInformation::findClient(clientID); |
---|
343 | if(!temp){ |
---|
344 | COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl; |
---|
345 | return false; |
---|
346 | } |
---|
347 | COUT(5) << "Con.Man: creating client id: " << temp->getID() << std::endl; |
---|
348 | |
---|
349 | // synchronise class ids |
---|
350 | connection->syncClassid(temp->getID()); |
---|
351 | |
---|
352 | // now synchronise functionIDs |
---|
353 | packet::FunctionIDs *fIDs = new packet::FunctionIDs(); |
---|
354 | fIDs->setClientID(clientID); |
---|
355 | bool b = fIDs->send(); |
---|
356 | assert(b); |
---|
357 | |
---|
358 | temp->setSynched(true); |
---|
359 | COUT(4) << "sending welcome" << std::endl; |
---|
360 | packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID()); |
---|
361 | w->setClientID(temp->getID()); |
---|
362 | b = w->send(); |
---|
363 | assert(b); |
---|
364 | packet::Gamestate *g = new packet::Gamestate(); |
---|
365 | g->setClientID(temp->getID()); |
---|
366 | b = g->collectData(0,0x1); |
---|
367 | if(!b) |
---|
368 | return false; //no data for the client |
---|
369 | b = g->compressData(); |
---|
370 | assert(b); |
---|
371 | b = g->send(); |
---|
372 | assert(b); |
---|
373 | return true; |
---|
374 | } |
---|
375 | |
---|
376 | bool Server::disconnectClient(ENetEvent *event){ |
---|
377 | COUT(4) << "removing client from list" << std::endl; |
---|
378 | //return removeClient(head_->findClient(&(peer->address))->getID()); |
---|
379 | |
---|
380 | //boost::recursive_mutex::scoped_lock lock(head_->mutex_); |
---|
381 | ClientInformation *client = ClientInformation::findClient(&event->peer->address); |
---|
382 | if(!client) |
---|
383 | return false; |
---|
384 | gamestates_->removeClient(client); |
---|
385 | |
---|
386 | // inform all the listeners |
---|
387 | ObjectList<ClientConnectionListener>::iterator listener = ObjectList<ClientConnectionListener>::begin(); |
---|
388 | while(listener){ |
---|
389 | listener->clientDisconnected(client->getID()); |
---|
390 | listener++; |
---|
391 | } |
---|
392 | |
---|
393 | return ClientInformation::removeClient(event->peer); |
---|
394 | } |
---|
395 | |
---|
396 | void Server::disconnectClient(int clientID){ |
---|
397 | ClientInformation *client = ClientInformation::findClient(clientID); |
---|
398 | if(client) |
---|
399 | disconnectClient(client); |
---|
400 | } |
---|
401 | void Server::disconnectClient( ClientInformation *client){ |
---|
402 | connection->disconnectClient(client); |
---|
403 | gamestates_->removeClient(client); |
---|
404 | } |
---|
405 | |
---|
406 | bool Server::chat(const std::string& message){ |
---|
407 | return this->sendChat(message, Host::getPlayerID()); |
---|
408 | } |
---|
409 | |
---|
410 | bool Server::broadcast(const std::string& message){ |
---|
411 | return this->sendChat(message, CLIENTID_UNKNOWN); |
---|
412 | } |
---|
413 | |
---|
414 | bool Server::sendChat(const std::string& message, unsigned int clientID){ |
---|
415 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
416 | packet::Chat *chat; |
---|
417 | while(temp){ |
---|
418 | chat = new packet::Chat(message, clientID); |
---|
419 | chat->setClientID(temp->getID()); |
---|
420 | if(!chat->send()) |
---|
421 | COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; |
---|
422 | temp = temp->next(); |
---|
423 | } |
---|
424 | // COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl; |
---|
425 | for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) |
---|
426 | it->incomingChat(message, clientID); |
---|
427 | |
---|
428 | return true; |
---|
429 | } |
---|
430 | |
---|
431 | } |
---|