Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/network/Client.cc @ 1282

Last change on this file since 1282 was 1264, checked in by scheusso, 17 years ago

merge network3 and camera branch into merge branch

File size: 7.9 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){
122      COUT(4) << "sending connectrequest" << std::endl;
[1232]123      client_connection.addPacket(pck_gen.generateConnectRequest());
124      client_connection.sendPackets();
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){
[1248]231    if(client_connection.isConnected() && isSynched_){
[1245]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        if(client_connection.addPacket(pck_gen.gstate(gs)))
[1246]237          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
[1245]238        // now delete it to save memory
[1251]239        delete[] gs->data;
[1245]240        delete gs;
241      }
242    }
[1184]243    ENetPacket *packet;
244    // stop if the packet queue is empty
245    while(!(client_connection.queueEmpty())){
246      packet = client_connection.getPacket();
247      COUT(5) << "tick packet size " << packet->dataLength << std::endl;
[1248]248      elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
[1184]249    }
[1246]250    if(!client_connection.sendPackets())
251      COUT(3) << "Problem sending packets to server" << std::endl;
[1184]252    return;
253  }
254
[1245]255  void Client::processGamestate( GameStateCompressed *data, int clientID){
[1248]256    int id = data->id;
257    COUT(5) << "received gamestate id: " << data->id << std::endl;
258    if(gamestate.pushGameState(data)){
259      if(!isSynched_)
260        isSynched_=true;
261      client_connection.addPacket(pck_gen.acknowledgement(id));
[1246]262        // we do this at the end of a tick
[1248]263       if(!client_connection.sendPackets())
264         COUT(2) << "Could not send acknowledgment" << std::endl;
[1199]265    }
[1184]266  }
267
268  void Client::processClassid(classid *clid){
269    orxonox::Identifier *id;
270    id=ID(std::string(clid->message));
271    if(id!=NULL)
272      id->setNetworkID(clid->clid);
273    COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
[1248]274    delete clid;
[1184]275    return;
276  }
277
278  void Client::processChat( chat *data){
279    COUT(0) << "Server: " << data->message << std::endl;
[1248]280    delete[] data->message;
281    delete data;
[1184]282  }
[1232]283 
284  bool Client::processWelcome( welcome *w ){
285    COUT(4) << "processing welcome message" << std::endl;
286    clientID_ = w->clientID;
287    shipID_ = w->shipID;
[1248]288    delete w;
[1245]289    return true;
[1232]290  }
[1184]291
292}
Note: See TracBrowser for help on using the repository browser.