Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/Client.cc @ 1373

Last change on this file since 1373 was 1360, checked in by rgrieder, 16 years ago

merged merge branch back to trunk

File size: 8.0 KB
RevLine 
[1168]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 */
[1184]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"
[1232]42#include "Synchronisable.h"
[1184]43#include "core/CoreIncludes.h"
44
45namespace network
46{
47  Client* Client::_sClient = 0;
48 
49  Client* Client::createSingleton(){
[1232]50    if(!_sClient){
[1184]51      _sClient = new Client();
[1232]52    }
[1184]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;
[1248]86    isSynched_=false;
[1184]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;
[1248]96    isSynched_=false;
[1184]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;
[1248]106    isSynched_=false;
[1184]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(){
[1232]119    Synchronisable::setClient(true);
[1184]120    isConnected=client_connection.createConnection();
[1233]121    if(isConnected){
[1360]122      COUT(3) << "sending connectrequest" << std::endl;
123      if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
124        COUT(1) << "could not create connection" << std::endl;
[1232]125    }else
[1233]126      COUT(1) << "could not create connection" << std::endl;
[1184]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    ENetEvent event;
219    // send packets
220    client_connection.sendPackets(&event);
221    if(event.type==ENET_EVENT_TYPE_NONE)
222      return true;
223    else
224      return false;
225  }
226
227  /**
228  * Performs a GameState update
229  */
230  void Client::tick(float time){
[1360]231//     COUT(3) << ".";
[1248]232    if(client_connection.isConnected() && isSynched_){
[1245]233      COUT(4) << "popping partial gamestate: " << std::endl;
234      GameStateCompressed *gs = gamestate.popPartialGameState();
235      if(gs){
236        COUT(4) << "client tick: sending gs " << gs << std::endl;
[1284]237        ENetPacket *packet = pck_gen.gstate(gs);
238        if( packet == NULL || !client_connection.addPacket(packet))
[1246]239          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
[1245]240        // now delete it to save memory
[1251]241        delete[] gs->data;
[1245]242        delete gs;
243      }
244    }
[1184]245    ENetPacket *packet;
246    // stop if the packet queue is empty
247    while(!(client_connection.queueEmpty())){
248      packet = client_connection.getPacket();
249      COUT(5) << "tick packet size " << packet->dataLength << std::endl;
[1248]250      elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
[1184]251    }
[1246]252    if(!client_connection.sendPackets())
253      COUT(3) << "Problem sending packets to server" << std::endl;
[1184]254    return;
255  }
256
[1245]257  void Client::processGamestate( GameStateCompressed *data, int clientID){
[1248]258    int id = data->id;
259    COUT(5) << "received gamestate id: " << data->id << std::endl;
260    if(gamestate.pushGameState(data)){
261      if(!isSynched_)
262        isSynched_=true;
[1360]263      if(!client_connection.addPacket(pck_gen.acknowledgement(id)))
264        return;
[1246]265        // we do this at the end of a tick
[1360]266      if(!client_connection.sendPackets())
267        COUT(2) << "Could not send acknowledgment" << std::endl;
[1199]268    }
[1184]269  }
270
271  void Client::processClassid(classid *clid){
272    orxonox::Identifier *id;
273    id=ID(std::string(clid->message));
274    if(id!=NULL)
275      id->setNetworkID(clid->clid);
276    COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
[1248]277    delete clid;
[1184]278    return;
279  }
280
281  void Client::processChat( chat *data){
282    COUT(0) << "Server: " << data->message << std::endl;
[1248]283    delete[] data->message;
284    delete data;
[1184]285  }
[1232]286 
287  bool Client::processWelcome( welcome *w ){
288    COUT(4) << "processing welcome message" << std::endl;
289    clientID_ = w->clientID;
290    shipID_ = w->shipID;
[1248]291    delete w;
[1245]292    return true;
[1232]293  }
[1184]294
295}
Note: See TracBrowser for help on using the repository browser.