Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 885 was 790, checked in by nicolasc, 17 years ago

merged FICN back into trunk
awaiting release.

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