Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/Server.cc @ 3202

Last change on this file since 3202 was 3202, checked in by scheusso, 15 years ago

rest of the cleanup ( mostly client connection handling)
network is now single-threaded ( only in order to become multithreaded again, but thats another story ;) )

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