Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bugfix in packetdecoder, inserted some std::

File size: 4.7 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    sleep(1); // TODO remove
115    return;
116  }
117
118  /**
119  * processes all the packets waiting in the queue
120  */
121  void Server::processQueue() {
122    ENetPacket *packet;
123    int clientID=-1;
124    while(!connection->queueEmpty()){
125      //std::cout << "Client " << clientID << " sent: " << std::endl;
126      packet = connection->getPacket(clientID);
127      elaborate(packet, clientID);
128    }
129  }
130
131  /**
132  * takes a new snapshot of the gamestate and sends it to the clients
133  */
134  void Server::updateGamestate() {
135    gamestates->update();
136    //std::cout << "updated gamestate, sending it" << std::endl;
137    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
138      sendGameState();
139    //std::cout << "sent gamestate" << std::endl;
140  }
141
142  /**
143  * sends the gamestate
144  */
145  bool Server::sendGameState() {
146    COUT(5) << "starting sendGameState" << std::endl;
147    ClientInformation *temp = clients;
148    bool added=false;
149    while(temp!=NULL){
150      if(temp->head){
151        temp=temp->next();
152        continue;
153      }
154      if( !(temp->getSynched()) ){
155        COUT(5) << "not sending gamestate" << std::endl;
156        temp=temp->next();
157        continue;
158      }
159      COUT(5) << "doing gamestate gamestate preparation" << std::endl;
160      int gid = temp->getGamestateID();
161      int cid = temp->getID();
162      COUT(5) << "server, got acked (gamestate) ID: " << gid << std::endl;
163      GameStateCompressed *gs = gamestates->popGameState(cid);
164      if(gs==NULL){
165        COUT(2) << "could not generate gamestate" << std::endl;
166        return false;
167      }
168      //std::cout << "adding gamestate" << std::endl;
169      connection->addPacket(packet_gen.gstate(gs), cid);
170      //std::cout << "added gamestate" << std::endl;
171      added=true;
172      temp=temp->next();
173    }
174    if(added)
175      return connection->sendPackets();
176    COUT(5) << "had no gamestates to send" << std::endl;
177    return false;
178  }
179
180  void Server::processAck( ack *data, int clientID) {
181    COUT(5) << "processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;
182    clients->findClient(clientID)->setGamestateID(data->a);
183  }
184
185}
Note: See TracBrowser for help on using the repository browser.