Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

removed crc testing and changes debug output

  • Property svn:eol-style set to native
File size: 12.0 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(5) << "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(5) << "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(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
232      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
233      int gid = temp->getGamestateID(); //get gamestate id
234      int cid = temp->getID(); //get client id
235      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
236      GameStateCompressed *gs = gamestates->popGameState(cid);
237      if(gs==NULL){
238        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
239        continue;
240      }
241      //std::cout << "adding gamestate" << std::endl;
242      ENetPacket *packet = packet_gen.gstate(gs);
243      if(!packet)
244        continue;
245      if ( !(connection->addPacket(packet, cid)) ){
246        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 
247        temp->addFailure();
248        /*if(temp->getFailures() > 0 )
249          disconnectClient(temp);*/
250      //std::cout << "added gamestate" << std::endl;
251      }else
252        temp->resetFailures();
253      added=true;
254      temp=temp->next();
255      // now delete gamestate
256      delete[] gs->data;
257      delete gs;
258    }
259    /*if(added) {
260      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
261      return connection->sendPackets();
262    }*/
263    //COUT(5) << "Server: had no gamestates to send" << std::endl;
264    return true;
265  }
266
267  void Server::processAck( ack *data, int clientID) {
268    COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
269    gamestates->ackGameState(clientID, data->a);
270    delete data;
271  }
272 
273  bool Server::processConnectRequest( connectRequest *con, int clientID ){
274    //(COUT(3) << "processing connectRequest " << std::endl;
275    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
276    //createClient(clientID);
277    delete con;
278    return true;
279  }
280 
281  void Server::processGamestate( GameStateCompressed *data, int clientID){
282    COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
283    gamestates->addGameState(data, clientID);
284        /*COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
285    else
286      if(clients->findClient(clientID))
287        clients->findClient(clientID)->resetFailures();*/
288  }
289 
290  bool Server::addClient(ENetEvent *event){
291    ClientInformation *temp = clients->insertBack(new ClientInformation);
292    if(!temp){
293      COUT(2) << "Server: could not add client" << std::endl;
294      return false;
295    }
296    if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
297      temp->prev()->setID(0); //bugfix: not necessary but usefull
298      temp->setID(1);
299    }
300    else
301      temp->setID(temp->prev()->getID()+1);
302    temp->setPeer(event->peer);
303    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
304    return createClient(temp->getID());
305  }
306 
307  bool Server::createClient(int clientID){
308    ClientInformation *temp = clients->findClient(clientID);
309    if(!temp){
310      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
311      return false;
312    }
313    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
314    connection->syncClassid(temp->getID());
315    COUT(5) << "creating spaceship for clientid: " << temp->getID() << std::endl;
316    // TODO: this is only a hack, untill we have a possibility to define default player-join actions
317    if(!createShip(temp))
318      COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
319    else
320      COUT(3) << "created spaceship" << std::endl;
321    temp->setSynched(true);
322    COUT(3) << "sending welcome" << std::endl;
323    connection->sendWelcome(temp->getID(), temp->getShipID(), true);
324    return true;
325  }
326 
327  bool Server::createShip(ClientInformation *client){
328    if(!client)
329      return false;
330    orxonox::Identifier* id = ID("SpaceShip");
331    if(!id){
332      COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
333      return false;
334    }
335    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
336    no->setPosition(orxonox::Vector3(0,0,80));
337    no->setScale(10);
338    //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
339    no->setMesh("assff.mesh");
340    no->setMaxSpeed(500);
341    no->setMaxSideAndBackSpeed(50);
342    no->setMaxRotation(1.0);
343    no->setTransAcc(200);
344    no->setRotAcc(3.0);
345    no->setTransDamp(75);
346    no->setRotDamp(1.0);
347    no->setCamera("cam_"+client->getID());
348    no->classID = id->getNetworkID();
349    no->create();
350   
351    client->setShipID(no->objectID);
352    return true;
353  }
354 
355  bool Server::disconnectClient(ENetEvent *event){
356    COUT(4) << "removing client from list" << std::endl;
357    //return removeClient(head_->findClient(&(peer->address))->getID());
358   
359    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
360    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
361    ClientInformation *client = clients->findClient(&event->peer->address);
362    if(!client)
363      return false;
364    while(it){
365      if(it->objectID!=client->getShipID()){
366        ++it;
367        continue;
368      }
369      orxonox::Iterator<orxonox::SpaceShip> temp=it;
370      ++it;
371      delete  *temp;
372      return clients->removeClient(event->peer);
373    }
374    return false;
375  }
376
377  void Server::disconnectClient(int clientID){
378    ClientInformation *client = clients->findClient(clientID);
379    if(client)
380      disconnectClient(client);
381  }
382  void Server::disconnectClient( ClientInformation *client){
383    connection->disconnectClient(client);
384    gamestates->removeClient(client);
385  }
386 
387}
Note: See TracBrowser for help on using the repository browser.