Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1056 was 1056, checked in by landauf, 16 years ago

don't panic, no codechanges!
added a link to www.orxonox.net

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 <iostream>
42
43#include "ConnectionManager.h"
44#include "PacketTypes.h"
45#include "GameStateManager.h"
46#include "ClientInformation.h"
47//#include "NetworkFrameListener.h"
48#include "util/Sleep.h"
49#include "Server.h"
50
51
52namespace network
53{
54  /**
55  * Constructor for default values (bindaddress is set to ENET_HOST_ANY
56  *
57  */
58  Server::Server() {
59    packet_gen = PacketGenerator();
60    clients = new ClientInformation(true);
61    connection = new ConnectionManager(clients);
62    gamestates = new GameStateManager(clients);
63  }
64
65  /**
66  * Constructor
67  * @param port Port to listen on
68  * @param bindAddress Address to listen on
69  */
70  Server::Server(int port, std::string bindAddress) {
71    packet_gen = PacketGenerator();
72    clients = new ClientInformation();
73    connection = new ConnectionManager(port, bindAddress, clients);
74    gamestates = new GameStateManager(clients);
75  }
76
77  /**
78  * Constructor
79  * @param port Port to listen on
80  * @param bindAddress Address to listen on
81  */
82  Server::Server(int port, const char *bindAddress) {
83    packet_gen = PacketGenerator();
84    clients = new ClientInformation();
85    connection = new ConnectionManager(port, bindAddress, clients);
86    gamestates = new GameStateManager(clients);
87  }
88
89  /**
90  * This function opens the server by creating the listener thread
91  */
92  void Server::open() {
93    connection->createListener();
94    return;
95  }
96
97  /**
98  * This function closes the server
99  */
100  void Server::close() {
101    connection->quitListener();
102    return;
103  }
104
105  /**
106  * This function sends out a message to all clients
107  * @param msg message
108  * @return true/false
109  */
110  bool Server::sendMSG(std::string msg) {
111    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
112    //std::cout <<"adding packets" << std::endl;
113    connection->addPacketAll(packet);
114    //std::cout <<"added packets" << std::endl;
115    return connection->sendPackets();
116  }
117
118  /**
119  * This function sends out a message to all clients
120  * @param msg message
121  * @return true/false
122  */
123  bool Server::sendMSG(const char *msg) {
124    ENetPacket *packet = packet_gen.chatMessage(msg);
125    std::cout <<"adding Packets" << std::endl;
126    connection->addPacketAll(packet);
127    //std::cout <<"added packets" << std::endl;
128    if (connection->sendPackets()){
129      std::cout << "Sucessfully" << std::endl;
130      return true;
131    }
132    return false;
133  }
134
135  /**
136  * Run this function once every tick
137  * calls processQueue and updateGamestate
138  * @param time time since last tick
139  */
140  void Server::tick(float time) {
141    processQueue();
142    updateGamestate();
143
144    sleep(1); // TODO remove
145    return;
146  }
147
148  /**
149  * processes all the packets waiting in the queue
150  */
151  void Server::processQueue() {
152    ENetPacket *packet;
153    int clientID=-1;
154    while(!connection->queueEmpty()){
155      //std::cout << "Client " << clientID << " sent: " << std::endl;
156      packet = connection->getPacket(clientID);
157      elaborate(packet, clientID);
158    }
159  }
160
161  /**
162  * takes a new snapshot of the gamestate and sends it to the clients
163  */
164  void Server::updateGamestate() {
165    gamestates->update();
166    //std::cout << "updated gamestate, sending it" << std::endl;
167    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
168      sendGameState();
169    //std::cout << "sent gamestate" << std::endl;
170  }
171
172  /**
173  * sends the gamestate
174  */
175  bool Server::sendGameState() {
176    COUT(5) << "starting sendGameState" << std::endl;
177    ClientInformation *temp = clients;
178    bool added=false;
179    while(temp!=NULL){
180      if(temp->head){
181        temp=temp->next();
182        continue;
183      }
184      if( !(temp->getSynched()) ){
185        COUT(5) << "not sending gamestate" << std::endl;
186        temp=temp->next();
187        continue;
188      }
189      COUT(5) << "doing gamestate gamestate preparation" << std::endl;
190      int gid = temp->getGamestateID();
191      int cid = temp->getID();
192      COUT(5) << "server, got acked (gamestate) ID: " << gid << std::endl;
193      GameStateCompressed *gs = gamestates->popGameState(cid);
194      if(gs==NULL){
195        COUT(2) << "could not generate gamestate" << std::endl;
196        return false;
197      }
198      //std::cout << "adding gamestate" << std::endl;
199      connection->addPacket(packet_gen.gstate(gs), cid);
200      //std::cout << "added gamestate" << std::endl;
201      added=true;
202      temp=temp->next();
203    }
204    if(added)
205      return connection->sendPackets();
206    COUT(5) << "had no gamestates to send" << std::endl;
207    return false;
208  }
209
210  void Server::processAck( ack *data, int clientID) {
211    COUT(5) << "processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;
212    clients->findClient(clientID)->setGamestateID(data->a);
213  }
214
215}
Note: See TracBrowser for help on using the repository browser.