Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/script_trigger/src/network/Client.cc @ 2004

Last change on this file since 2004 was 1293, checked in by scheusso, 17 years ago

merged changes from input & camera & network branch into trunk

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