Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/network/Client.cc @ 2143

Last change on this file since 2143 was 2112, checked in by rgrieder, 16 years ago

Test: replacing namespace network with namespace orxonox. network::packet —> orxonox::packet

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