| 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> |
|---|
| 44 | #include <cassert> |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | #include "ConnectionManager.h" |
|---|
| 48 | #include "GamestateManager.h" |
|---|
| 49 | #include "ClientInformation.h" |
|---|
| 50 | #include "util/Sleep.h" |
|---|
| 51 | #include "objects/SpaceShip.h" |
|---|
| 52 | #include "core/ConsoleCommand.h" |
|---|
| 53 | #include "core/CoreIncludes.h" |
|---|
| 54 | #include "core/Iterator.h" |
|---|
| 55 | #include "packet/Chat.h" |
|---|
| 56 | #include "packet/Packet.h" |
|---|
| 57 | #include "packet/Welcome.h" |
|---|
| 58 | #include "packet/DeleteObjects.h" |
|---|
| 59 | #include <util/Convert.h> |
|---|
| 60 | |
|---|
| 61 | namespace network |
|---|
| 62 | { |
|---|
| 63 | const int MAX_FAILURES = 20; |
|---|
| 64 | const int NETWORK_FREQUENCY = 30; |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Constructor for default values (bindaddress is set to ENET_HOST_ANY |
|---|
| 68 | * |
|---|
| 69 | */ |
|---|
| 70 | Server::Server() { |
|---|
| 71 | timeSinceLastUpdate_=0; |
|---|
| 72 | connection = new ConnectionManager(); |
|---|
| 73 | gamestates_ = new GamestateManager(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | Server::Server(int port){ |
|---|
| 77 | timeSinceLastUpdate_=0; |
|---|
| 78 | connection = new ConnectionManager(port); |
|---|
| 79 | gamestates_ = new GamestateManager(); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Constructor |
|---|
| 84 | * @param port Port to listen on |
|---|
| 85 | * @param bindAddress Address to listen on |
|---|
| 86 | */ |
|---|
| 87 | Server::Server(int port, std::string bindAddress) { |
|---|
| 88 | timeSinceLastUpdate_=0; |
|---|
| 89 | connection = new ConnectionManager(port, bindAddress); |
|---|
| 90 | gamestates_ = new GamestateManager(); |
|---|
| 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; |
|---|
| 100 | connection = new ConnectionManager(port, bindAddress); |
|---|
| 101 | gamestates_ = new GamestateManager(); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * @brief Destructor |
|---|
| 106 | */ |
|---|
| 107 | Server::~Server(){ |
|---|
| 108 | if(connection) |
|---|
| 109 | delete connection; |
|---|
| 110 | if(gamestates_) |
|---|
| 111 | delete gamestates_; |
|---|
| 112 | } |
|---|
| 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 | |
|---|
| 130 | bool Server::processChat(std::string message, unsigned int playerID){ |
|---|
| 131 | ClientInformation *temp = ClientInformation::getBegin(); |
|---|
| 132 | packet::Chat *chat; |
|---|
| 133 | while(temp){ |
|---|
| 134 | chat = new packet::Chat(message, playerID); |
|---|
| 135 | chat->setClientID(temp->getID()); |
|---|
| 136 | if(!chat->send()) |
|---|
| 137 | COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; |
|---|
| 138 | temp = temp->next(); |
|---|
| 139 | } |
|---|
| 140 | COUT(1) << "Player " << playerID << ": " << message << std::endl; |
|---|
| 141 | return true; |
|---|
| 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; |
|---|
| 154 | if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){ |
|---|
| 155 | timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_; |
|---|
| 156 | gamestates_->processGamestates(); |
|---|
| 157 | updateGamestate(); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | bool Server::queuePacket(ENetPacket *packet, int clientID){ |
|---|
| 162 | return connection->addPacket(packet, clientID); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /** |
|---|
| 166 | * processes all the packets waiting in the queue |
|---|
| 167 | */ |
|---|
| 168 | void Server::processQueue() { |
|---|
| 169 | ENetEvent *event; |
|---|
| 170 | while(!connection->queueEmpty()){ |
|---|
| 171 | //std::cout << "Client " << clientID << " sent: " << std::endl; |
|---|
| 172 | //clientID here is a reference to grab clientID from ClientInformation |
|---|
| 173 | event = connection->getEvent(); |
|---|
| 174 | if(!event) |
|---|
| 175 | continue; |
|---|
| 176 | assert(event->type != ENET_EVENT_TYPE_NONE); |
|---|
| 177 | switch( event->type ) { |
|---|
| 178 | case ENET_EVENT_TYPE_CONNECT: |
|---|
| 179 | COUT(3) << "processing event_Type_connect" << std::endl; |
|---|
| 180 | addClient(event); |
|---|
| 181 | break; |
|---|
| 182 | case ENET_EVENT_TYPE_DISCONNECT: |
|---|
| 183 | if(ClientInformation::findClient(&event->peer->address)) |
|---|
| 184 | disconnectClient(event); |
|---|
| 185 | break; |
|---|
| 186 | case ENET_EVENT_TYPE_RECEIVE: |
|---|
| 187 | if(!processPacket(event->packet, event->peer)) |
|---|
| 188 | COUT(3) << "processing incoming packet failed" << std::endl; |
|---|
| 189 | break; |
|---|
| 190 | default: |
|---|
| 191 | break; |
|---|
| 192 | } |
|---|
| 193 | delete event; |
|---|
| 194 | //if statement to catch case that packetbuffer is empty |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * takes a new snapshot of the gamestate and sends it to the clients |
|---|
| 200 | */ |
|---|
| 201 | void Server::updateGamestate() { |
|---|
| 202 | gamestates_->update(); |
|---|
| 203 | COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl; |
|---|
| 204 | //std::cout << "updated gamestate, sending it" << std::endl; |
|---|
| 205 | //if(clients->getGamestateID()!=GAMESTATEID_INITIAL) |
|---|
| 206 | sendGameState(); |
|---|
| 207 | sendObjectDeletes(); |
|---|
| 208 | COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl; |
|---|
| 209 | //std::cout << "sent gamestate" << std::endl; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){ |
|---|
| 213 | packet::Packet *p = packet::Packet::createPacket(packet, peer); |
|---|
| 214 | return p->process(); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | /** |
|---|
| 218 | * sends the gamestate |
|---|
| 219 | */ |
|---|
| 220 | bool Server::sendGameState() { |
|---|
| 221 | COUT(5) << "Server: starting function sendGameState" << std::endl; |
|---|
| 222 | ClientInformation *temp = ClientInformation::getBegin(); |
|---|
| 223 | bool added=false; |
|---|
| 224 | while(temp != NULL){ |
|---|
| 225 | if( !(temp->getSynched()) ){ |
|---|
| 226 | COUT(5) << "Server: not sending gamestate" << std::endl; |
|---|
| 227 | temp=temp->next(); |
|---|
| 228 | if(!temp) |
|---|
| 229 | break; |
|---|
| 230 | //think this works without continue |
|---|
| 231 | continue; |
|---|
| 232 | } |
|---|
| 233 | COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl; |
|---|
| 234 | COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; |
|---|
| 235 | int gid = temp->getGamestateID(); //get gamestate id |
|---|
| 236 | int cid = temp->getID(); //get client id |
|---|
| 237 | COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl; |
|---|
| 238 | packet::Gamestate *gs = gamestates_->popGameState(cid); |
|---|
| 239 | if(gs==NULL){ |
|---|
| 240 | COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; |
|---|
| 241 | continue; |
|---|
| 242 | } |
|---|
| 243 | //std::cout << "adding gamestate" << std::endl; |
|---|
| 244 | gs->setClientID(cid); |
|---|
| 245 | if ( !gs->send() ){ |
|---|
| 246 | COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; |
|---|
| 247 | temp->addFailure(); |
|---|
| 248 | }else |
|---|
| 249 | temp->resetFailures(); |
|---|
| 250 | added=true; |
|---|
| 251 | temp=temp->next(); |
|---|
| 252 | // gs gets automatically deleted by enet callback |
|---|
| 253 | } |
|---|
| 254 | return true; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | bool Server::sendObjectDeletes(){ |
|---|
| 258 | ClientInformation *temp = ClientInformation::getBegin(); |
|---|
| 259 | packet::DeleteObjects *del = new packet::DeleteObjects(); |
|---|
| 260 | if(!del->fetchIDs()) |
|---|
| 261 | return true; //everything ok (no deletes this tick) |
|---|
| 262 | // COUT(3) << "sending DeleteObjects" << std::endl; |
|---|
| 263 | while(temp != NULL){ |
|---|
| 264 | if( !(temp->getSynched()) ){ |
|---|
| 265 | COUT(5) << "Server: not sending gamestate" << std::endl; |
|---|
| 266 | temp=temp->next(); |
|---|
| 267 | continue; |
|---|
| 268 | } |
|---|
| 269 | int cid = temp->getID(); //get client id |
|---|
| 270 | packet::DeleteObjects *cd = new packet::DeleteObjects(*del); |
|---|
| 271 | assert(cd); |
|---|
| 272 | cd->setClientID(cid); |
|---|
| 273 | if ( !cd->send() ) |
|---|
| 274 | COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; |
|---|
| 275 | temp=temp->next(); |
|---|
| 276 | // gs gets automatically deleted by enet callback |
|---|
| 277 | } |
|---|
| 278 | return true; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | bool Server::addClient(ENetEvent *event){ |
|---|
| 283 | ClientInformation *temp = ClientInformation::insertBack(new ClientInformation); |
|---|
| 284 | if(!temp){ |
|---|
| 285 | COUT(2) << "Server: could not add client" << std::endl; |
|---|
| 286 | return false; |
|---|
| 287 | } |
|---|
| 288 | if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack |
|---|
| 289 | temp->setID(1); |
|---|
| 290 | } |
|---|
| 291 | else |
|---|
| 292 | temp->setID(temp->prev()->getID()+1); |
|---|
| 293 | temp->setPeer(event->peer); |
|---|
| 294 | COUT(3) << "Server: added client id: " << temp->getID() << std::endl; |
|---|
| 295 | return createClient(temp->getID()); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | bool Server::createClient(int clientID){ |
|---|
| 299 | ClientInformation *temp = ClientInformation::findClient(clientID); |
|---|
| 300 | if(!temp){ |
|---|
| 301 | COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl; |
|---|
| 302 | return false; |
|---|
| 303 | } |
|---|
| 304 | COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; |
|---|
| 305 | connection->syncClassid(temp->getID()); |
|---|
| 306 | COUT(5) << "creating spaceship for clientid: " << temp->getID() << std::endl; |
|---|
| 307 | // TODO: this is only a hack, untill we have a possibility to define default player-join actions |
|---|
| 308 | if(!createShip(temp)) |
|---|
| 309 | COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl; |
|---|
| 310 | else |
|---|
| 311 | COUT(3) << "created spaceship" << std::endl; |
|---|
| 312 | temp->setSynched(true); |
|---|
| 313 | COUT(3) << "sending welcome" << std::endl; |
|---|
| 314 | packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID()); |
|---|
| 315 | w->setClientID(temp->getID()); |
|---|
| 316 | bool b = w->send(); |
|---|
| 317 | assert(b); |
|---|
| 318 | packet::Gamestate *g = new packet::Gamestate(); |
|---|
| 319 | g->setClientID(temp->getID()); |
|---|
| 320 | b = g->collectData(0); |
|---|
| 321 | assert(b); |
|---|
| 322 | b = g->compressData(); |
|---|
| 323 | assert(b); |
|---|
| 324 | b = g->send(); |
|---|
| 325 | assert(b); |
|---|
| 326 | return true; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | bool Server::createShip(ClientInformation *client){ |
|---|
| 330 | if(!client) |
|---|
| 331 | return false; |
|---|
| 332 | orxonox::Identifier* id = ClassByName("SpaceShip"); |
|---|
| 333 | if(!id){ |
|---|
| 334 | COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl; |
|---|
| 335 | return false; |
|---|
| 336 | } |
|---|
| 337 | orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate()); |
|---|
| 338 | no->classID = id->getNetworkID(); |
|---|
| 339 | client->setShipID(no->getObjectID()); |
|---|
| 340 | no->setPosition(orxonox::Vector3(0,0,80)); |
|---|
| 341 | no->setScale(10); |
|---|
| 342 | //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0)); |
|---|
| 343 | no->setMesh("assff.mesh"); |
|---|
| 344 | no->setMaxSpeed(500); |
|---|
| 345 | no->setMaxSideAndBackSpeed(50); |
|---|
| 346 | no->setMaxRotation(1.0); |
|---|
| 347 | no->setTransAcc(200); |
|---|
| 348 | no->setRotAcc(3.0); |
|---|
| 349 | no->setTransDamp(75); |
|---|
| 350 | no->setRotDamp(1.0); |
|---|
| 351 | no->setCamera(std::string("cam_") + convertToString(client->getID())); |
|---|
| 352 | no->create(); |
|---|
| 353 | |
|---|
| 354 | return true; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | bool Server::disconnectClient(ENetEvent *event){ |
|---|
| 358 | COUT(4) << "removing client from list" << std::endl; |
|---|
| 359 | //return removeClient(head_->findClient(&(peer->address))->getID()); |
|---|
| 360 | |
|---|
| 361 | //boost::recursive_mutex::scoped_lock lock(head_->mutex_); |
|---|
| 362 | orxonox::ObjectList<orxonox::SpaceShip>::iterator it = orxonox::ObjectList<orxonox::SpaceShip>::begin(); |
|---|
| 363 | ClientInformation *client = ClientInformation::findClient(&event->peer->address); |
|---|
| 364 | if(!client) |
|---|
| 365 | return false; |
|---|
| 366 | gamestates_->removeClient(client); |
|---|
| 367 | while(it){ |
|---|
| 368 | if(it->getObjectID()!=client->getShipID()){ |
|---|
| 369 | ++it; |
|---|
| 370 | continue; |
|---|
| 371 | } |
|---|
| 372 | orxonox::ObjectList<orxonox::SpaceShip>::iterator temp=it; |
|---|
| 373 | ++it; |
|---|
| 374 | delete *temp; |
|---|
| 375 | return ClientInformation::removeClient(event->peer); |
|---|
| 376 | } |
|---|
| 377 | return false; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | void Server::disconnectClient(int clientID){ |
|---|
| 381 | ClientInformation *client = ClientInformation::findClient(clientID); |
|---|
| 382 | if(client) |
|---|
| 383 | disconnectClient(client); |
|---|
| 384 | } |
|---|
| 385 | void Server::disconnectClient( ClientInformation *client){ |
|---|
| 386 | connection->disconnectClient(client); |
|---|
| 387 | gamestates_->removeClient(client); |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | bool Server::chat(std::string message){ |
|---|
| 391 | ClientInformation *temp = ClientInformation::getBegin(); |
|---|
| 392 | packet::Chat *chat; |
|---|
| 393 | while(temp){ |
|---|
| 394 | chat = new packet::Chat(message, Host::getPlayerID()); |
|---|
| 395 | chat->setClientID(temp->getID()); |
|---|
| 396 | if(!chat->send()) |
|---|
| 397 | COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; |
|---|
| 398 | temp = temp->next(); |
|---|
| 399 | } |
|---|
| 400 | COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl; |
|---|
| 401 | return true; |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | } |
|---|