Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1653 was 1534, checked in by rgrieder, 16 years ago

merged network branch back to trunk

  • Property svn:eol-style set to native
File size: 7.1 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#include "core/ConsoleCommand.h"
45#include "Server.h"
46
47namespace network
48{
49  SetConsoleCommandShortcut(Client, Chat);
50 
51  Client* Client::_sClient = 0;
52 
53  Client* Client::createSingleton(){
54    if(!_sClient){
55      _sClient = new Client();
56    }
57    return _sClient;
58  }
59 
60  Client* Client::createSingleton(std::string address, int port){
61    if(!_sClient)
62      _sClient = new Client(address, port);
63    return _sClient;
64  }
65 
66  Client* Client::createSingleton(const char *address, int port){
67    if(!_sClient)
68      _sClient = new Client(address, port);
69    return _sClient;
70  }
71 
72  void Client::destroySingleton(){
73    if(_sClient){
74      delete _sClient;
75      _sClient = 0;
76    }
77  }
78 
79  Client* Client::getSingleton(){
80    return _sClient; 
81  }
82 
83  /**
84  * Constructor for the Client class
85  * initializes the address and the port to default localhost:NETWORK_PORT
86  */
87  Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){
88    // set server address to localhost
89    isConnected=false;
90    isSynched_=false;
91    gameStateFailure_=false;
92  }
93
94  /**
95  * Constructor for the Client class
96  * @param address the server address
97  * @param port port of the application on the server
98  */
99  Client::Client(std::string address, int port) : client_connection(port, address){
100    isConnected=false;
101    isSynched_=false;
102    gameStateFailure_=false;
103  }
104
105  /**
106  * Constructor for the Client class
107  * @param address the server address
108  * @param port port of the application on the server
109  */
110  Client::Client(const char *address, int port) : client_connection(port, address){
111    isConnected=false;
112    isSynched_=false;
113    gameStateFailure_=false;
114  }
115
116  Client::~Client(){
117    if(isConnected)
118      closeConnection();
119  }
120 
121  /**
122  * Establish the Connection to the Server
123  * @return true/false
124  */
125  bool Client::establishConnection(){
126    Synchronisable::setClient(true);
127    isConnected=client_connection.createConnection();
128    if(isConnected){
129//       COUT(3) << "sending connectrequest" << std::endl;
130//       if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
131//         COUT(1) << "could not send connection request !!!!!!!!!" << std::endl;
132    }else
133      COUT(1) << "could not create connection laber" << std::endl;
134    return isConnected;
135  }
136
137  /**
138  * closes the Connection to the Server
139  * @return true/false
140  */
141  bool Client::closeConnection(){
142    isConnected=false;
143    return client_connection.closeConnection();
144  }
145
146 
147
148  void Client::Chat( std::string message ){
149    if(Client::getSingleton())
150      Client::getSingleton()->sendChat(message);
151    else if(Server::getSingleton())
152      Server::getSingleton()->sendChat(message);
153    else
154      COUT(1) << "do you want to monologize ??" << std::endl;
155  }
156 
157
158  /**
159  * submits a chat message to the server
160  * @param message message to send
161  * @return true/false
162  */
163  bool Client::sendChat( std::string message ){
164    // generate packet and add it to queue
165    if(!isConnected)
166      return false;
167    return client_connection.addPacket(pck_gen.chatMessage( message.c_str() ));
168      //return client_connection.sendPackets();
169    // send packets
170    return false;
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, int clientId){
239    COUT(1) << 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.