Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud2/src/network/Server.cc @ 1075

Last change on this file since 1075 was 918, checked in by rgrieder, 17 years ago
  • created basic construct for the InputHandler (mere copy of InputManager but without the Listeners)
  • some msvc related fixes with winsocks.h and winsocks2.h
  • fix in Tickable.h (due to lazy displacement to new folder)
File size: 4.8 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 "util/Sleep.h"
21#include "Server.h"
22
23
24namespace network
25{
26  /**
27  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
28  *
29  */
30  Server::Server() {
31    packet_gen = PacketGenerator();
32    clients = new ClientInformation(true);
33    connection = new ConnectionManager(clients);
34    gamestates = new GameStateManager(clients);
35  }
36
37  /**
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) {
43    packet_gen = PacketGenerator();
44    clients = new ClientInformation();
45    connection = new ConnectionManager(port, bindAddress, clients);
46    gamestates = new GameStateManager(clients);
47  }
48
49  /**
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) {
55    packet_gen = PacketGenerator();
56    clients = new ClientInformation();
57    connection = new ConnectionManager(port, bindAddress, clients);
58    gamestates = new GameStateManager(clients);
59  }
60
61  /**
62  * This function opens the server by creating the listener thread
63  */
64  void Server::open() {
65    connection->createListener();
66    return;
67  }
68
69  /**
70  * This function closes the server
71  */
72  void Server::close() {
73    connection->quitListener();
74    return;
75  }
76
77  /**
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) {
83    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
84    //std::cout <<"adding packets" << std::endl;
85    connection->addPacketAll(packet);
86    //std::cout <<"added packets" << std::endl;
87    return connection->sendPackets();
88  }
89
90  /**
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) {
96    ENetPacket *packet = packet_gen.chatMessage(msg);
97    std::cout <<"adding Packets" << std::endl;
98    connection->addPacketAll(packet);
99    //std::cout <<"added packets" << std::endl;
100    if (connection->sendPackets()){
101      std::cout << "Sucessfully" << std::endl;
102      return true;
103    }
104    return false;
105  }
106
107  /**
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) {
113    processQueue();
114    updateGamestate();
115
116    sleep(1); // TODO remove
117    return;
118  }
119
120  /**
121  * processes all the packets waiting in the queue
122  */
123  void Server::processQueue() {
124    ENetPacket *packet;
125    int clientID=-1;
126    while(!connection->queueEmpty()){
127      //std::cout << "Client " << clientID << " sent: " << std::endl;
128      packet = connection->getPacket(clientID);
129      elaborate(packet, clientID);
130    }
131  }
132
133  /**
134  * takes a new snapshot of the gamestate and sends it to the clients
135  */
136  void Server::updateGamestate() {
137    gamestates->update();
138    //std::cout << "updated gamestate, sending it" << std::endl;
139    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
140      sendGameState();
141    //std::cout << "sent gamestate" << std::endl;
142  }
143
144  /**
145  * sends the gamestate
146  */
147  bool Server::sendGameState() {
148    COUT(5) << "starting sendGameState" << std::endl;
149    ClientInformation *temp = clients;
150    bool added=false;
151    while(temp!=NULL){
152      if(temp->head){
153        temp=temp->next();
154        continue;
155      }
156      if( !(temp->getSynched()) ){
157        COUT(5) << "not sending gamestate" << std::endl;
158        temp=temp->next();
159        continue;
160      }
161      COUT(5) << "doing gamestate gamestate preparation" << std::endl;
162      int gid = temp->getGamestateID();
163      int cid = temp->getID();
164      COUT(5) << "server, got acked (gamestate) ID: " << gid << std::endl;
165      GameStateCompressed *gs = gamestates->popGameState(cid);
166      if(gs==NULL){
167        COUT(2) << "could not generate gamestate" << std::endl;
168        return false;
169      }
170      //std::cout << "adding gamestate" << std::endl;
171      connection->addPacket(packet_gen.gstate(gs), cid);
172      //std::cout << "added gamestate" << std::endl;
173      added=true;
174      temp=temp->next();
175    }
176    if(added)
177      return connection->sendPackets();
178    COUT(5) << "had no gamestates to send" << std::endl;
179    return false;
180  }
181
182  void Server::processAck( ack *data, int clientID) {
183    COUT(5) << "processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;
184    clients->findClient(clientID)->setGamestateID(data->a);
185  }
186
187}
Note: See TracBrowser for help on using the repository browser.