Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/network/Server.cc @ 3731

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

changed some debug output
removed some uneccessary "multi"threading
some enhancements in Connection

  • Property svn:eol-style set to native
File size: 11.1 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#include <enet/enet.h>
44#include <cassert>
45#include <string>
46
47#include "util/Debug.h"
48#include "core/Clock.h"
49#include "core/ObjectList.h"
50#include "core/Executor.h"
51#include "packet/Chat.h"
52#include "packet/ClassID.h"
53#include "packet/DeleteObjects.h"
54#include "packet/FunctionIDs.h"
55#include "packet/Gamestate.h"
56#include "packet/Welcome.h"
57#include "ChatListener.h"
58#include "ClientInformation.h"
59#include "FunctionCallManager.h"
60#include "GamestateManager.h"
61
62namespace orxonox
63{
64  const unsigned int MAX_FAILURES = 20;
65
66  /**
67  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
68  *
69  */
70  Server::Server() {
71    this->timeSinceLastUpdate_=0;
72  }
73
74  Server::Server(int port){
75    this->setPort( port );
76    this->timeSinceLastUpdate_=0;
77  }
78
79  /**
80  * Constructor
81  * @param port Port to listen on
82  * @param bindAddress Address to listen on
83  */
84  Server::Server(int port, const std::string& bindAddress) {
85    this->setPort( port );
86    this->setBindAddress( bindAddress );
87    this->timeSinceLastUpdate_=0;
88  }
89
90  /**
91  * @brief Destructor
92  */
93  Server::~Server(){
94  }
95
96  /**
97  * This function opens the server by creating the listener thread
98  */
99  void Server::open() {
100    COUT(4) << "opening server" << endl;
101    this->openListener();
102    return;
103  }
104
105  /**
106  * This function closes the server
107  */
108  void Server::close() {
109    COUT(4) << "closing server" << endl;
110    this->disconnectClients();
111    this->closeListener();
112    return;
113  }
114
115  bool Server::processChat(const std::string& message, unsigned int playerID){
116    ClientInformation *temp = ClientInformation::getBegin();
117    packet::Chat *chat;
118    while(temp){
119      chat = new packet::Chat(message, playerID);
120      chat->setClientID(temp->getID());
121      if(!chat->send())
122        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
123      temp = temp->next();
124    }
125//    COUT(1) << "Player " << playerID << ": " << message << std::endl;
126    return true;
127  }
128
129
130  /**
131  * Run this function once every tick
132  * calls processQueue and updateGamestate
133  * @param time time since last tick
134  */
135  void Server::update(const Clock& time) {
136    // receive incoming packets
137    Connection::processQueue();
138   
139    if ( ClientInformation::hasClients() )
140    {
141      // process incoming gamestates
142      GamestateManager::processGamestates();
143     
144      // send function calls to clients
145      FunctionCallManager::sendCalls();
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
155    }
156  }
157
158  bool Server::queuePacket(ENetPacket *packet, int clientID){
159    return ServerConnection::addPacket(packet, clientID);
160  }
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  }
169
170  /**
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  }
177
178  /**
179  * takes a new snapshot of the gamestate and sends it to the clients
180  */
181  void Server::updateGamestate() {
182    if( ClientInformation::getBegin()==NULL )
183      //no client connected
184      return;
185    GamestateManager::update();
186    COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
187    //std::cout << "updated gamestate, sending it" << std::endl;
188    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
189    sendGameState();
190    sendObjectDeletes();
191    COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
192    //std::cout << "sent gamestate" << std::endl;
193  }
194
195  bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){
196    packet::Packet *p = packet::Packet::createPacket(packet, peer);
197    return p->process();
198  }
199
200  /**
201  * sends the gamestate
202  */
203  bool Server::sendGameState() {
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();
236    return true;
237  }
238
239  bool Server::sendObjectDeletes(){
240    ClientInformation *temp = ClientInformation::getBegin();
241    if( temp == NULL )
242      //no client connected
243      return true;
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    }
263    delete del;
264    return true;
265  }
266
267
268  void Server::addClient(ENetEvent *event){
269    static unsigned int newid=1;
270
271    COUT(2) << "Server: adding client" << std::endl;
272    ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
273    if(!temp){
274      COUT(2) << "Server: could not add client" << std::endl;
275    }
276    temp->setID(newid);
277    temp->setPeer(event->peer);
278
279    // inform all the listeners
280    ObjectList<ClientConnectionListener>::iterator listener = ObjectList<ClientConnectionListener>::begin();
281    while(listener){
282      listener->clientConnected(newid);
283      listener++;
284    }
285
286    ++newid;
287
288    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
289    createClient(temp->getID());
290}
291
292  bool Server::createClient(int clientID){
293    ClientInformation *temp = ClientInformation::findClient(clientID);
294    if(!temp){
295      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
296      return false;
297    }
298    COUT(5) << "Con.Man: creating client id: " << temp->getID() << std::endl;
299   
300    // synchronise class ids
301    syncClassid(temp->getID());
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   
309    temp->setSynched(true);
310    COUT(4) << "sending welcome" << std::endl;
311    packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID());
312    w->setClientID(temp->getID());
313    b = w->send();
314    assert(b);
315    packet::Gamestate *g = new packet::Gamestate();
316    g->setClientID(temp->getID());
317    b = g->collectData(0,0x1);
318    if(!b)
319      return false; //no data for the client
320    b = g->compressData();
321    assert(b);
322    b = g->send();
323    assert(b);
324    return true;
325  }
326 
327  void Server::disconnectClient( ClientInformation *client ){
328    ServerConnection::disconnectClient( client );
329    GamestateManager::removeClient(client);
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
337  }
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){
348    ClientInformation *temp = ClientInformation::getBegin();
349    packet::Chat *chat;
350    while(temp){
351      chat = new packet::Chat(message, clientID);
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    }
357//    COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl;
358    for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it)
359      it->incomingChat(message, clientID);
360
361    return true;
362  }
363
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
375}
Note: See TracBrowser for help on using the repository browser.