Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/shader/src/network/Client.cc @ 10227

Last change on this file since 10227 was 1505, checked in by rgrieder, 16 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

  • Property svn:eol-style set to native
File size: 6.7 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    return client_connection.addPacket(pck_gen.chatMessage( message.c_str() ));
156      //return client_connection.sendPackets();
157    // send packets
158    return false;
159  }
160
161  /**
162  * Performs a GameState update
163  */
164  void Client::tick(float time){
165//     COUT(3) << ".";
166    if(client_connection.isConnected() && isSynched_){
167      COUT(4) << "popping partial gamestate: " << std::endl;
168      GameStateCompressed *gs = gamestate.popPartialGameState();
169      if(gs){
170        COUT(4) << "client tick: sending gs " << gs << std::endl;
171        ENetPacket *packet = pck_gen.gstate(gs);
172        if( packet == NULL || !client_connection.addPacket(packet))
173          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
174        // now delete it to save memory
175        delete[] gs->data;
176        delete gs;
177      }
178    }
179    ENetEvent *event;
180    // stop if the packet queue is empty
181    while(!(client_connection.queueEmpty())){
182      event = client_connection.getEvent();
183      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
184      elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
185    }
186    int gameStateID = gamestate.processGameState();
187    if(gameStateID==GAMESTATEID_INITIAL)
188      if(gameStateFailure_){
189        if(!client_connection.addPacket(pck_gen.acknowledgement(GAMESTATEID_INITIAL)))
190          COUT(3) << "could not (negatively) ack gamestate" << std::endl;
191        else 
192          COUT(4) << "negatively acked a gamestate" << std::endl;
193        }
194      else
195        gameStateFailure_=true;
196    else if(gameStateID!=0){
197      // ack gamestate and set synched
198      if(!isSynched_)
199        isSynched_=true;
200      gameStateFailure_=false;
201      if(!client_connection.addPacket(pck_gen.acknowledgement(gameStateID)))
202        COUT(3) << "could not ack gamestate" << std::endl;
203    }// otherwise we had no gamestate to load
204    gamestate.cleanup();
205    /*if(!client_connection.sendPackets())
206      COUT(3) << "Problem sending packets to server" << std::endl;*/
207    return;
208  }
209
210  void Client::processGamestate( GameStateCompressed *data, int clientID){
211    COUT(5) << "received gamestate id: " << data->id << std::endl;
212    gamestate.addGameState(data);
213  }
214
215  void Client::processClassid(classid *clid){
216    orxonox::Identifier *id;
217    id=ID(std::string(clid->message));
218    if(id!=NULL)
219      id->setNetworkID(clid->clid);
220    COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
221    COUT(4) << "id(classid)->getName " << ID((unsigned int)clid->clid)->getName() << std::endl;
222    delete clid;
223    return;
224  }
225
226  void Client::processChat( chat *data){
227    COUT(0) << "Server: " << data->message << std::endl;
228    delete[] data->message;
229    delete data;
230  }
231 
232  bool Client::processWelcome( welcome *w ){
233    COUT(4) << "processing welcome message" << std::endl;
234    clientID_ = w->clientID;
235    shipID_ = w->shipID;
236    delete w;
237    return true;
238  }
239
240}
Note: See TracBrowser for help on using the repository browser.