Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network branch merged into trunk

  • Property svn:eol-style set to native
File size: 5.4 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#include "packet/Acknowledgement.h"
48
49namespace network
50{
51//   SetConsoleCommandShortcut(Client, chat);
52 
53 
54  /**
55  * Constructor for the Client class
56  * initializes the address and the port to default localhost:NETWORK_PORT
57  */
58  Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){
59    // set server address to localhost
60    isConnected=false;
61    isSynched_=false;
62    gameStateFailure_=false;
63  }
64
65  /**
66  * Constructor for the Client class
67  * @param address the server address
68  * @param port port of the application on the server
69  */
70  Client::Client(std::string address, int port) : client_connection(port, address){
71    isConnected=false;
72    isSynched_=false;
73    gameStateFailure_=false;
74  }
75
76  /**
77  * Constructor for the Client class
78  * @param address the server address
79  * @param port port of the application on the server
80  */
81  Client::Client(const char *address, int port) : client_connection(port, address){
82    isConnected=false;
83    isSynched_=false;
84    gameStateFailure_=false;
85  }
86
87  Client::~Client(){
88    if(isConnected)
89      closeConnection();
90  }
91 
92  /**
93  * Establish the Connection to the Server
94  * @return true/false
95  */
96  bool Client::establishConnection(){
97    Synchronisable::setClient(true);
98    isConnected=client_connection.createConnection();
99    if(isConnected){
100//       COUT(3) << "sending connectrequest" << std::endl;
101//       if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
102//         COUT(1) << "could not send connection request !!!!!!!!!" << std::endl;
103    }else
104      COUT(1) << "could not create connection laber" << std::endl;
105    return isConnected;
106  }
107
108  /**
109  * closes the Connection to the Server
110  * @return true/false
111  */
112  bool Client::closeConnection(){
113    isConnected=false;
114    return client_connection.closeConnection();
115  }
116
117  bool Client::queuePacket(ENetPacket *packet, int clientID){
118    return client_connection.addPacket(packet);
119  }
120 
121  bool Client::processChat(packet::Chat *message, unsigned int clientID){
122    return message->process();
123  }
124 
125  /*bool Client::sendChat(packet::Chat *chat){
126    chat->process();
127    packet::Packet *p = new packet::Packet(chat);
128    return p->send();
129  }*/
130 
131
132  /**
133  * submits a chat message to the server
134  * @param message message to send
135  * @return true/false
136  */
137  bool Client::sendChat( std::string message ){
138    // generate packet and add it to queue
139    if(!isConnected)
140      return false;
141    packet::Chat chat(message, 0);
142    return chat.send();
143    // send packets
144  }
145
146  /**
147  * Performs a GameState update
148  */
149  void Client::tick(float time){
150//     COUT(3) << ".";
151    if(client_connection.isConnected() && isSynched_){
152      COUT(4) << "popping partial gamestate: " << std::endl;
153      /*packet::Gamestate *gs = gamestate.getGamestate();
154      if(gs){
155        COUT(4) << "client tick: sending gs " << gs << std::endl;
156        if( !gs->send() )
157          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
158        // gs gets automatically deleted by enet callback
159      }*/
160    }
161    ENetEvent *event;
162    // stop if the packet queue is empty
163    while(!(client_connection.queueEmpty())){
164      event = client_connection.getEvent();
165      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
166      packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer);
167      assert(packet->process());
168    }
169    int gameStateID = gamestate.processGamestates();
170    /*if(gameStateID==GAMESTATEID_INITIAL)
171      if(gameStateFailure_){
172        packet::Acknowledgement ack(GAMESTATEID_INITIAL, 0);
173        if(!ack.send())
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      packet::Acknowledgement ack(gameStateID, 0);
186      if(!ack.send())
187        COUT(3) << "could not ack gamestate" << std::endl;
188    }*/// otherwise we had no gamestate to load
189    gamestate.cleanup();
190    return;
191  }
192
193}
Note: See TracBrowser for help on using the repository browser.