Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/network/Server.cc @ 1321

Last change on this file since 1321 was 1062, checked in by rgrieder, 17 years ago
  • changed header file inclusion order
File size: 5.7 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    std::cout <<"adding Packets" << std::endl;
127    connection->addPacketAll(packet);
128    //std::cout <<"added packets" << std::endl;
129    if (connection->sendPackets()){
130      std::cout << "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    sleep(1); // 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      packet = connection->getPacket(clientID);
158      elaborate(packet, clientID);
159    }
160  }
161
162  /**
163  * takes a new snapshot of the gamestate and sends it to the clients
164  */
165  void Server::updateGamestate() {
166    gamestates->update();
167    //std::cout << "updated gamestate, sending it" << std::endl;
168    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
169      sendGameState();
170    //std::cout << "sent gamestate" << std::endl;
171  }
172
173  /**
174  * sends the gamestate
175  */
176  bool Server::sendGameState() {
177    COUT(5) << "starting sendGameState" << std::endl;
178    ClientInformation *temp = clients;
179    bool added=false;
180    while(temp!=NULL){
181      if(temp->head){
182        temp=temp->next();
183        continue;
184      }
185      if( !(temp->getSynched()) ){
186        COUT(5) << "not sending gamestate" << std::endl;
187        temp=temp->next();
188        continue;
189      }
190      COUT(5) << "doing gamestate gamestate preparation" << std::endl;
191      int gid = temp->getGamestateID();
192      int cid = temp->getID();
193      COUT(5) << "server, got acked (gamestate) ID: " << gid << std::endl;
194      GameStateCompressed *gs = gamestates->popGameState(cid);
195      if(gs==NULL){
196        COUT(2) << "could not generate gamestate" << std::endl;
197        return false;
198      }
199      //std::cout << "adding gamestate" << std::endl;
200      connection->addPacket(packet_gen.gstate(gs), cid);
201      //std::cout << "added gamestate" << std::endl;
202      added=true;
203      temp=temp->next();
204    }
205    if(added)
206      return connection->sendPackets();
207    COUT(5) << "had no gamestates to send" << std::endl;
208    return false;
209  }
210
211  void Server::processAck( ack *data, int clientID) {
212    COUT(5) << "processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;
213    clients->findClient(clientID)->setGamestateID(data->a);
214  }
215
216}
Note: See TracBrowser for help on using the repository browser.