Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/Client.cc @ 1432

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

another one bites the dust: solved that problem with zeros in the gamestate

File size: 8.5 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: Client
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 "Client.h"
42#include "Synchronisable.h"
43#include "core/CoreIncludes.h"
44
45namespace network
46{
47  Client* Client::_sClient = 0;
48 
49  Client* Client::createSingleton(){
50    if(!_sClient){
51      _sClient = new Client();
52    }
53    return _sClient;
54  }
55 
56  Client* Client::createSingleton(std::string address, int port){
57    if(!_sClient)
58      _sClient = new Client(address, port);
59    return _sClient;
60  }
61 
62  Client* Client::createSingleton(const char *address, int port){
63    if(!_sClient)
64      _sClient = new Client(address, port);
65    return _sClient;
66  }
67 
68  void Client::destroySingleton(){
69    if(_sClient){
70      delete _sClient;
71      _sClient = 0;
72    }
73  }
74 
75  Client* Client::getSingleton(){
76    return _sClient; 
77  }
78 
79  /**
80  * Constructor for the Client class
81  * initializes the address and the port to default localhost:NETWORK_PORT
82  */
83  Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){
84    // set server address to localhost
85    isConnected=false;
86    isSynched_=false;
87    gameStateFailure_=false;
88  }
89
90  /**
91  * Constructor for the Client class
92  * @param address the server address
93  * @param port port of the application on the server
94  */
95  Client::Client(std::string address, int port) : client_connection(port, address){
96    isConnected=false;
97    isSynched_=false;
98    gameStateFailure_=false;
99  }
100
101  /**
102  * Constructor for the Client class
103  * @param address the server address
104  * @param port port of the application on the server
105  */
106  Client::Client(const char *address, int port) : client_connection(port, address){
107    isConnected=false;
108    isSynched_=false;
109    gameStateFailure_=false;
110  }
111
112  Client::~Client(){
113    if(isConnected)
114      closeConnection();
115  }
116 
117  /**
118  * Establish the Connection to the Server
119  * @return true/false
120  */
121  bool Client::establishConnection(){
122    Synchronisable::setClient(true);
123    isConnected=client_connection.createConnection();
124    if(isConnected){
125//       COUT(3) << "sending connectrequest" << std::endl;
126//       if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
127//         COUT(1) << "could not send connection request !!!!!!!!!" << std::endl;
128    }else
129      COUT(1) << "could not create connection laber" << std::endl;
130    return isConnected;
131  }
132
133  /**
134  * closes the Connection to the Server
135  * @return true/false
136  */
137  bool Client::closeConnection(){
138    isConnected=false;
139    return client_connection.closeConnection();
140  }
141
142  /**
143  * submits a MouseAction to the server
144  * @param x x Coordinate
145  * @param y y Coordinate
146  * @return true/false
147  */
148  bool Client::sendMouse(double x, double y){
149    // generate packet and add it to the queue
150    if(!isConnected)
151      return false;
152    if(!client_connection.addPacket(pck_gen.mousem(x, y)))
153      return false;
154    // send packets
155    return client_connection.sendPackets();
156  }
157
158  /**
159  * submits a Keystrike to the server
160  * @param key_code code to submit
161  * @return true/false
162  */
163  bool Client::sendKeyboard(char key_code){
164    // generate packet and add it to queue
165    if(!isConnected)
166      return false;
167    if(!client_connection.addPacket(pck_gen.keystrike(key_code)))
168      return false;
169    // send packets
170    return client_connection.sendPackets();
171  }
172
173  /**
174  * submits a chat message to the server
175  * @param message message to send
176  * @return true/false
177  */
178  bool Client::sendChat( std::string message ){
179    // generate packet and add it to queue
180    if(!isConnected)
181      return false;
182    if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() )))
183      return client_connection.sendPackets();
184    // send packets
185    return false;
186  }
187
188  /**
189  * Adds a MouseAction to the PacketQueue
190  * @param x x Coordinate
191  * @param y y Coordinate
192  * @return true/false
193  */
194  bool Client::addMouse(double x, double y){
195    // generate packet and add it to the queue
196    if(client_connection.addPacket(pck_gen.mousem(x, y)))
197      return true;
198    else
199      return false;
200  }
201
202  /**
203  * Adds a Keystrike to the PacketQueue
204  * @param key_code
205  * @return true/false
206  */
207  bool Client::addKeyboard(char key_code){
208    // generate packet and add it to queue
209    if(client_connection.addPacket(pck_gen.keystrike(key_code)))
210      return true;
211    else
212      return false;
213  }
214
215  /**
216  * Sends out all the packets queued by addXXX
217  */
218  bool Client::sendPackets(){
219    if(!isConnected)
220      return false;
221    // send packets
222    client_connection.sendPackets();
223    return true;
224  }
225
226  /**
227  * Performs a GameState update
228  */
229  void Client::tick(float time){
230//     COUT(3) << ".";
231    if(client_connection.isConnected() && isSynched_){
232      COUT(4) << "popping partial gamestate: " << std::endl;
233      GameStateCompressed *gs = gamestate.popPartialGameState();
234      if(gs){
235        COUT(4) << "client tick: sending gs " << gs << std::endl;
236        ENetPacket *packet = pck_gen.gstate(gs);
237        if( packet == NULL || !client_connection.addPacket(packet))
238          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
239        // now delete it to save memory
240        delete[] gs->data;
241        delete gs;
242      }
243    }
244    ENetEvent *event;
245    // stop if the packet queue is empty
246    while(!(client_connection.queueEmpty())){
247      event = client_connection.getEvent();
248      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
249      elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
250    }
251    int gameStateID = gamestate.processGameState();
252    if(gameStateID==GAMESTATEID_INITIAL)
253      if(gameStateFailure_){
254        if(!client_connection.addPacket(pck_gen.acknowledgement(GAMESTATEID_INITIAL)))
255          COUT(3) << "could not (negatively) ack gamestate" << std::endl;
256        else 
257          COUT(4) << "negatively acked a gamestate" << std::endl;
258        }
259      else
260        gameStateFailure_=true;
261    else if(gameStateID!=0){
262      // ack gamestate and set synched
263      if(!isSynched_)
264        isSynched_=true;
265      gameStateFailure_=false;
266      if(!client_connection.addPacket(pck_gen.acknowledgement(gameStateID)))
267        COUT(3) << "could not ack gamestate" << std::endl;
268    }// otherwise we had no gamestate to load
269    gamestate.cleanup();
270    if(!client_connection.sendPackets())
271      COUT(3) << "Problem sending packets to server" << std::endl;
272    return;
273  }
274
275  void Client::processGamestate( GameStateCompressed *data, int clientID){
276    COUT(5) << "received gamestate id: " << data->id << std::endl;
277    gamestate.addGameState(data);
278  }
279
280  void Client::processClassid(classid *clid){
281    orxonox::Identifier *id;
282    id=ID(std::string(clid->message));
283    if(id!=NULL)
284      id->setNetworkID(clid->clid);
285    COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
286    delete clid;
287    return;
288  }
289
290  void Client::processChat( chat *data){
291    COUT(0) << "Server: " << data->message << std::endl;
292    delete[] data->message;
293    delete data;
294  }
295 
296  bool Client::processWelcome( welcome *w ){
297    COUT(4) << "processing welcome message" << std::endl;
298    clientID_ = w->clientID;
299    shipID_ = w->shipID;
300    delete w;
301    return true;
302  }
303
304}
Note: See TracBrowser for help on using the repository browser.