Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/network/Server.cc @ 1916

Last change on this file since 1916 was 1916, checked in by landauf, 16 years ago

removed WorldEntity, SpaceShip and several other objects
removed SpaceShip-related hacks in network and other places

  • Property svn:eol-style set to native
File size: 10.7 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, (C) 2007
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#include <iostream>
44#include <cassert>
45
46
47#include "ConnectionManager.h"
48#include "GamestateManager.h"
49#include "ClientInformation.h"
50#include "util/Sleep.h"
51#include "core/ConsoleCommand.h"
52#include "core/CoreIncludes.h"
53#include "core/Iterator.h"
54#include "packet/Chat.h"
55#include "packet/Packet.h"
56#include "packet/Welcome.h"
57#include "packet/DeleteObjects.h"
58#include <util/Convert.h>
59
60namespace network
61{
62  const int MAX_FAILURES = 20;
63  const int NETWORK_FREQUENCY = 30;
64
65  /**
66  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
67  *
68  */
69  Server::Server() {
70    timeSinceLastUpdate_=0;
71    connection = new ConnectionManager();
72    gamestates_ = new GamestateManager();
73  }
74
75  Server::Server(int port){
76    timeSinceLastUpdate_=0;
77    connection = new ConnectionManager(port);
78    gamestates_ = new GamestateManager();
79  }
80
81  /**
82  * Constructor
83  * @param port Port to listen on
84  * @param bindAddress Address to listen on
85  */
86  Server::Server(int port, std::string bindAddress) {
87    timeSinceLastUpdate_=0;
88    connection = new ConnectionManager(port, bindAddress);
89    gamestates_ = new GamestateManager();
90  }
91
92  /**
93  * Constructor
94  * @param port Port to listen on
95  * @param bindAddress Address to listen on
96  */
97  Server::Server(int port, const char *bindAddress) {
98    timeSinceLastUpdate_=0;
99    connection = new ConnectionManager(port, bindAddress);
100    gamestates_ = new GamestateManager();
101  }
102
103  /**
104  * @brief Destructor
105  */
106  Server::~Server(){
107    if(connection)
108      delete connection;
109    if(gamestates_)
110      delete gamestates_;
111  }
112
113  /**
114  * This function opens the server by creating the listener thread
115  */
116  void Server::open() {
117    connection->createListener();
118    return;
119  }
120
121  /**
122  * This function closes the server
123  */
124  void Server::close() {
125    connection->quitListener();
126    return;
127  }
128
129  bool Server::processChat(std::string message, unsigned int playerID){
130    ClientInformation *temp = ClientInformation::getBegin();
131    packet::Chat *chat;
132    while(temp){
133      chat = new packet::Chat(message, playerID);
134      chat->setClientID(temp->getID());
135      if(!chat->send())
136        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
137      temp = temp->next();
138    }
139    COUT(1) << "Player " << playerID << ": " << message << std::endl;
140    return true;
141  }
142
143
144  /**
145  * Run this function once every tick
146  * calls processQueue and updateGamestate
147  * @param time time since last tick
148  */
149  void Server::tick(float time) {
150    processQueue();
151    //this steers our network frequency
152    timeSinceLastUpdate_+=time;
153    if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){
154      timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_;
155      gamestates_->processGamestates();
156      updateGamestate();
157    }
158  }
159
160  bool Server::queuePacket(ENetPacket *packet, int clientID){
161    return connection->addPacket(packet, clientID);
162  }
163
164  /**
165  * processes all the packets waiting in the queue
166  */
167  void Server::processQueue() {
168    ENetEvent *event;
169    while(!connection->queueEmpty()){
170      //std::cout << "Client " << clientID << " sent: " << std::endl;
171      //clientID here is a reference to grab clientID from ClientInformation
172      event = connection->getEvent();
173      if(!event)
174        continue;
175      assert(event->type != ENET_EVENT_TYPE_NONE);
176      switch( event->type ) {
177      case ENET_EVENT_TYPE_CONNECT:
178        COUT(3) << "processing event_Type_connect" << std::endl;
179        addClient(event);
180        break;
181      case ENET_EVENT_TYPE_DISCONNECT:
182        if(ClientInformation::findClient(&event->peer->address))
183          disconnectClient(event);
184        break;
185      case ENET_EVENT_TYPE_RECEIVE:
186        if(!processPacket(event->packet, event->peer))
187          COUT(3) << "processing incoming packet failed" << std::endl;
188        break;
189      default:
190        break;
191      }
192      delete event;
193      //if statement to catch case that packetbuffer is empty
194    }
195  }
196
197  /**
198  * takes a new snapshot of the gamestate and sends it to the clients
199  */
200  void Server::updateGamestate() {
201    gamestates_->update();
202    COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
203    //std::cout << "updated gamestate, sending it" << std::endl;
204    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
205    sendGameState();
206    sendObjectDeletes();
207    COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
208    //std::cout << "sent gamestate" << std::endl;
209  }
210
211  bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){
212    packet::Packet *p = packet::Packet::createPacket(packet, peer);
213    return p->process();
214  }
215
216  /**
217  * sends the gamestate
218  */
219  bool Server::sendGameState() {
220    COUT(5) << "Server: starting function sendGameState" << std::endl;
221    ClientInformation *temp = ClientInformation::getBegin();
222    bool added=false;
223    while(temp != NULL){
224      if( !(temp->getSynched()) ){
225        COUT(5) << "Server: not sending gamestate" << std::endl;
226        temp=temp->next();
227        if(!temp)
228          break;
229        //think this works without continue
230        continue;
231      }
232      COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
233      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
234      int gid = temp->getGamestateID(); //get gamestate id
235      int cid = temp->getID(); //get client id
236      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
237      packet::Gamestate *gs = gamestates_->popGameState(cid);
238      if(gs==NULL){
239        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
240        continue;
241      }
242      //std::cout << "adding gamestate" << std::endl;
243      gs->setClientID(cid);
244      if ( !gs->send() ){
245        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
246        temp->addFailure();
247      }else
248        temp->resetFailures();
249      added=true;
250      temp=temp->next();
251      // gs gets automatically deleted by enet callback
252    }
253    return true;
254  }
255
256  bool Server::sendObjectDeletes(){
257    ClientInformation *temp = ClientInformation::getBegin();
258    packet::DeleteObjects *del = new packet::DeleteObjects();
259    if(!del->fetchIDs())
260      return true;  //everything ok (no deletes this tick)
261//     COUT(3) << "sending DeleteObjects" << std::endl;
262    while(temp != NULL){
263      if( !(temp->getSynched()) ){
264        COUT(5) << "Server: not sending gamestate" << std::endl;
265        temp=temp->next();
266        continue;
267      }
268      int cid = temp->getID(); //get client id
269      packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
270      assert(cd);
271      cd->setClientID(cid);
272      if ( !cd->send() )
273        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
274      temp=temp->next();
275      // gs gets automatically deleted by enet callback
276    }
277    return true;
278  }
279
280
281  bool Server::addClient(ENetEvent *event){
282    ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
283    if(!temp){
284      COUT(2) << "Server: could not add client" << std::endl;
285      return false;
286    }
287    if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack
288      temp->setID(1);
289    }
290    else
291      temp->setID(temp->prev()->getID()+1);
292    temp->setPeer(event->peer);
293    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
294    return createClient(temp->getID());
295  }
296
297  bool Server::createClient(int clientID){
298    ClientInformation *temp = ClientInformation::findClient(clientID);
299    if(!temp){
300      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
301      return false;
302    }
303    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
304    connection->syncClassid(temp->getID());
305    temp->setSynched(true);
306    COUT(3) << "sending welcome" << std::endl;
307    packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID());
308    w->setClientID(temp->getID());
309    bool b = w->send();
310    assert(b);
311    packet::Gamestate *g = new packet::Gamestate();
312    g->setClientID(temp->getID());
313    b = g->collectData(0);
314    assert(b);
315    b = g->compressData();
316    assert(b);
317    b = g->send();
318    assert(b);
319    return true;
320  }
321
322  bool Server::disconnectClient(ENetEvent *event){
323    COUT(4) << "removing client from list" << std::endl;
324    //return removeClient(head_->findClient(&(peer->address))->getID());
325
326    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
327    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
328    if(!client)
329      return false;
330    gamestates_->removeClient(client);
331    return ClientInformation::removeClient(event->peer);
332  }
333
334  void Server::disconnectClient(int clientID){
335    ClientInformation *client = ClientInformation::findClient(clientID);
336    if(client)
337      disconnectClient(client);
338  }
339  void Server::disconnectClient( ClientInformation *client){
340    connection->disconnectClient(client);
341    gamestates_->removeClient(client);
342  }
343
344  bool Server::chat(std::string message){
345    ClientInformation *temp = ClientInformation::getBegin();
346    packet::Chat *chat;
347    while(temp){
348      chat = new packet::Chat(message, Host::getPlayerID());
349      chat->setClientID(temp->getID());
350      if(!chat->send())
351        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
352      temp = temp->next();
353    }
354    COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl;
355    return true;
356  }
357
358}
Note: See TracBrowser for help on using the repository browser.