Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1518 was 1518, checked in by scheusso, 16 years ago

we are able to chat now ;) just use the command char <string>

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