Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

forgot something

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