Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

implemented some sort of buffer for the spaceship movements (makes the movements on the client smoother)

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