Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some fixes in client disconnect procedure — mostly because of multithreading

File size: 8.4 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
46#include "ConnectionManager.h"
47#include "PacketTypes.h"
48#include "GameStateManager.h"
49#include "ClientInformation.h"
50//#include "NetworkFrameListener.h"
51#include "util/Sleep.h"
52
53
54namespace network
55{
56 
57 
58#define MAX_FAILURES 20;
59 
60 
61  /**
62  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
63  *
64  */
65  Server::Server() {
66    packet_gen = PacketGenerator();
67    clients = new ClientInformation(true);
68    connection = new ConnectionManager(clients);
69    gamestates = new GameStateManager(clients);
70  }
71 
72  Server::Server(int port){
73    packet_gen = PacketGenerator();
74    clients = new ClientInformation(true);
75    connection = new ConnectionManager(clients, port);
76    gamestates = new GameStateManager(clients);
77  }
78
79  /**
80  * Constructor
81  * @param port Port to listen on
82  * @param bindAddress Address to listen on
83  */
84  Server::Server(int port, std::string bindAddress) {
85    packet_gen = PacketGenerator();
86    clients = new ClientInformation();
87    connection = new ConnectionManager(port, bindAddress, clients);
88    gamestates = new GameStateManager(clients);
89  }
90
91  /**
92  * Constructor
93  * @param port Port to listen on
94  * @param bindAddress Address to listen on
95  */
96  Server::Server(int port, const char *bindAddress) {
97    packet_gen = PacketGenerator();
98    clients = new ClientInformation();
99    connection = new ConnectionManager(port, bindAddress, clients);
100    gamestates = new GameStateManager(clients);
101  }
102
103  /**
104  * This function opens the server by creating the listener thread
105  */
106  void Server::open() {
107    connection->createListener();
108    return;
109  }
110
111  /**
112  * This function closes the server
113  */
114  void Server::close() {
115    connection->quitListener();
116    return;
117  }
118
119  /**
120  * This function sends out a message to all clients
121  * @param msg message
122  * @return true/false
123  */
124  bool Server::sendMSG(std::string msg) {
125    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
126    //std::cout <<"adding packets" << std::endl;
127    if(connection->addPacketAll(packet))
128    //std::cout <<"added packets" << std::endl;
129      return connection->sendPackets();
130    else
131      return false;
132  }
133
134  /**
135  * This function sends out a message to all clients
136  * @param msg message
137  * @return true/false
138  */
139  bool Server::sendMSG(const char *msg) {
140    ENetPacket *packet = packet_gen.chatMessage(msg);
141    COUT(4) <<"Server: adding Packets" << std::endl;
142    connection->addPacketAll(packet);
143    //std::cout <<"added packets" << std::endl;
144    if (connection->sendPackets()){
145      COUT(4) << "Server: Sucessfully" << std::endl;
146      return true;
147    }
148    return false;
149  }
150
151  /**
152  * Run this function once every tick
153  * calls processQueue and updateGamestate
154  * @param time time since last tick
155  */
156  void Server::tick(float time) {
157    processQueue();
158    updateGamestate();
159//     usleep(500000); // TODO remove
160    return;
161  }
162
163  /**
164  * processes all the packets waiting in the queue
165  */
166  void Server::processQueue() {
167    ENetPacket *packet;
168    int clientID=-1;
169    while(!connection->queueEmpty()){
170      //std::cout << "Client " << clientID << " sent: " << std::endl;
171      //clientID here is a reference to grab clientID from ClientInformation
172      packet = connection->getPacket(clientID);
173      if(!packet)
174        continue;
175      //if statement to catch case that packetbuffer is empty
176      if( !elaborate(packet, clientID) ) 
177        COUT(3) << "Server: could not elaborate" << std::endl;
178    }
179  }
180
181  /**
182  * takes a new snapshot of the gamestate and sends it to the clients
183  */
184  void Server::updateGamestate() {
185    gamestates->update();
186    COUT(4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
187    //std::cout << "updated gamestate, sending it" << std::endl;
188    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
189    sendGameState();
190    COUT(4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
191    //std::cout << "sent gamestate" << std::endl;
192  }
193
194  /**
195  * sends the gamestate
196  */
197  bool Server::sendGameState() {
198    COUT(5) << "Server: starting function sendGameState" << std::endl;
199    boost::recursive_mutex::scoped_lock lock(clients->mutex_);
200    ClientInformation *temp = clients;
201    bool added=false;
202    while(temp != NULL){
203      if(temp->getHead()){
204        temp=temp->next();
205        //think this works without continue
206        continue;
207      }
208      if( !(temp->getSynched()) ){
209        COUT(5) << "Server: not sending gamestate" << std::endl;
210        temp=temp->next();
211        //think this works without continue
212        continue;
213      }
214      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
215      int gid = temp->getGamestateID(); //get gamestate id
216      int cid = temp->getID(); //get client id
217      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
218      GameStateCompressed *gs = gamestates->popGameState(cid);
219      if(gs==NULL){
220        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
221        return false;
222      }
223      //std::cout << "adding gamestate" << std::endl;
224      if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){
225        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 
226        temp->addFailure();
227        if(temp->getFailures() > 20 )
228          disconnectClient(temp);
229      //std::cout << "added gamestate" << std::endl;
230      }
231      added=true;
232      temp=temp->next();
233      // now delete gamestate
234      delete[] gs->data;
235      delete gs;
236    }
237    if(added) {
238      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
239      return connection->sendPackets();
240    }
241    COUT(5) << "Server: had no gamestates to send" << std::endl;
242    return false;
243  }
244
245  void Server::processAck( ack *data, int clientID) {
246    COUT(4) << "\b\b\b\n\n\n\n\nServer: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
247    gamestates->ackGameState(clientID, data->a);
248    delete data;
249  }
250 
251  bool Server::processConnectRequest( connectRequest *con, int clientID ){
252    COUT(3) << "processing connectRequest " << std::endl;
253    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
254    connection->createClient(clientID);
255    delete con;
256    return true;
257  }
258 
259  void Server::processGamestate( GameStateCompressed *data, int clientID){
260    COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
261    if(!gamestates->pushGameState(data, clientID))
262        COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
263    else
264      if(clients->findClient(clientID))
265        clients->findClient(clientID)->resetFailures();
266  }
267
268  void Server::disconnectClient(int clientID){
269    ClientInformation *client = clients->findClient(clientID);
270    if(client)
271      disconnectClient(client);
272  }
273  void Server::disconnectClient( ClientInformation *client){
274    connection->disconnectClient(client);
275    gamestates->removeClient(client);
276  }
277 
278}
Note: See TracBrowser for help on using the repository browser.