Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1494 was 1494, checked in by rgrieder, 16 years ago
  • set the svn:eol-style property to all files so, that where ever you check out, you'll get the right line endings (had to change every file with mixed endings to windows in order to set the property)
  • Property svn:eol-style set to native
File size: 6.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    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
144 
145
146  /**
147  * submits a chat message to the server
148  * @param message message to send
149  * @return true/false
150  */
151  bool Client::sendChat( std::string message ){
152    // generate packet and add it to queue
153    if(!isConnected)
154      return false;
155    if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() )))
156      return client_connection.sendPackets();
157    // send packets
158    return false;
159  }
160
161
162  /**
163  * Sends out all the packets queued by addXXX
164  */
165  bool Client::sendPackets(){
166    if(!isConnected)
167      return false;
168    // send packets
169    client_connection.sendPackets();
170    return true;
171  }
172
173  /**
174  * Performs a GameState update
175  */
176  void Client::tick(float time){
177//     COUT(3) << ".";
178    if(client_connection.isConnected() && isSynched_){
179      COUT(4) << "popping partial gamestate: " << std::endl;
180      GameStateCompressed *gs = gamestate.popPartialGameState();
181      if(gs){
182        COUT(4) << "client tick: sending gs " << gs << std::endl;
183        ENetPacket *packet = pck_gen.gstate(gs);
184        if( packet == NULL || !client_connection.addPacket(packet))
185          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
186        // now delete it to save memory
187        delete[] gs->data;
188        delete gs;
189      }
190    }
191    ENetEvent *event;
192    // stop if the packet queue is empty
193    while(!(client_connection.queueEmpty())){
194      event = client_connection.getEvent();
195      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
196      elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
197    }
198    int gameStateID = gamestate.processGameState();
199    if(gameStateID==GAMESTATEID_INITIAL)
200      if(gameStateFailure_){
201        if(!client_connection.addPacket(pck_gen.acknowledgement(GAMESTATEID_INITIAL)))
202          COUT(3) << "could not (negatively) ack gamestate" << std::endl;
203        else 
204          COUT(4) << "negatively acked a gamestate" << std::endl;
205        }
206      else
207        gameStateFailure_=true;
208    else if(gameStateID!=0){
209      // ack gamestate and set synched
210      if(!isSynched_)
211        isSynched_=true;
212      gameStateFailure_=false;
213      if(!client_connection.addPacket(pck_gen.acknowledgement(gameStateID)))
214        COUT(3) << "could not ack gamestate" << std::endl;
215    }// otherwise we had no gamestate to load
216    gamestate.cleanup();
217    if(!client_connection.sendPackets())
218      COUT(3) << "Problem sending packets to server" << std::endl;
219    return;
220  }
221
222  void Client::processGamestate( GameStateCompressed *data, int clientID){
223    COUT(5) << "received gamestate id: " << data->id << std::endl;
224    gamestate.addGameState(data);
225  }
226
227  void Client::processClassid(classid *clid){
228    orxonox::Identifier *id;
229    id=ID(std::string(clid->message));
230    if(id!=NULL)
231      id->setNetworkID(clid->clid);
232    COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
233    COUT(4) << "id(classid)->getName " << ID((unsigned int)clid->clid)->getName() << std::endl;
234    delete clid;
235    return;
236  }
237
238  void Client::processChat( chat *data){
239    COUT(0) << "Server: " << data->message << std::endl;
240    delete[] data->message;
241    delete data;
242  }
243 
244  bool Client::processWelcome( welcome *w ){
245    COUT(4) << "processing welcome message" << std::endl;
246    clientID_ = w->clientID;
247    shipID_ = w->shipID;
248    delete w;
249    return true;
250  }
251
252}
Note: See TracBrowser for help on using the repository browser.