Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/shader/src/network/Server.cc @ 2490

Last change on this file since 2490 was 1505, checked in by rgrieder, 17 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

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