Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added more bugfixing and debug output

File size: 4.6 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    if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
137      sendGameState();
138    //std::cout << "sent gamestate" << std::endl;
139  }
140
141  /**
142  * sends the gamestate
143  */
144  bool Server::sendGameState() {
145    COUT(5) << "starting sendGameState" << std::endl;
146    ClientInformation *temp = clients;
147    bool added=false;
148    while(temp!=NULL){
149      if(temp->head){
150        temp=temp->next();
151        continue;
152      }
153      if( !(temp->getSynched()) ){
154        std::cout << "not sending gamestate" << std::endl;
155        temp=temp->next();
156        continue;
157      }
158      std::cout << "doing gamestate" << std::endl;
159      int gid = temp->getGamestateID();
160      int cid = temp->getID();
161      std::cout << "server, got acked ID: " << gid << std::endl;
162      GameStateCompressed *gs = gamestates->popGameState(cid);
163      if(gs==NULL){
164        COUT(2) << "could not generate gamestate" << std::endl;
165        return false;
166      }
167      //std::cout << "adding gamestate" << std::endl;
168      connection->addPacket(packet_gen.gstate(gs), cid);
169      //std::cout << "added gamestate" << std::endl;
170      added=true;
171      temp=temp->next();
172    }
173    if(added)
174      return connection->sendPackets();
175    COUT(2) << "could not send packets (containing i.e. gamestates)" << std::endl;
176    return false;
177  }
178
179  void Server::processAck( ack *data, int clientID) {
180    clients->findClient(clientID)->setGamestateID(data->a);
181  }
182
183}
Note: See TracBrowser for help on using the repository browser.