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