Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound5/src/libraries/network/Server.cc @ 6518

Last change on this file since 6518 was 6417, checked in by rgrieder, 15 years ago

Merged presentation2 branch back to trunk.
Major new features:

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