Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/Server.cc @ 1706

Last change on this file since 1706 was 1705, checked in by scheusso, 16 years ago

further changes

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