Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/Server.cc @ 663

Last change on this file since 663 was 660, checked in by nicolasc, 17 years ago
  • added FIXME-tags - should be selfexplanatory
File size: 4.3 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
13
[285]14#include "Server.h"
[230]15
16namespace network{
[285]17
[230]18  /**
19   * Constructor for default values (bindaddress is set to ENET_HOST_ANY
[285]20   *
[230]21   */
22  Server::Server(){
[369]23    packet_gen = PacketGenerator();
[444]24    clients = new ClientInformation(true);
[436]25    connection = new ConnectionManager(clients);
26    gamestates = new GameStateManager(clients);
[230]27  }
[285]28
[230]29  /**
30   * Constructor
31   * @param port Port to listen on
32   * @param bindAddress Address to listen on
33   */
[369]34  Server::Server(int port, std::string bindAddress){
35    packet_gen = PacketGenerator();
[436]36    clients = new ClientInformation();
37    connection = new ConnectionManager(port, bindAddress, clients);
38    gamestates = new GameStateManager(clients);
[230]39  }
[285]40
[230]41  /**
42   * Constructor
43   * @param port Port to listen on
44   * @param bindAddress Address to listen on
45   */
[369]46  Server::Server(int port, const char *bindAddress){
47    packet_gen = PacketGenerator();
[436]48    clients = new ClientInformation();
49    connection = new ConnectionManager(port, bindAddress, clients);
50    gamestates = new GameStateManager(clients);
[230]51  }
[660]52
[369]53  /**
54   * This function opens the server by creating the listener thread
55   */
56  void Server::open(){
[436]57    connection->createListener();
[369]58    return;
59  }
[660]60
[369]61  /**
62   * This function closes the server
63   */
64  void Server::close(){
[436]65    connection->quitListener();
[369]66    return;
67  }
[660]68
[369]69  /**
70   * This function sends out a message to all clients
71   * @param msg message
72   * @return true/false
73   */
74  bool Server::sendMSG(std::string msg){
75    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
[479]76    //std::cout <<"adding packets" << std::endl;
[436]77    connection->addPacketAll(packet);
[479]78    //std::cout <<"added packets" << std::endl;
[436]79    return connection->sendPackets();
[369]80  }
81  /**
82   * This function sends out a message to all clients
83   * @param msg message
84   * @return true/false
85   */
86  bool Server::sendMSG(const char *msg){
87    ENetPacket *packet = packet_gen.chatMessage(msg);
[632]88    std::cout <<"adding Packets" << std::endl;
[436]89    connection->addPacketAll(packet);
[479]90    //std::cout <<"added packets" << std::endl;
[601]91    if (connection->sendPackets()){
[632]92        std::cout << "Sucessfully" << std::endl;
[601]93        return true;
94    }
95    return false;
[369]96  }
[660]97
[380]98  /**
99   * Run this function once every tick
100   * calls processQueue and updateGamestate
[459]101   * @param time time since last tick
[380]102   */
[459]103  void Server::tick(float time){
[380]104    processQueue();
105    updateGamestate();
106    return;
[369]107  }
[660]108
[380]109  /**
110   * processes all the packets waiting in the queue
111   */
112  void Server::processQueue(){
113    ENetPacket *packet;
[381]114    int clientID=-1;
[436]115    while(!connection->queueEmpty()){
[479]116      //std::cout << "Client " << clientID << " sent: " << std::endl;
[436]117      packet = connection->getPacket(clientID);
[422]118      elaborate(packet, clientID);
[380]119    }
120  }
[660]121
[380]122  /**
123   * takes a new snapshot of the gamestate and sends it to the clients
124   */
125  void Server::updateGamestate(){
[436]126    gamestates->update();
[606]127    //std::cout << "updated gamestate, sending it" << std::endl;
[422]128    sendGameState();
[606]129    //std::cout << "sent gamestate" << std::endl;
[380]130  }
[660]131
[425]132  /**
133   * sends the gamestate
134   */
[422]135  bool Server::sendGameState(){
[607]136    std::cout << "starting gamestate" << std::endl;
137    ClientInformation *temp = clients;
138    bool added=false;
139    while(temp!=NULL){
140      if(temp->head){
141        temp=temp->next();
142        continue;
143      }
[636]144      if( !(temp->getSynched()) ){
145        std::cout << "not sending gamestate" << std::endl;
146        temp=temp->next();
147        continue;
148      }
[607]149      std::cout << "doing gamestate" << std::endl;
[624]150      int gid = temp->getGamestateID();
151      int cid = temp->getID();
152      std::cout << "server, got acked ID: " << gid << std::endl;
[660]153      GameStateCompressed *gs = &(gamestates->popGameState(cid)); // FIXME: taking address of temporary, check if correct
[624]154      //std::cout << "adding gamestate" << std::endl;
155      connection->addPacket(packet_gen.gstate(gs), cid);
156      //std::cout << "added gamestate" << std::endl;
[607]157      added=true;
158      temp=temp->next();
[605]159    }
[607]160    if(added)
161      return connection->sendPackets();
162    else return false;
163    //return true;
[422]164  }
[660]165
[620]166  void Server::processAck( ack *data, int clientID){
[624]167    clients->findClient(clientID)->setGamestateID(data->a);
[620]168  }
[660]169
[230]170}
Note: See TracBrowser for help on using the repository browser.