Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1017 was 1008, checked in by dumenim, 16 years ago

changed some comments and catched some return values and maybe some stuff we have to unchange

File size: 5.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 "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    COUT(4) <<"Server: adding Packets" << std::endl;
98    connection->addPacketAll(packet);
99    //std::cout <<"added packets" << std::endl;
100    if (connection->sendPackets()){
101      COUT(4) << "Server: 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      //clientID here is a reference to grab clientID from ClientInformation
129      packet = connection->getPacket(clientID);
130      //if statement to catch case that packetbuffer is empty
131      if( !elaborate(packet, clientID) ) 
132        COUT(4) << "Server: PacketBuffer empty" << std::endl;
133    }
134  }
135
136  /**
137  * takes a new snapshot of the gamestate and sends it to the clients
138  */
139  void Server::updateGamestate() {
140    gamestates->update();
141    COUT(4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
142    //std::cout << "updated gamestate, sending it" << std::endl;
143    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
144    sendGameState();
145    COUT(4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
146    //std::cout << "sent gamestate" << std::endl;
147  }
148
149  /**
150  * sends the gamestate
151  */
152  bool Server::sendGameState() {
153    COUT(5) << "Server: starting function sendGameState" << std::endl;
154    ClientInformation *temp = clients;
155    bool added=false;
156    while(temp != NULL){
157      if(temp->head){
158        temp=temp->next();
159        //think this works without continue
160        continue;
161      }
162      if( !(temp->getSynched()) ){
163        COUT(5) << "Server: not sending gamestate" << std::endl;
164        temp=temp->next();
165        //think this works without continue
166        continue;
167      }
168      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
169      int gid = temp->getGamestateID(); //get gamestate id
170      int cid = temp->getID(); //get client id
171      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
172      GameStateCompressed *gs = gamestates->popGameState(cid);
173      if(gs==NULL){
174        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
175        return false;
176      }
177      //std::cout << "adding gamestate" << std::endl;
178      if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) )
179        COUT(4) << "Server: packet with client id (cid): " << cid << " not sended" << std::endl; 
180      //std::cout << "added gamestate" << std::endl;
181      added=true;
182      temp=temp->next();
183    }
184    if(added) {
185      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
186      return connection->sendPackets();
187    }
188    COUT(5) << "Server: had no gamestates to send" << std::endl;
189    return false;
190  }
191
192  void Server::processAck( ack *data, int clientID) {
193    COUT(5) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;
194    clients->findClient(clientID)->setGamestateID(data->a);
195  }
196
197}
Note: See TracBrowser for help on using the repository browser.