Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1061 was 1056, checked in by landauf, 18 years ago

don't panic, no codechanges!
added a link to www.orxonox.net

File size: 5.7 KB
RevLine 
[1056]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
[230]29//
30// C++ Implementation: Server
31//
[285]32// Description:
[230]33//
34//
35// Author:  Oliver Scheuss, (C) 2007
36//
37// Copyright: See COPYING file that comes with this distribution
38//
39//
40
[777]41#include <iostream>
[230]42
[777]43#include "ConnectionManager.h"
44#include "PacketTypes.h"
45#include "GameStateManager.h"
46#include "ClientInformation.h"
47//#include "NetworkFrameListener.h"
[1021]48#include "util/Sleep.h"
[285]49#include "Server.h"
[230]50
[285]51
[777]52namespace network
53{
[230]54  /**
[777]55  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
56  *
57  */
58  Server::Server() {
[369]59    packet_gen = PacketGenerator();
[444]60    clients = new ClientInformation(true);
[436]61    connection = new ConnectionManager(clients);
62    gamestates = new GameStateManager(clients);
[230]63  }
[285]64
[230]65  /**
[777]66  * Constructor
67  * @param port Port to listen on
68  * @param bindAddress Address to listen on
69  */
70  Server::Server(int port, std::string bindAddress) {
[369]71    packet_gen = PacketGenerator();
[436]72    clients = new ClientInformation();
73    connection = new ConnectionManager(port, bindAddress, clients);
74    gamestates = new GameStateManager(clients);
[230]75  }
[285]76
[230]77  /**
[777]78  * Constructor
79  * @param port Port to listen on
80  * @param bindAddress Address to listen on
81  */
82  Server::Server(int port, const char *bindAddress) {
[369]83    packet_gen = PacketGenerator();
[436]84    clients = new ClientInformation();
85    connection = new ConnectionManager(port, bindAddress, clients);
86    gamestates = new GameStateManager(clients);
[230]87  }
[660]88
[369]89  /**
[777]90  * This function opens the server by creating the listener thread
91  */
92  void Server::open() {
[436]93    connection->createListener();
[369]94    return;
95  }
[660]96
[369]97  /**
[777]98  * This function closes the server
99  */
100  void Server::close() {
[436]101    connection->quitListener();
[369]102    return;
103  }
[660]104
[369]105  /**
[777]106  * This function sends out a message to all clients
107  * @param msg message
108  * @return true/false
109  */
110  bool Server::sendMSG(std::string msg) {
[369]111    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
[479]112    //std::cout <<"adding packets" << std::endl;
[436]113    connection->addPacketAll(packet);
[479]114    //std::cout <<"added packets" << std::endl;
[436]115    return connection->sendPackets();
[369]116  }
[777]117
[369]118  /**
[777]119  * This function sends out a message to all clients
120  * @param msg message
121  * @return true/false
122  */
123  bool Server::sendMSG(const char *msg) {
[369]124    ENetPacket *packet = packet_gen.chatMessage(msg);
[632]125    std::cout <<"adding Packets" << std::endl;
[436]126    connection->addPacketAll(packet);
[479]127    //std::cout <<"added packets" << std::endl;
[601]128    if (connection->sendPackets()){
[777]129      std::cout << "Sucessfully" << std::endl;
130      return true;
[601]131    }
132    return false;
[369]133  }
[660]134
[380]135  /**
[777]136  * Run this function once every tick
137  * calls processQueue and updateGamestate
138  * @param time time since last tick
139  */
140  void Server::tick(float time) {
[380]141    processQueue();
142    updateGamestate();
[1021]143
144    sleep(1); // TODO remove
[380]145    return;
[369]146  }
[660]147
[380]148  /**
[777]149  * processes all the packets waiting in the queue
150  */
151  void Server::processQueue() {
[380]152    ENetPacket *packet;
[381]153    int clientID=-1;
[436]154    while(!connection->queueEmpty()){
[479]155      //std::cout << "Client " << clientID << " sent: " << std::endl;
[436]156      packet = connection->getPacket(clientID);
[422]157      elaborate(packet, clientID);
[380]158    }
159  }
[660]160
[380]161  /**
[777]162  * takes a new snapshot of the gamestate and sends it to the clients
163  */
164  void Server::updateGamestate() {
[436]165    gamestates->update();
[606]166    //std::cout << "updated gamestate, sending it" << std::endl;
[1021]167    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
168      sendGameState();
[606]169    //std::cout << "sent gamestate" << std::endl;
[380]170  }
[660]171
[425]172  /**
[777]173  * sends the gamestate
174  */
175  bool Server::sendGameState() {
[1021]176    COUT(5) << "starting sendGameState" << std::endl;
[607]177    ClientInformation *temp = clients;
178    bool added=false;
179    while(temp!=NULL){
180      if(temp->head){
181        temp=temp->next();
182        continue;
183      }
[636]184      if( !(temp->getSynched()) ){
[1021]185        COUT(5) << "not sending gamestate" << std::endl;
[636]186        temp=temp->next();
187        continue;
188      }
[1021]189      COUT(5) << "doing gamestate gamestate preparation" << std::endl;
[624]190      int gid = temp->getGamestateID();
191      int cid = temp->getID();
[1021]192      COUT(5) << "server, got acked (gamestate) ID: " << gid << std::endl;
193      GameStateCompressed *gs = gamestates->popGameState(cid);
194      if(gs==NULL){
195        COUT(2) << "could not generate gamestate" << std::endl;
196        return false;
197      }
[624]198      //std::cout << "adding gamestate" << std::endl;
199      connection->addPacket(packet_gen.gstate(gs), cid);
200      //std::cout << "added gamestate" << std::endl;
[607]201      added=true;
202      temp=temp->next();
[605]203    }
[607]204    if(added)
205      return connection->sendPackets();
[1021]206    COUT(5) << "had no gamestates to send" << std::endl;
207    return false;
[422]208  }
[660]209
[777]210  void Server::processAck( ack *data, int clientID) {
[1021]211    COUT(5) << "processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;
[624]212    clients->findClient(clientID)->setGamestateID(data->a);
[620]213  }
[660]214
[230]215}
Note: See TracBrowser for help on using the repository browser.