Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/Server.cc @ 5687

Last change on this file since 5687 was 3304, checked in by rgrieder, 15 years ago

Merged netp6 branch back to the trunk.

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