Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changed concept of threading, had to change packetbuffer (using events now)

File size: 12.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, (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 
61 
62  /**
63  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
64  *
65  */
66  Server::Server() {
67    packet_gen = PacketGenerator();
68    clients = new ClientInformation(true);
69    connection = new ConnectionManager(clients);
70    gamestates = new GameStateManager(clients);
71  }
72 
73  Server::Server(int port){
74    packet_gen = PacketGenerator();
75    clients = new ClientInformation(true);
76    connection = new ConnectionManager(clients, port);
77    gamestates = new GameStateManager(clients);
78  }
79
80  /**
81  * Constructor
82  * @param port Port to listen on
83  * @param bindAddress Address to listen on
84  */
85  Server::Server(int port, std::string bindAddress) {
86    packet_gen = PacketGenerator();
87    clients = new ClientInformation();
88    connection = new ConnectionManager(port, bindAddress, clients);
89    gamestates = new GameStateManager(clients);
90  }
91
92  /**
93  * Constructor
94  * @param port Port to listen on
95  * @param bindAddress Address to listen on
96  */
97  Server::Server(int port, const char *bindAddress) {
98    packet_gen = PacketGenerator();
99    clients = new ClientInformation();
100    connection = new ConnectionManager(port, bindAddress, clients);
101    gamestates = new GameStateManager(clients);
102  }
103
104  /**
105  * This function opens the server by creating the listener thread
106  */
107  void Server::open() {
108    connection->createListener();
109    return;
110  }
111
112  /**
113  * This function closes the server
114  */
115  void Server::close() {
116    connection->quitListener();
117    return;
118  }
119
120  /**
121  * This function sends out a message to all clients
122  * @param msg message
123  * @return true/false
124  */
125  bool Server::sendMSG(std::string msg) {
126    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
127    //std::cout <<"adding packets" << std::endl;
128    if(connection->addPacketAll(packet))
129    //std::cout <<"added packets" << std::endl;
130      return connection->sendPackets();
131    else
132      return false;
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    connection->addPacketAll(packet);
144    //std::cout <<"added packets" << std::endl;
145    if (connection->sendPackets()){
146      COUT(4) << "Server: Sucessfully" << std::endl;
147      return true;
148    }
149    return false;
150  }
151
152  /**
153  * Run this function once every tick
154  * calls processQueue and updateGamestate
155  * @param time time since last tick
156  */
157  void Server::tick(float time) {
158    processQueue();
159    updateGamestate();
160//     usleep(500000); // 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      switch( event->type ) {
177      case ENET_EVENT_TYPE_CONNECT:
178        COUT(3) << "processing event_Type_connect" << std::endl;
179        addClient(event);
180        break;
181      case ENET_EVENT_TYPE_DISCONNECT:
182        if(clients->findClient(&event->peer->address))
183          disconnectClient(event);
184        break;
185      case ENET_EVENT_TYPE_RECEIVE:
186        if(clients->findClient(&event->peer->address)){
187          clientID = clients->findClient(&event->peer->address)->getID();
188          if( !elaborate(event->packet, clientID) ) 
189            COUT(3) << "Server: could not elaborate" << std::endl;
190        }
191        break;
192      }
193      delete event;
194      //if statement to catch case that packetbuffer is empty
195    }
196  }
197
198  /**
199  * takes a new snapshot of the gamestate and sends it to the clients
200  */
201  void Server::updateGamestate() {
202    gamestates->update();
203    COUT(4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
204    //std::cout << "updated gamestate, sending it" << std::endl;
205    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
206    sendGameState();
207    COUT(4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
208    //std::cout << "sent gamestate" << std::endl;
209  }
210
211  /**
212  * sends the gamestate
213  */
214  bool Server::sendGameState() {
215    COUT(5) << "Server: starting function sendGameState" << std::endl;
216    boost::recursive_mutex::scoped_lock lock(clients->mutex_);
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        return false;
239      }
240      //std::cout << "adding gamestate" << std::endl;
241      if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){
242        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 
243        temp->addFailure();
244        if(temp->getFailures() > 20 )
245          disconnectClient(temp);
246      //std::cout << "added gamestate" << std::endl;
247      }
248      added=true;
249      temp=temp->next();
250      // now delete gamestate
251      delete[] gs->data;
252      delete gs;
253    }
254    if(added) {
255      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
256      return connection->sendPackets();
257    }
258    COUT(5) << "Server: had no gamestates to send" << std::endl;
259    return false;
260  }
261
262  void Server::processAck( ack *data, int clientID) {
263    COUT(4) << "\b\b\b\n\n\n\n\nServer: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
264    gamestates->ackGameState(clientID, data->a);
265    delete data;
266  }
267 
268  bool Server::processConnectRequest( connectRequest *con, int clientID ){
269    //(COUT(3) << "processing connectRequest " << std::endl;
270    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
271    //createClient(clientID);
272    delete con;
273    return true;
274  }
275 
276  void Server::processGamestate( GameStateCompressed *data, int clientID){
277    COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
278    if(!gamestates->pushGameState(data, clientID))
279        COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
280    else
281      if(clients->findClient(clientID))
282        clients->findClient(clientID)->resetFailures();
283  }
284 
285  bool Server::addClient(ENetEvent *event){
286    ClientInformation *temp = clients->insertBack(new ClientInformation);
287    if(!temp){
288      COUT(2) << "Server: could not add client" << std::endl;
289      return false;
290    }
291    if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
292      temp->prev()->setID(0); //bugfix: not necessary but usefull
293      temp->setID(1);
294    }
295    else
296      temp->setID(temp->prev()->getID()+1);
297    temp->setPeer(event->peer);
298    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
299    return createClient(temp->getID());
300  }
301 
302  bool Server::createClient(int clientID){
303    ClientInformation *temp = clients->findClient(clientID);
304    if(!temp){
305      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
306      return false;
307    }
308    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
309    connection->syncClassid(temp->getID());
310    COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl;
311    // TODO: this is only a hack, untill we have a possibility to define default player-join actions
312    if(!createShip(temp))
313      COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
314    else
315      COUT(3) << "created spaceship" << std::endl;
316    temp->setSynched(true);
317    COUT(3) << "sending welcome" << std::endl;
318    connection->sendWelcome(temp->getID(), temp->getShipID(), true);
319    return true;
320  }
321 
322  bool Server::createShip(ClientInformation *client){
323    if(!client)
324      return false;
325    orxonox::Identifier* id = ID("SpaceShip");
326    if(!id){
327      COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
328      return false;
329    }
330    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
331    no->setPosition(orxonox::Vector3(0,0,80));
332    no->setScale(10);
333    //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
334    no->setMesh("assff.mesh");
335    no->setMaxSpeed(500);
336    no->setMaxSideAndBackSpeed(50);
337    no->setMaxRotation(1.0);
338    no->setTransAcc(200);
339    no->setRotAcc(3.0);
340    no->setTransDamp(75);
341    no->setRotDamp(1.0);
342    no->setCamera("cam_"+client->getID());
343    no->classID = id->getNetworkID();
344    no->create();
345   
346    client->setShipID(no->objectID);
347    return true;
348  }
349 
350  bool Server::disconnectClient(ENetEvent *event){
351    COUT(4) << "removing client from list" << std::endl;
352    //return removeClient(head_->findClient(&(peer->address))->getID());
353   
354    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
355    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
356    ClientInformation *client = clients->findClient(&event->peer->address);
357    if(!client)
358      return false;
359    while(it){
360      if(it->objectID!=client->getShipID()){
361        ++it;
362        continue;
363      }
364      orxonox::Iterator<orxonox::SpaceShip> temp=it;
365      ++it;
366      delete  *temp;
367      return clients->removeClient(event->peer);
368    }
369    return false;
370  }
371
372  void Server::disconnectClient(int clientID){
373    ClientInformation *client = clients->findClient(clientID);
374    if(client)
375      disconnectClient(client);
376  }
377  void Server::disconnectClient( ClientInformation *client){
378    connection->disconnectClient(client);
379    gamestates->removeClient(client);
380  }
381 
382}
Note: See TracBrowser for help on using the repository browser.