Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/network/Client.cc @ 2934

Last change on this file since 2934 was 2800, checked in by rgrieder, 16 years ago

Renaming "tick" to "update" for all those classes not inheriting from Tickable to avoid confusions.
GameState::ticked still exists, but that's going to change anyway.

  • Property svn:eol-style set to native
File size: 4.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 <cassert>
42#include <enet/enet.h>
43
44#include "Client.h"
45#include "Host.h"
46#include "synchronisable/Synchronisable.h"
47#include "core/Clock.h"
48#include "core/CoreIncludes.h"
49#include "packet/Packet.h"
50
51// #include "packet/Acknowledgement.h"
52
53namespace orxonox
54{
55//   SetConsoleCommandShortcut(Client, chat);
56
57
58  /**
59  * Constructor for the Client class
60  * initializes the address and the port to default localhost:NETWORK_PORT
61  */
62  Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){
63    // set server address to localhost
64    isConnected=false;
65    isSynched_=false;
66    gameStateFailure_=false;
67  }
68
69  /**
70  * Constructor for the Client class
71  * @param address the server address
72  * @param port port of the application on the server
73  */
74  Client::Client(const std::string& address, int port) : client_connection(port, address){
75    isConnected=false;
76    isSynched_=false;
77    gameStateFailure_=false;
78  }
79
80  /**
81  * Constructor for the Client class
82  * @param address the server address
83  * @param port port of the application on the server
84  */
85  Client::Client(const char *address, int port) : client_connection(port, address){
86    isConnected=false;
87    isSynched_=false;
88    gameStateFailure_=false;
89  }
90
91  Client::~Client(){
92    if(isConnected)
93      closeConnection();
94  }
95
96  /**
97  * Establish the Connection to the Server
98  * @return true/false
99  */
100  bool Client::establishConnection(){
101    Synchronisable::setClient(true);
102    isConnected=client_connection.createConnection();
103    if(!isConnected)
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(const std::string& message, unsigned int playerID){
122//    COUT(1) << "Player " << playerID << ": " << message << std::endl;
123    return true;
124  }
125
126  /**
127   * This function implements the method of sending a chat message to the server
128   * @param message message to be sent
129   * @return result(true/false)
130   */
131  bool Client::chat(const std::string& message){
132    packet::Chat *m = new packet::Chat(message, Host::getPlayerID());
133    return m->send();
134  }
135
136
137  /**
138   * Processes incoming packets, sends a gamestate to the server and does the cleanup
139   * @param time
140   */
141  void Client::update(const Clock& time){
142//     COUT(3) << ".";
143    if(client_connection.isConnected() && isSynched_){
144      COUT(4) << "popping partial gamestate: " << std::endl;
145      packet::Gamestate *gs = gamestate.getGamestate();
146      if(gs){
147        COUT(4) << "client tick: sending gs " << gs << std::endl;
148        if( !gs->send() )
149          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
150        // gs gets automatically deleted by enet callback
151      }
152    }
153    ENetEvent *event;
154    // stop if the packet queue is empty
155    while(!(client_connection.queueEmpty())){
156      event = client_connection.getEvent();
157      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
158      packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer);
159      // note: packet commits suicide here except for the GameState. That is then deleted by a GamestateHandler
160      bool b = packet->process();
161      assert(b);
162    }
163    if(gamestate.processGamestates())
164    {
165      if(!isSynched_)
166        isSynched_=true;
167    }
168    gamestate.cleanup();
169    return;
170  }
171
172}
Note: See TracBrowser for help on using the repository browser.