Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1348 was 1293, checked in by scheusso, 17 years ago

merged changes from input & camera & network branch into trunk

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