Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

a hole lot of changes:

  • changes in structure concerning client/server (they inherit from Host now)
  • some small changes in spaceship
  • delete unused files
  • modified clientinformation (is a singleton now)
  • modified gamestatemanager (adopted clientinformation changes)
  • modified connectionmanager (adopted CI changes, is a singleton now)
  • Property svn:eol-style set to native
File size: 6.6 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 "Host.h"
43#include "Synchronisable.h"
44#include "core/CoreIncludes.h"
45#include "core/ConsoleCommand.h"
46#include "packet/Packet.h"
47
48namespace network
49{
50//   SetConsoleCommandShortcut(Client, chat);
51 
52 
53  /**
54  * Constructor for the Client class
55  * initializes the address and the port to default localhost:NETWORK_PORT
56  */
57  Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){
58    // set server address to localhost
59    isConnected=false;
60    isSynched_=false;
61    gameStateFailure_=false;
62  }
63
64  /**
65  * Constructor for the Client class
66  * @param address the server address
67  * @param port port of the application on the server
68  */
69  Client::Client(std::string address, int port) : client_connection(port, address){
70    isConnected=false;
71    isSynched_=false;
72    gameStateFailure_=false;
73  }
74
75  /**
76  * Constructor for the Client class
77  * @param address the server address
78  * @param port port of the application on the server
79  */
80  Client::Client(const char *address, int port) : client_connection(port, address){
81    isConnected=false;
82    isSynched_=false;
83    gameStateFailure_=false;
84  }
85
86  Client::~Client(){
87    if(isConnected)
88      closeConnection();
89  }
90 
91  /**
92  * Establish the Connection to the Server
93  * @return true/false
94  */
95  bool Client::establishConnection(){
96    Synchronisable::setClient(true);
97    isConnected=client_connection.createConnection();
98    if(isConnected){
99//       COUT(3) << "sending connectrequest" << std::endl;
100//       if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
101//         COUT(1) << "could not send connection request !!!!!!!!!" << std::endl;
102    }else
103      COUT(1) << "could not create connection laber" << std::endl;
104    return isConnected;
105  }
106
107  /**
108  * closes the Connection to the Server
109  * @return true/false
110  */
111  bool Client::closeConnection(){
112    isConnected=false;
113    return client_connection.closeConnection();
114  }
115
116  bool Client::queuePacket(ENetPacket *packet, int clientID){
117    return client_connection.addPacket(packet);
118  }
119 
120  bool Client::processChat(packet::Chat *message, unsigned int clientID){
121    return message->process();
122  }
123 
124  bool Client::sendChat(packet::Chat *chat){
125    chat->process();
126    packet::Packet *p = new packet::Packet(chat);
127    return p->send();
128  }
129
130  /**
131  * submits a chat message to the server
132  * @param message message to send
133  * @return true/false
134  */
135  bool Client::sendChat( std::string message ){
136    // generate packet and add it to queue
137    if(!isConnected)
138      return false;
139    return client_connection.addPacket(pck_gen.chatMessage( message.c_str() ));
140      //return client_connection.sendPackets();
141    // send packets
142    return false;
143  }
144
145  /**
146  * Performs a GameState update
147  */
148  void Client::tick(float time){
149//     COUT(3) << ".";
150    if(client_connection.isConnected() && isSynched_){
151      COUT(4) << "popping partial gamestate: " << std::endl;
152      GameStateCompressed *gs = gamestate.popPartialGameState();
153      if(gs){
154        COUT(4) << "client tick: sending gs " << gs << std::endl;
155        ENetPacket *packet = pck_gen.gstate(gs);
156        if( packet == NULL || !client_connection.addPacket(packet))
157          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
158        // now delete it to save memory
159        delete[] gs->data;
160        delete gs;
161      }
162    }
163    ENetEvent *event;
164    // stop if the packet queue is empty
165    while(!(client_connection.queueEmpty())){
166      event = client_connection.getEvent();
167      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
168      elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
169    }
170    int gameStateID = gamestate.processGameState();
171    if(gameStateID==GAMESTATEID_INITIAL)
172      if(gameStateFailure_){
173        if(!client_connection.addPacket(pck_gen.acknowledgement(GAMESTATEID_INITIAL)))
174          COUT(3) << "could not (negatively) ack gamestate" << std::endl;
175        else 
176          COUT(4) << "negatively acked a gamestate" << std::endl;
177        }
178      else
179        gameStateFailure_=true;
180    else if(gameStateID!=0){
181      // ack gamestate and set synched
182      if(!isSynched_)
183        isSynched_=true;
184      gameStateFailure_=false;
185      if(!client_connection.addPacket(pck_gen.acknowledgement(gameStateID)))
186        COUT(3) << "could not ack gamestate" << std::endl;
187    }// otherwise we had no gamestate to load
188    gamestate.cleanup();
189    /*if(!client_connection.sendPackets())
190      COUT(3) << "Problem sending packets to server" << std::endl;*/
191    return;
192  }
193
194  void Client::processGamestate( GameStateCompressed *data, int clientID){
195    COUT(5) << "received gamestate id: " << data->id << std::endl;
196    gamestate.addGameState(data);
197  }
198
199  void Client::processClassid(classid *clid){
200    orxonox::Identifier *id;
201    id=ID(std::string(clid->message));
202    if(id!=NULL)
203      id->setNetworkID(clid->clid);
204    COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
205    COUT(4) << "id(classid)->getName " << ID((unsigned int)clid->clid)->getName() << std::endl;
206    delete clid;
207    return;
208  }
209
210//   void Client::processChat( chat *data, int clientId){
211//     COUT(1) << data->message << std::endl;
212//     delete[] data->message;
213//     delete data;
214//   }
215 
216  bool Client::processWelcome( welcome *w ){
217    COUT(4) << "processing welcome message" << std::endl;
218    clientID_ = w->clientID;
219    shipID_ = w->shipID;
220    delete w;
221    return true;
222  }
223
224}
Note: See TracBrowser for help on using the repository browser.