Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

a lot of changes in order to make it possible to have mulpiple clients with each one a new ship
camera changes
object changes
synchronisable: backsyncronisation should be possible now
gamestatemanager/gamestateclient: functions for backsyncronisation
some changes in order to get the input system (the old one) on the client working
TODO something with the camera position is wrong at the moment (clientside)

File size: 7.0 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(200000); // 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    }
213    if(added) {
214      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
215      return connection->sendPackets();
216    }
217    COUT(5) << "Server: had no gamestates to send" << std::endl;
218    return false;
219  }
220
221  void Server::processAck( ack *data, int clientID) {
222    COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
223    gamestates->ackGameState(clientID, data->a);
224  }
225 
226  bool Server::processConnectRequest( connectRequest *con, int clientID ){
227    COUT(4) << "processing connectRequest " << std::endl;
228    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
229    connection->createClient(clientID);
230  }
231
232}
Note: See TracBrowser for help on using the repository browser.