Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged FICN back into trunk
awaiting release.

File size: 4.4 KB
Line 
1//
2// C++ Implementation: Server
3//
4// Description:
5//
6//
7// Author:  Oliver Scheuss, (C) 2007
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12
13#include <iostream>
14
15#include "ConnectionManager.h"
16#include "PacketTypes.h"
17#include "GameStateManager.h"
18#include "ClientInformation.h"
19//#include "NetworkFrameListener.h"
20#include "Server.h"
21
22
23namespace network
24{
25  /**
26  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
27  *
28  */
29  Server::Server() {
30    packet_gen = PacketGenerator();
31    clients = new ClientInformation(true);
32    connection = new ConnectionManager(clients);
33    gamestates = new GameStateManager(clients);
34  }
35
36  /**
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) {
42    packet_gen = PacketGenerator();
43    clients = new ClientInformation();
44    connection = new ConnectionManager(port, bindAddress, clients);
45    gamestates = new GameStateManager(clients);
46  }
47
48  /**
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) {
54    packet_gen = PacketGenerator();
55    clients = new ClientInformation();
56    connection = new ConnectionManager(port, bindAddress, clients);
57    gamestates = new GameStateManager(clients);
58  }
59
60  /**
61  * This function opens the server by creating the listener thread
62  */
63  void Server::open() {
64    connection->createListener();
65    return;
66  }
67
68  /**
69  * This function closes the server
70  */
71  void Server::close() {
72    connection->quitListener();
73    return;
74  }
75
76  /**
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) {
82    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
83    //std::cout <<"adding packets" << std::endl;
84    connection->addPacketAll(packet);
85    //std::cout <<"added packets" << std::endl;
86    return connection->sendPackets();
87  }
88
89  /**
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) {
95    ENetPacket *packet = packet_gen.chatMessage(msg);
96    std::cout <<"adding Packets" << std::endl;
97    connection->addPacketAll(packet);
98    //std::cout <<"added packets" << std::endl;
99    if (connection->sendPackets()){
100      std::cout << "Sucessfully" << std::endl;
101      return true;
102    }
103    return false;
104  }
105
106  /**
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) {
112    processQueue();
113    updateGamestate();
114    return;
115  }
116
117  /**
118  * processes all the packets waiting in the queue
119  */
120  void Server::processQueue() {
121    ENetPacket *packet;
122    int clientID=-1;
123    while(!connection->queueEmpty()){
124      //std::cout << "Client " << clientID << " sent: " << std::endl;
125      packet = connection->getPacket(clientID);
126      elaborate(packet, clientID);
127    }
128  }
129
130  /**
131  * takes a new snapshot of the gamestate and sends it to the clients
132  */
133  void Server::updateGamestate() {
134    gamestates->update();
135    //std::cout << "updated gamestate, sending it" << std::endl;
136    sendGameState();
137    //std::cout << "sent gamestate" << std::endl;
138  }
139
140  /**
141  * sends the gamestate
142  */
143  bool Server::sendGameState() {
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      }
152      if( !(temp->getSynched()) ){
153        std::cout << "not sending gamestate" << std::endl;
154        temp=temp->next();
155        continue;
156      }
157      std::cout << "doing gamestate" << std::endl;
158      int gid = temp->getGamestateID();
159      int cid = temp->getID();
160      std::cout << "server, got acked ID: " << gid << std::endl;
161      GameStateCompressed *gs = &(gamestates->popGameState(cid)); // FIXME: taking address of temporary, check if correct
162      //std::cout << "adding gamestate" << std::endl;
163      connection->addPacket(packet_gen.gstate(gs), cid);
164      //std::cout << "added gamestate" << std::endl;
165      added=true;
166      temp=temp->next();
167    }
168    if(added)
169      return connection->sendPackets();
170    else
171      return false;
172  }
173
174  void Server::processAck( ack *data, int clientID) {
175    clients->findClient(clientID)->setGamestateID(data->a);
176  }
177
178}
Note: See TracBrowser for help on using the repository browser.