Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7295 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

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