Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/network/Server.cc @ 1299

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

a huge fix in packetbuffer

File size: 8.1 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss, (C) 2007
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29//
30// C++ Implementation: Server
31//
32// Description:
33//
34//
35// Author:  Oliver Scheuss, (C) 2007
36//
37// Copyright: See COPYING file that comes with this distribution
38//
39//
40
41#include "Server.h"
42
43#include <iostream>
44
45#include "ConnectionManager.h"
46#include "PacketTypes.h"
47#include "GameStateManager.h"
48#include "ClientInformation.h"
49//#include "NetworkFrameListener.h"
50#include "util/Sleep.h"
51
52
53namespace network
54{
55 
56 
57#define MAX_FAILURES 20;
58 
59 
60  /**
61  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
62  *
63  */
64  Server::Server() {
65    packet_gen = PacketGenerator();
66    clients = new ClientInformation(true);
67    connection = new ConnectionManager(clients);
68    gamestates = new GameStateManager(clients);
69  }
70
71  /**
72  * Constructor
73  * @param port Port to listen on
74  * @param bindAddress Address to listen on
75  */
76  Server::Server(int port, std::string bindAddress) {
77    packet_gen = PacketGenerator();
78    clients = new ClientInformation();
79    connection = new ConnectionManager(port, bindAddress, clients);
80    gamestates = new GameStateManager(clients);
81  }
82
83  /**
84  * Constructor
85  * @param port Port to listen on
86  * @param bindAddress Address to listen on
87  */
88  Server::Server(int port, const char *bindAddress) {
89    packet_gen = PacketGenerator();
90    clients = new ClientInformation();
91    connection = new ConnectionManager(port, bindAddress, clients);
92    gamestates = new GameStateManager(clients);
93  }
94
95  /**
96  * This function opens the server by creating the listener thread
97  */
98  void Server::open() {
99    connection->createListener();
100    return;
101  }
102
103  /**
104  * This function closes the server
105  */
106  void Server::close() {
107    connection->quitListener();
108    return;
109  }
110
111  /**
112  * This function sends out a message to all clients
113  * @param msg message
114  * @return true/false
115  */
116  bool Server::sendMSG(std::string msg) {
117    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
118    //std::cout <<"adding packets" << std::endl;
119    if(connection->addPacketAll(packet))
120    //std::cout <<"added packets" << std::endl;
121      return connection->sendPackets();
122    else
123      return false;
124  }
125
126  /**
127  * This function sends out a message to all clients
128  * @param msg message
129  * @return true/false
130  */
131  bool Server::sendMSG(const char *msg) {
132    ENetPacket *packet = packet_gen.chatMessage(msg);
133    COUT(4) <<"Server: adding Packets" << std::endl;
134    connection->addPacketAll(packet);
135    //std::cout <<"added packets" << std::endl;
136    if (connection->sendPackets()){
137      COUT(4) << "Server: Sucessfully" << std::endl;
138      return true;
139    }
140    return false;
141  }
142
143  /**
144  * Run this function once every tick
145  * calls processQueue and updateGamestate
146  * @param time time since last tick
147  */
148  void Server::tick(float time) {
149    processQueue();
150    updateGamestate();
151
152//     usleep(500000); // TODO remove
153    return;
154  }
155
156  /**
157  * processes all the packets waiting in the queue
158  */
159  void Server::processQueue() {
160    ENetPacket *packet;
161    int clientID=-1;
162    while(!connection->queueEmpty()){
163      //std::cout << "Client " << clientID << " sent: " << std::endl;
164      //clientID here is a reference to grab clientID from ClientInformation
165      packet = connection->getPacket(clientID);
166      if(!packet)
167        continue;
168      //if statement to catch case that packetbuffer is empty
169      if( !elaborate(packet, clientID) ) 
170        COUT(3) << "Server: could not elaborate" << std::endl;
171    }
172  }
173
174  /**
175  * takes a new snapshot of the gamestate and sends it to the clients
176  */
177  void Server::updateGamestate() {
178    gamestates->update();
179    COUT(4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
180    //std::cout << "updated gamestate, sending it" << std::endl;
181    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
182    sendGameState();
183    COUT(4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
184    //std::cout << "sent gamestate" << std::endl;
185  }
186
187  /**
188  * sends the gamestate
189  */
190  bool Server::sendGameState() {
191    COUT(5) << "Server: starting function sendGameState" << std::endl;
192    ClientInformation *temp = clients;
193    bool added=false;
194    while(temp != NULL){
195      if(temp->head){
196        temp=temp->next();
197        //think this works without continue
198        continue;
199      }
200      if( !(temp->getSynched()) ){
201        COUT(5) << "Server: not sending gamestate" << std::endl;
202        temp=temp->next();
203        //think this works without continue
204        continue;
205      }
206      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
207      int gid = temp->getGamestateID(); //get gamestate id
208      int cid = temp->getID(); //get client id
209      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
210      GameStateCompressed *gs = gamestates->popGameState(cid);
211      if(gs==NULL){
212        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
213        return false;
214      }
215      //std::cout << "adding gamestate" << std::endl;
216      if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){
217        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->failures_ << std::endl; 
218        temp->failures_++;
219        if(temp->failures_ > 20 )
220          disconnectClient(temp);
221      //std::cout << "added gamestate" << std::endl;
222      }
223      added=true;
224      temp=temp->next();
225      // now delete gamestate
226      delete[] gs->data;
227      delete gs;
228    }
229    if(added) {
230      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
231      return connection->sendPackets();
232    }
233    COUT(5) << "Server: had no gamestates to send" << std::endl;
234    return false;
235  }
236
237  void Server::processAck( ack *data, int clientID) {
238    COUT(4) << "\b\b\b\n\n\n\n\nServer: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
239    gamestates->ackGameState(clientID, data->a);
240    delete data;
241  }
242 
243  bool Server::processConnectRequest( connectRequest *con, int clientID ){
244    COUT(3) << "processing connectRequest " << std::endl;
245    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
246    connection->createClient(clientID);
247    delete con;
248    return true;
249  }
250 
251  void Server::processGamestate( GameStateCompressed *data, int clientID){
252    COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
253    if(!gamestates->pushGameState(data, clientID))
254        COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
255    else
256      if(clients->findClient(clientID))
257        clients->findClient(clientID)->failures_=0;
258  }
259
260  void Server::disconnectClient(int clientID){
261    ClientInformation *client = clients->findClient(clientID);
262    if(client)
263      disconnectClient(client);
264  }
265  void Server::disconnectClient( ClientInformation *client){
266    connection->disconnectClient(client);
267    gamestates->removeClient(client);
268  }
269 
270}
Note: See TracBrowser for help on using the repository browser.