Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup/src/libraries/network/Server.cc @ 5935

Last change on this file since 5935 was 5935, checked in by dafrick, 15 years ago

Hopefully merged trunk successfully into pickup branch.

  • 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
[5749]43#define WIN32_LEAN_AND_MEAN
[2773]44#include <enet/enet.h>
[1755]45#include <cassert>
[3214]46#include <string>
[1502]47
[5935]48#include "util/Clock.h"
[3214]49#include "util/Debug.h"
50#include "core/ObjectList.h"
[3304]51#include "core/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  */
71  Server::Server() {
[3304]72    this->timeSinceLastUpdate_=0;
[1502]73  }
[1747]74
[1502]75  Server::Server(int port){
[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  */
[2087]85  Server::Server(int port, const std::string& bindAddress) {
[3214]86    this->setPort( port );
87    this->setBindAddress( bindAddress );
[3304]88    this->timeSinceLastUpdate_=0;
[1502]89  }
90
91  /**
[1907]92  * @brief Destructor
93  */
94  Server::~Server(){
95  }
[1502]96
97  /**
98  * This function opens the server by creating the listener thread
99  */
100  void Server::open() {
[3214]101    COUT(4) << "opening server" << endl;
102    this->openListener();
[1502]103    return;
104  }
105
106  /**
107  * This function closes the server
108  */
109  void Server::close() {
[3214]110    COUT(4) << "closing server" << endl;
111    this->disconnectClients();
112    this->closeListener();
[1502]113    return;
114  }
115
[2087]116  bool Server::processChat(const std::string& message, unsigned int playerID){
[1735]117    ClientInformation *temp = ClientInformation::getBegin();
[1907]118    packet::Chat *chat;
[1735]119    while(temp){
[1907]120      chat = new packet::Chat(message, playerID);
[1735]121      chat->setClientID(temp->getID());
122      if(!chat->send())
123        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
[1907]124      temp = temp->next();
[1735]125    }
[2087]126//    COUT(1) << "Player " << playerID << ": " << message << std::endl;
[1907]127    return true;
[1502]128  }
129
130
131  /**
132  * Run this function once every tick
133  * calls processQueue and updateGamestate
134  * @param time time since last tick
135  */
[2896]136  void Server::update(const Clock& time) {
[3304]137    // receive incoming packets
[3214]138    Connection::processQueue();
[3304]139   
140    if ( ClientInformation::hasClients() )
[3214]141    {
[3304]142      // process incoming gamestates
143      GamestateManager::processGamestates();
144     
145      // send function calls to clients
[3084]146      FunctionCallManager::sendCalls();
[3304]147     
148      //this steers our network frequency
149      timeSinceLastUpdate_+=time.getDeltaTime();
150      if(timeSinceLastUpdate_>=NETWORK_PERIOD)
151      {
152        timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
153        updateGamestate();
154      }
155      sendPackets(); // flush the enet queue
[1502]156    }
157  }
[1747]158
[1735]159  bool Server::queuePacket(ENetPacket *packet, int clientID){
[3214]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() {
[3304]183    if( ClientInformation::getBegin()==NULL )
[2662]184      //no client connected
[3304]185      return;
186    GamestateManager::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() {
[3304]205//     COUT(5) << "Server: starting function sendGameState" << std::endl;
206//     ClientInformation *temp = ClientInformation::getBegin();
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();
212//         if(!temp)
213//           break;
214//         continue;
215//       }
216//       COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
217//       COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
218//       int cid = temp->getID(); //get client id
219//       packet::Gamestate *gs = GamestateManager::popGameState(cid);
220//       if(gs==NULL){
221//         COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
222//         temp = temp->next();
223//         continue;
224//       }
225//       //std::cout << "adding gamestate" << std::endl;
226//       gs->setClientID(cid);
227//       if ( !gs->send() ){
228//         COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
229//         temp->addFailure();
230//       }else
231//         temp->resetFailures();
232//       added=true;
233//       temp=temp->next();
234//       // gs gets automatically deleted by enet callback
235//     }
236    GamestateManager::sendGamestates();
[1502]237    return true;
238  }
[1747]239
[1907]240  bool Server::sendObjectDeletes(){
241    ClientInformation *temp = ClientInformation::getBegin();
[2662]242    if( temp == NULL )
243      //no client connected
244      return true;
[1907]245    packet::DeleteObjects *del = new packet::DeleteObjects();
246    if(!del->fetchIDs())
[5935]247    {
248      delete del;
[1907]249      return true;  //everything ok (no deletes this tick)
[5935]250    }
[1907]251//     COUT(3) << "sending DeleteObjects" << std::endl;
252    while(temp != NULL){
253      if( !(temp->getSynched()) ){
254        COUT(5) << "Server: not sending gamestate" << std::endl;
255        temp=temp->next();
256        continue;
257      }
258      int cid = temp->getID(); //get client id
259      packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
260      assert(cd);
261      cd->setClientID(cid);
262      if ( !cd->send() )
263        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
264      temp=temp->next();
265      // gs gets automatically deleted by enet callback
266    }
[3198]267    delete del;
[1907]268    return true;
269  }
[1747]270
[1907]271
[5935]272  void Server::addPeer(ENetEvent *event){
[2087]273    static unsigned int newid=1;
274
275    COUT(2) << "Server: adding client" << std::endl;
[1735]276    ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
[1502]277    if(!temp){
278      COUT(2) << "Server: could not add client" << std::endl;
279    }
[2087]280    temp->setID(newid);
[1502]281    temp->setPeer(event->peer);
[2087]282
283    // inform all the listeners
[5935]284    ClientConnectionListener::broadcastClientConnected(newid);
[2087]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
[5935]292  void Server::removePeer(ENetEvent *event)
293  {
294    COUT(4) << "removing client from list" << std::endl;
295    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
296    if(!client)
297      return;
298    else
299    {
300      //ServerConnection::disconnectClient( client );
301      ClientConnectionListener::broadcastClientDisconnected( client->getID() );
302      delete client;
303    }
304  }
305
[1502]306  bool Server::createClient(int clientID){
[1735]307    ClientInformation *temp = ClientInformation::findClient(clientID);
[1502]308    if(!temp){
309      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
310      return false;
311    }
[3084]312    COUT(5) << "Con.Man: creating client id: " << temp->getID() << std::endl;
313   
314    // synchronise class ids
[3214]315    syncClassid(temp->getID());
[3084]316   
317    // now synchronise functionIDs
318    packet::FunctionIDs *fIDs = new packet::FunctionIDs();
319    fIDs->setClientID(clientID);
320    bool b = fIDs->send();
321    assert(b);
322   
[1502]323    temp->setSynched(true);
[3084]324    COUT(4) << "sending welcome" << std::endl;
[1735]325    packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID());
326    w->setClientID(temp->getID());
[3084]327    b = w->send();
[1907]328    assert(b);
[1751]329    packet::Gamestate *g = new packet::Gamestate();
330    g->setClientID(temp->getID());
[3102]331    b = g->collectData(0,0x1);
[2087]332    if(!b)
333      return false; //no data for the client
[1907]334    b = g->compressData();
335    assert(b);
336    b = g->send();
337    assert(b);
[1502]338    return true;
339  }
[3198]340 
[3214]341  void Server::disconnectClient( ClientInformation *client ){
342    ServerConnection::disconnectClient( client );
[3304]343    GamestateManager::removeClient(client);
[5935]344    // inform all the listeners
345    ClientConnectionListener::broadcastClientDisconnected(client->getID());
[1502]346  }
[2087]347
348  bool Server::chat(const std::string& message){
349      return this->sendChat(message, Host::getPlayerID());
350  }
351
352  bool Server::broadcast(const std::string& message){
353      return this->sendChat(message, CLIENTID_UNKNOWN);
354  }
355
356  bool Server::sendChat(const std::string& message, unsigned int clientID){
[1907]357    ClientInformation *temp = ClientInformation::getBegin();
358    packet::Chat *chat;
359    while(temp){
[2087]360      chat = new packet::Chat(message, clientID);
[1907]361      chat->setClientID(temp->getID());
362      if(!chat->send())
363        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
364      temp = temp->next();
365    }
[2087]366//    COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl;
[2171]367    for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it)
[2087]368      it->incomingChat(message, clientID);
369
[1907]370    return true;
371  }
[1747]372
[3214]373  void Server::syncClassid(unsigned int clientID) {
374    int failures=0;
375    packet::ClassID *classid = new packet::ClassID();
376    classid->setClientID(clientID);
377    while(!classid->send() && failures < 10){
378      failures++;
379    }
380    assert(failures<10);
381    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
382  }
383
[1502]384}
Note: See TracBrowser for help on using the repository browser.