Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp2/src/network/Server.cc @ 2965

Last change on this file since 2965 was 2953, checked in by scheusso, 16 years ago

some changes for testing purpose

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