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