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