Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/Server.cc @ 10524

Last change on this file since 10524 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

  • 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:
[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"
[8858]49#include "util/Output.h"
[7284]50#include "core/command/Executor.h"
[1735]51#include "packet/Chat.h"
[3214]52#include "packet/ClassID.h"
53#include "packet/DeleteObjects.h"
54#include "packet/FunctionIDs.h"
55#include "packet/Gamestate.h"
[1735]56#include "packet/Welcome.h"
[8327]57// #include "ClientInformation.h"
[3084]58#include "FunctionCallManager.h"
[3214]59#include "GamestateManager.h"
[1502]60
[2171]61namespace orxonox
[1502]62{
[2087]63  const unsigned int MAX_FAILURES = 20;
[1747]64
[1502]65  /**
66  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
67  *
68  */
[7163]69  Server::Server()
70  {
[3304]71    this->timeSinceLastUpdate_=0;
[1502]72  }
[1747]73
[7163]74  Server::Server(int port)
75  {
[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  */
[7163]85  Server::Server(int port, const std::string& bindAddress)
86  {
[3214]87    this->setPort( port );
88    this->setBindAddress( bindAddress );
[3304]89    this->timeSinceLastUpdate_=0;
[1502]90  }
91
92  /**
[1907]93  * @brief Destructor
94  */
[7163]95  Server::~Server()
96  {
[1907]97  }
[1502]98
99  /**
100  * This function opens the server by creating the listener thread
101  */
[7163]102  void Server::open()
103  {
104    Host::setActive(true);
[8858]105    orxout(verbose, context::network) << "opening server" << endl;
[3214]106    this->openListener();
[8858]107
[7801]108    /* make discoverable on LAN */
[7163]109    LANDiscoverable::setActivity(true);
[7801]110
111    /* make discoverable on WAN */
112    WANDiscoverable::setActivity(true);
113
114    /* done */
[1502]115    return;
116  }
117
118  /**
119  * This function closes the server
120  */
[7163]121  void Server::close()
122  {
123    Host::setActive(false);
[8858]124    orxout(verbose, context::network) << "closing server" << endl;
[3214]125    this->disconnectClients();
126    this->closeListener();
[7801]127
128    /* tell master server we're closing */
[8858]129    orxout(internal_info, context::network) << "disconnecting." << endl;
130    WANDiscoverable::setActivity(false);
131    orxout(internal_info, context::network) << "disconnecting done" << endl;
[7801]132
[7163]133    LANDiscoverable::setActivity(false);
[1502]134    return;
135  }
136
137  /**
138  * Run this function once every tick
139  * calls processQueue and updateGamestate
140  * @param time time since last tick
141  */
[7163]142  void Server::update(const Clock& time)
143  {
[3304]144    // receive incoming packets
[3214]145    Connection::processQueue();
[7801]146
[7163]147    // receive and process incoming discovery packets
148    LANDiscoverable::update();
[6417]149
[8327]150    if ( GamestateManager::hasPeers() )
[3214]151    {
[3304]152      // process incoming gamestates
153      GamestateManager::processGamestates();
[7801]154      FunctionCallManager::processBufferedFunctionCalls();
[6417]155
[3304]156      // send function calls to clients
[7801]157      FunctionCallManager::sendCalls( static_cast<Host*>(this) );
[6417]158
[3304]159      //this steers our network frequency
160      timeSinceLastUpdate_+=time.getDeltaTime();
161      if(timeSinceLastUpdate_>=NETWORK_PERIOD)
162      {
163        timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
164        updateGamestate();
165      }
[7801]166//       sendPackets(); // flush the enet queue
[1502]167    }
168  }
[1747]169
[7801]170  void Server::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)
[7163]171  {
[7801]172    ServerConnection::addPacket(packet, clientID, channelID);
[1735]173  }
[6417]174
[2087]175  /**
[6417]176   * @brief: returns ping time to client in milliseconds
[2087]177   */
[7163]178  unsigned int Server::getRTT(unsigned int clientID)
179  {
[8327]180//     assert(ClientInformation::findClient(clientID));
181//     return ClientInformation::findClient(clientID)->getRTT();
182    // TODO: reimplement
183    return 0;
[2087]184  }
[6417]185
[5961]186  void Server::printRTT()
187  {
[8327]188//     for( ClientInformation* temp=ClientInformation::getBegin(); temp!=0; temp=temp->next() )
[8858]189//       orxout(message) << "Round trip time to client with ID: " << temp->getID() << " is " << temp->getRTT() << " ms" << endl;
[5961]190  }
[1502]191
192  /**
[2087]193   * @brief: return packet loss ratio to client (scales from 0 to 1)
194   */
[8351]195  float Server::getPacketLoss(unsigned int clientID)
[7163]196  {
[8327]197//     assert(ClientInformation::findClient(clientID));
198//     return ClientInformation::findClient(clientID)->getPacketLoss();
199    return 0.;
[2087]200  }
[1502]201
202  /**
203  * takes a new snapshot of the gamestate and sends it to the clients
204  */
[7163]205  void Server::updateGamestate()
206  {
[8327]207    if( this->clientIDs_.size()==0 )
[2662]208      //no client connected
[3304]209      return;
210    GamestateManager::update();
[8858]211//     orxout(verbose_more, context::network) << "Server: one gamestate update complete, goig to sendGameState" << endl;
212    //orxout(verbose_more, context::network) << "updated gamestate, sending it" << endl;
[1502]213    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
[7801]214    sendGameStates();
[1907]215    sendObjectDeletes();
[8858]216//     orxout(verbose_more, context::network) << "Server: one sendGameState turn complete, repeat in next tick" << endl;
217    //orxout(verbose_more, context::network) << "sent gamestate" << endl;
[1502]218  }
219
220  /**
[7801]221  * sends the current gamestate to all peers
[1502]222  */
[7801]223  bool Server::sendGameStates()
[7163]224  {
[7801]225    std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates();
226    std::vector<packet::Gamestate*>::iterator it;
227    for( it = gamestates.begin(); it != gamestates.end(); ++it )
228    {
229      (*it)->send(static_cast<Host*>(this));
230    }
[1502]231    return true;
232  }
[1747]233
[7801]234
[7163]235  bool Server::sendObjectDeletes()
236  {
[8327]237//     ClientInformation *temp = ClientInformation::getBegin();
238//     if( temp == NULL )
[2662]239      //no client connected
[8327]240    if( this->clientIDs_.size()==0 )
[2662]241      return true;
[1907]242    packet::DeleteObjects *del = new packet::DeleteObjects();
243    if(!del->fetchIDs())
[5929]244    {
245      delete del;
[1907]246      return true;  //everything ok (no deletes this tick)
[5929]247    }
[8858]248//     orxout(verbose, context::network) << "sending DeleteObjects" << endl;
[8327]249//     while(temp != NULL){
250//       if( !(temp->getSynched()) )
251//       {
[8858]252//         orxout(verbose_more, context::network) << "Server: not sending gamestate" << endl;
[8327]253//         temp=temp->next();
254//         continue;
255//       }
256//       int cid = temp->getID(); //get client id
257//       packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
258//       assert(cd);
259    del->setPeerID(NETWORK_PEER_ID_BROADCAST);
260    if ( !del->send( static_cast<Host*>(this) ) )
[8858]261      orxout(internal_warning, context::network) << "Server: could not broadcast deleteObjects packet" << endl;
[8327]262//       temp=temp->next();
[1907]263      // gs gets automatically deleted by enet callback
[8327]264//     }
265//     delete del;
[1907]266    return true;
267  }
[1747]268
[1907]269
[8327]270  void Server::addPeer(uint32_t peerID)
[7163]271  {
[8327]272//     static unsigned int newid=1;
[8858]273//
274//     orxout(internal_info, context::network) << "Server: adding client" << endl;
[8327]275//     ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
276//     if(!temp)
277//     {
[8858]278//       orxout(internal_warning, context::network) << "Server: could not add client" << endl;
[8327]279//     }
280//     temp->setID(newid);
281//     temp->setPeer(event->peer);
[2087]282
283    // inform all the listeners
[8327]284    this->clientIDs_.push_back(peerID);
285    ClientConnectionListener::broadcastClientConnected(peerID);
286    GamestateManager::addPeer(peerID);
[2087]287
[8327]288//     ++newid;
[2087]289
[8858]290    orxout(internal_info, context::network) << "Server: added client id: " << peerID << endl;
[8327]291    createClient(peerID);
[2087]292}
[1747]293
[8327]294  void Server::removePeer(uint32_t peerID)
[5929]295  {
[8858]296    orxout(verbose, context::network) << "removing client from list" << endl;
[8327]297//     ClientInformation *client = ClientInformation::findClient(&event->peer->address);
298//     if(!client)
299//       return;
300//     else
301//     {
302  std::vector<uint32_t>::iterator it;
303  for( it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it )
304  {
305    if( *it == peerID )
[5929]306    {
[8327]307      this->clientIDs_.erase(it);
308      break;
309    }
310  }
311  ClientConnectionListener::broadcastClientDisconnected(peerID);
312  GamestateManager::removePeer(peerID);
[5929]313      //ServerConnection::disconnectClient( client );
[5961]314      //ClientConnectionListener::broadcastClientDisconnected( client->getID() ); //this is done in ClientInformation now
[8327]315//       delete client;
316//     }
[5929]317  }
[8858]318
[7801]319  void Server::processPacket(packet::Packet* packet)
320  {
321    if( packet->isReliable() )
322    {
[8327]323      if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
[7801]324        packet->process(static_cast<Host*>(this));
325      else
326        this->packetQueue_.push_back(packet);
327    }
328    else
329      packet->process(static_cast<Host*>(this));
330  }
[5929]331
[7801]332
[7163]333  bool Server::createClient(int clientID)
334  {
[8327]335//     ClientInformation *temp = ClientInformation::findClient(clientID);
336//     if(!temp)
337//     {
[8858]338//       orxout(internal_error, context::network) << "Server. could not create client with id: " << clientID << endl;
[8327]339//       return false;
340//     }
[8858]341//     orxout(verbose, context::network) << "Con.Man: creating client id: " << temp->getID() << endl;
[6417]342
[3084]343    // synchronise class ids
[8327]344    syncClassid(clientID);
[6417]345
[3084]346    // now synchronise functionIDs
347    packet::FunctionIDs *fIDs = new packet::FunctionIDs();
[7801]348    fIDs->setPeerID(clientID);
349    bool b = fIDs->send( static_cast<Host*>(this) );
[3084]350    assert(b);
[6417]351
[8327]352//     temp->setSynched(true);
[7801]353    GamestateManager::setSynched(clientID);
[8858]354
355    orxout(verbose, context::network) << "sending welcome" << endl;
[8706]356    packet::Welcome *w = new packet::Welcome(clientID);
[8327]357    w->setPeerID(clientID);
[7801]358    b = w->send( static_cast<Host*>(this) );
[1907]359    assert(b);
[8327]360//     packet::Gamestate *g = new packet::Gamestate();
361//     g->setPeerID(clientID);
362//     b = g->collectData(0,packet::GAMESTATE_MODE_SERVER);
[7801]363//     assert(b);
[8327]364//     if(!b)
365//       return false; //no data for the client
366// //     b = g->compressData();
367// //     assert(b);
368//     b = g->send( static_cast<Host*>(this) );
369//     assert(b);
[1502]370    return true;
371  }
[6417]372
[8327]373  void Server::disconnectClient( uint32_t clientID )
[7163]374  {
[8327]375    ServerConnection::disconnectClient( clientID );
376    GamestateManager::removePeer( clientID );
[5929]377    // inform all the listeners
[5961]378    // ClientConnectionListener::broadcastClientDisconnected(client->getID()); // this is done in ClientInformation now
[1502]379  }
[2087]380
[8858]381  /**
382   * @brief Sends a chat message to the given target ID.
383   * @param message message to be sent
384   * @param sourceID the ID of the sender
385   * @param targetID the ID of the receiver
386   */
387  void Server::doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID)
[7163]388  {
[8858]389    // check if the target exists. just ignore the message otherwise
390    if (!this->isValidTarget(targetID)) // TODO: remove this if an invalid clientIDs don't trigger assertions anymore
391      return;
392
393    // send the message to the target
394    packet::Chat* packet = new packet::Chat(message, sourceID, targetID);
395    packet->setPeerID(targetID);
396    packet->send( static_cast<Host*>(this) );
397
398    // if the target is (or includes) this host as well, call the parent function which passes the message to the listeners
399    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == Host::getPlayerID())
400      Host::doReceiveChat(message, sourceID, targetID);
[2087]401  }
402
[8858]403  /**
404   * @brief Gets called if a packet::Chat packet is received. Forwards the packet to the target
405   * and calls the parent function if necessary.
406   */
407  void Server::doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID)
[7163]408  {
[8858]409      this->doSendChat(message, sourceID, targetID);
[2087]410  }
411
[8858]412  /**
413   * @brief Returns true if the target ID is in the list of clients (or if it
414   * corresponds to the broadcast or the server ID).
415   */
416  bool Server::isValidTarget(unsigned int targetID)
[7163]417  {
[8858]418    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == NETWORK_PEER_ID_SERVER)
419      return true;
[2087]420
[8858]421    std::vector<uint32_t>::iterator it;
422    for( it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it )
423      if( *it == targetID )
424        return true;
425
426    return false;
[1907]427  }
[1747]428
[7163]429  void Server::syncClassid(unsigned int clientID)
430  {
[3214]431    int failures=0;
432    packet::ClassID *classid = new packet::ClassID();
[7801]433    classid->setPeerID(clientID);
434    while(!classid->send( static_cast<Host*>(this) ) && failures < 10){
[3214]435      failures++;
436    }
437    assert(failures<10);
[8858]438    orxout(verbose, context::network) << "syncClassid:\tall synchClassID packets have been sent" << endl;
[3214]439  }
440
[1502]441}
Note: See TracBrowser for help on using the repository browser.