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