Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network3/src/network/Server.cc @ 1253

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

fixes over fixes

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