Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

chat works again now. some doxygen. small change according to playerid in Host

  • Property svn:eol-style set to native
File size: 4.5 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 "packet/Packet.h"
46// #include "packet/Acknowledgement.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    isServer_ = 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    isServer_ = false;
75  }
76
77  /**
78  * Constructor for the Client class
79  * @param address the server address
80  * @param port port of the application on the server
81  */
82  Client::Client(const char *address, int port) : client_connection(port, address){
83    isConnected=false;
84    isSynched_=false;
85    gameStateFailure_=false;
86    isServer_ = 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(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(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      assert(packet->process());
158    }
159    if(gamestate.processGamestates())
160    {
161      if(!isSynched_)
162        isSynched_=true;
163    }
164    gamestate.cleanup();
165    return;
166  }
167
168}
Note: See TracBrowser for help on using the repository browser.