Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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