Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/Server.cc @ 1740

Last change on this file since 1740 was 1735, checked in by scheusso, 16 years ago

network branch merged into trunk

  • Property svn:eol-style set to native
File size: 12.1 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:
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
45
46#include "ConnectionManager.h"
[1735]47#include "GamestateManager.h"
[1502]48#include "ClientInformation.h"
49#include "util/Sleep.h"
50#include "objects/SpaceShip.h"
[1534]51#include "core/ConsoleCommand.h"
[1735]52#include "packet/Chat.h"
53#include "packet/Packet.h"
54#include "packet/Welcome.h"
[1502]55
56namespace network
57{
[1534]58  #define MAX_FAILURES 20;
59  #define NETWORK_FREQUENCY 30
[1502]60 
61  /**
62  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
63  *
64  */
65  Server::Server() {
66    timeSinceLastUpdate_=0;
[1735]67    connection = new ConnectionManager();
68    gamestates_ = new GamestateManager();
[1502]69  }
70 
71  Server::Server(int port){
72    timeSinceLastUpdate_=0;
[1735]73    connection = new ConnectionManager(port);
74    gamestates_ = new GamestateManager();
[1502]75  }
76
77  /**
78  * Constructor
79  * @param port Port to listen on
80  * @param bindAddress Address to listen on
81  */
82  Server::Server(int port, std::string bindAddress) {
83    timeSinceLastUpdate_=0;
[1735]84    connection = new ConnectionManager(port, bindAddress);
85    gamestates_ = new GamestateManager();
[1502]86  }
87
88  /**
89  * Constructor
90  * @param port Port to listen on
91  * @param bindAddress Address to listen on
92  */
93  Server::Server(int port, const char *bindAddress) {
94    timeSinceLastUpdate_=0;
[1735]95    connection = new ConnectionManager(port, bindAddress);
96    gamestates_ = new GamestateManager();
[1502]97  }
98
99  /**
100  * This function opens the server by creating the listener thread
101  */
102  void Server::open() {
103    connection->createListener();
104    return;
105  }
106
107  /**
108  * This function closes the server
109  */
110  void Server::close() {
111    connection->quitListener();
112    return;
113  }
114
[1735]115  bool Server::processChat(packet::Chat *message, unsigned int clientID){
116    ClientInformation *temp = ClientInformation::getBegin();
117    while(temp){
118      message->setClientID(temp->getID());
119      if(!message->send())
120        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
121      temp = temp->next();
122    }
123    return message->process();
124  }
125 
[1502]126  /**
127  * This function sends out a message to all clients
128  * @param msg message
129  * @return true/false
130  */
[1735]131  bool Server::sendChat(packet::Chat *chat) {
132    //TODO: change this (no informations about who wrote a message)
133    assert(0);
134    ClientInformation *temp = ClientInformation::getBegin();
135    while(temp){
136      chat->setClientID(temp->getID());
137      if(!chat->send())
138        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
139    }
140    return chat->process();;
[1502]141  }
142
143  /**
144  * This function sends out a message to all clients
145  * @param msg message
146  * @return true/false
147  */
[1735]148//   bool Server::sendChat(const char *msg) {
149//     char *message = new char [strlen(msg)+10+1];
150//     sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg);
151//     COUT(1) << message << std::endl;
152//     ENetPacket *packet = packet_gen.chatMessage(message);
153//     COUT(5) <<"Server: adding Packets" << std::endl;
154//     return connection->addPacketAll(packet);
155//   }
[1502]156
157  /**
158  * Run this function once every tick
159  * calls processQueue and updateGamestate
160  * @param time time since last tick
161  */
162  void Server::tick(float time) {
163    processQueue();
164    //this steers our network frequency
165    timeSinceLastUpdate_+=time;
166    if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){
[1548]167      timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_;
168//      timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;
[1735]169      gamestates_->processGamestates();
[1502]170      updateGamestate();
171    }
[1548]172    /*while(timeSinceLastUpdate_>1./NETWORK_FREQUENCY)
173      timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;*/
[1502]174//     usleep(5000); // TODO remove
175    return;
176  }
[1735]177 
178  bool Server::queuePacket(ENetPacket *packet, int clientID){
179    return connection->addPacket(packet, clientID);
180  }
[1502]181
182  /**
183  * processes all the packets waiting in the queue
184  */
185  void Server::processQueue() {
186    ENetEvent *event;
187    while(!connection->queueEmpty()){
188      //std::cout << "Client " << clientID << " sent: " << std::endl;
189      //clientID here is a reference to grab clientID from ClientInformation
190      event = connection->getEvent();
191      if(!event)
192        continue;
193      assert(event->type != ENET_EVENT_TYPE_NONE);
194      switch( event->type ) {
195      case ENET_EVENT_TYPE_CONNECT:
196        COUT(3) << "processing event_Type_connect" << std::endl;
197        addClient(event);
198        break;
199      case ENET_EVENT_TYPE_DISCONNECT:
[1735]200        if(ClientInformation::findClient(&event->peer->address))
[1502]201          disconnectClient(event);
202        break;
203      case ENET_EVENT_TYPE_RECEIVE:
[1735]204        if(!processPacket(event->packet, event->peer))
205          COUT(3) << "processing incoming packet failed" << std::endl;
[1502]206        break;
[1556]207      default:
208        break;
[1502]209      }
210      delete event;
211      //if statement to catch case that packetbuffer is empty
212    }
213  }
214
215  /**
216  * takes a new snapshot of the gamestate and sends it to the clients
217  */
218  void Server::updateGamestate() {
[1735]219    gamestates_->update();
[1534]220    COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
[1502]221    //std::cout << "updated gamestate, sending it" << std::endl;
222    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
223    sendGameState();
[1534]224    COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
[1502]225    //std::cout << "sent gamestate" << std::endl;
226  }
227
[1735]228  bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){
229    packet::Packet *p = packet::Packet::createPacket(packet, peer);
230    return p->process();
231  }
232 
[1502]233  /**
234  * sends the gamestate
235  */
236  bool Server::sendGameState() {
237    COUT(5) << "Server: starting function sendGameState" << std::endl;
[1735]238    ClientInformation *temp = ClientInformation::getBegin();
[1502]239    bool added=false;
240    while(temp != NULL){
241      if( !(temp->getSynched()) ){
242        COUT(5) << "Server: not sending gamestate" << std::endl;
243        temp=temp->next();
[1735]244        if(!temp)
245          break;
[1502]246        //think this works without continue
247        continue;
248      }
[1534]249      COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
[1502]250      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
251      int gid = temp->getGamestateID(); //get gamestate id
252      int cid = temp->getID(); //get client id
253      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
[1735]254      packet::Gamestate *gs = gamestates_->popGameState(cid);
[1502]255      if(gs==NULL){
256        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
257        continue;
258      }
259      //std::cout << "adding gamestate" << std::endl;
[1735]260      gs->setClientID(cid);
261      assert(gs->compressData());
262      if ( !gs->send() ){
[1502]263        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 
264        temp->addFailure();
265      }else
266        temp->resetFailures();
267      added=true;
268      temp=temp->next();
[1735]269      // gs gets automatically deleted by enet callback
[1502]270    }
271    /*if(added) {
272      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
273      return connection->sendPackets();
274    }*/
275    //COUT(5) << "Server: had no gamestates to send" << std::endl;
276    return true;
277  }
278 
[1735]279//   void Server::processChat( chat *data, int clientId){
280//     char *message = new char [strlen(data->message)+10+1];
281//     sprintf(message, "Player %d: %s", clientId, data->message);
282//     COUT(1) << message << std::endl;
283//     ENetPacket *pck = packet_gen.chatMessage(message);
284//     connection->addPacketAll(pck);
285//     delete[] data->message;
286//     delete data;
287//   }
[1502]288 
289  bool Server::addClient(ENetEvent *event){
[1735]290    ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
[1502]291    if(!temp){
292      COUT(2) << "Server: could not add client" << std::endl;
293      return false;
294    }
[1735]295    if(temp->prev()->getBegin()) { //not good if you use anything else than insertBack
[1502]296      temp->prev()->setID(0); //bugfix: not necessary but usefull
297      temp->setID(1);
298    }
299    else
300      temp->setID(temp->prev()->getID()+1);
301    temp->setPeer(event->peer);
302    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
303    return createClient(temp->getID());
304  }
305 
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    }
312    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
313    connection->syncClassid(temp->getID());
[1534]314    COUT(5) << "creating spaceship for clientid: " << temp->getID() << std::endl;
[1502]315    // TODO: this is only a hack, untill we have a possibility to define default player-join actions
316    if(!createShip(temp))
317      COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
318    else
319      COUT(3) << "created spaceship" << std::endl;
320    temp->setSynched(true);
321    COUT(3) << "sending welcome" << std::endl;
[1735]322    packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID());
323    w->setClientID(temp->getID());
324    assert(w->send());
[1502]325    return true;
326  }
327 
328  bool Server::createShip(ClientInformation *client){
329    if(!client)
330      return false;
331    orxonox::Identifier* id = ID("SpaceShip");
332    if(!id){
333      COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
334      return false;
335    }
336    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
[1735]337    no->classID = id->getNetworkID();
338    client->setShipID(no->objectID);
[1502]339    no->setPosition(orxonox::Vector3(0,0,80));
340    no->setScale(10);
341    //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
342    no->setMesh("assff.mesh");
343    no->setMaxSpeed(500);
344    no->setMaxSideAndBackSpeed(50);
345    no->setMaxRotation(1.0);
346    no->setTransAcc(200);
347    no->setRotAcc(3.0);
348    no->setTransDamp(75);
349    no->setRotDamp(1.0);
350    no->setCamera("cam_"+client->getID());
351    no->create();
[1735]352    no->setBacksync(true);
[1502]353   
354    return true;
355  }
356 
357  bool Server::disconnectClient(ENetEvent *event){
358    COUT(4) << "removing client from list" << std::endl;
359    //return removeClient(head_->findClient(&(peer->address))->getID());
360   
361    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
362    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
[1735]363    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
[1502]364    if(!client)
365      return false;
366    while(it){
367      if(it->objectID!=client->getShipID()){
368        ++it;
369        continue;
370      }
371      orxonox::Iterator<orxonox::SpaceShip> temp=it;
372      ++it;
373      delete  *temp;
[1735]374      return ClientInformation::removeClient(event->peer);
[1502]375    }
376    return false;
377  }
378
379  void Server::disconnectClient(int clientID){
[1735]380    ClientInformation *client = ClientInformation::findClient(clientID);
[1502]381    if(client)
382      disconnectClient(client);
383  }
384  void Server::disconnectClient( ClientInformation *client){
385    connection->disconnectClient(client);
[1735]386    gamestates_->removeClient(client);
[1502]387  }
388 
389}
Note: See TracBrowser for help on using the repository browser.