Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/Server.cc @ 1438

Last change on this file since 1438 was 1360, checked in by rgrieder, 17 years ago

merged merge branch back to trunk

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//     usleep(500000); // TODO remove
152    return;
153  }
154
155  /**
156  * processes all the packets waiting in the queue
157  */
158  void Server::processQueue() {
159    ENetPacket *packet;
160    int clientID=-1;
161    while(!connection->queueEmpty()){
162      //std::cout << "Client " << clientID << " sent: " << std::endl;
163      //clientID here is a reference to grab clientID from ClientInformation
164      packet = connection->getPacket(clientID);
165      if(!packet)
166        continue;
167      //if statement to catch case that packetbuffer is empty
168      if( !elaborate(packet, clientID) ) 
169        COUT(3) << "Server: could not elaborate" << std::endl;
170    }
171  }
172
173  /**
174  * takes a new snapshot of the gamestate and sends it to the clients
175  */
176  void Server::updateGamestate() {
177    gamestates->update();
178    COUT(4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
179    //std::cout << "updated gamestate, sending it" << std::endl;
180    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
181    sendGameState();
182    COUT(4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
183    //std::cout << "sent gamestate" << std::endl;
184  }
185
186  /**
187  * sends the gamestate
188  */
189  bool Server::sendGameState() {
190    COUT(5) << "Server: starting function sendGameState" << std::endl;
191    ClientInformation *temp = clients;
192    bool added=false;
193    while(temp != NULL){
194      if(temp->getHead()){
195        temp=temp->next();
196        //think this works without continue
197        continue;
198      }
199      if( !(temp->getSynched()) ){
200        COUT(5) << "Server: not sending gamestate" << std::endl;
201        temp=temp->next();
202        //think this works without continue
203        continue;
204      }
205      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
206      int gid = temp->getGamestateID(); //get gamestate id
207      int cid = temp->getID(); //get client id
208      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
209      GameStateCompressed *gs = gamestates->popGameState(cid);
210      if(gs==NULL){
211        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
212        return false;
213      }
214      //std::cout << "adding gamestate" << std::endl;
215      if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){
216        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 
217        temp->addFailure();
218        if(temp->getFailures() > 20 )
219          disconnectClient(temp);
220      //std::cout << "added gamestate" << std::endl;
221      }
222      added=true;
223      temp=temp->next();
224      // now delete gamestate
225      delete[] gs->data;
226      delete gs;
227    }
228    if(added) {
229      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
230      return connection->sendPackets();
231    }
232    COUT(5) << "Server: had no gamestates to send" << std::endl;
233    return false;
234  }
235
236  void Server::processAck( ack *data, int clientID) {
237    COUT(4) << "\b\b\b\n\n\n\n\nServer: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
238    gamestates->ackGameState(clientID, data->a);
239    delete data;
240  }
241 
242  bool Server::processConnectRequest( connectRequest *con, int clientID ){
243    COUT(3) << "processing connectRequest " << std::endl;
244    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
245    connection->createClient(clientID);
246    delete con;
247    return true;
248  }
249 
250  void Server::processGamestate( GameStateCompressed *data, int clientID){
251    COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
252    if(!gamestates->pushGameState(data, clientID))
253        COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
254    else
255      if(clients->findClient(clientID))
256        clients->findClient(clientID)->resetFailures();
257  }
258
259  void Server::disconnectClient(int clientID){
260    ClientInformation *client = clients->findClient(clientID);
261    if(client)
262      disconnectClient(client);
263  }
264  void Server::disconnectClient( ClientInformation *client){
265    connection->disconnectClient(client);
266    gamestates->removeClient(client);
267  }
268 
269}
Note: See TracBrowser for help on using the repository browser.