Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged network branch back to trunk

  • 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  }
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(1) << "could not create connection laber" << std::endl;
100    return isConnected;
101  }
102
103  /**
104  * closes the Connection to the Server
105  * @return true/false
106  */
107  bool Client::closeConnection(){
108    isConnected=false;
109    return client_connection.closeConnection();
110  }
111
112  bool Client::queuePacket(ENetPacket *packet, int clientID){
113    return client_connection.addPacket(packet);
114  }
115
116  bool Client::processChat(std::string message, unsigned int playerID){
117    COUT(1) << "Player " << playerID << ": " << message << std::endl;
118    return true;
119  }
120 
121  /**
122   * This function implements the method of sending a chat message to the server
123   * @param message message to be sent
124   * @return result(true/false)
125   */
126  bool Client::chat(std::string message){
127    packet::Chat *m = new packet::Chat(message, Host::getPlayerID());
128    return m->send();
129  }
130
131
132  /**
133   * Processes incoming packets, sends a gamestate to the server and does the cleanup
134   * @param time
135   */
136  void Client::tick(float time){
137//     COUT(3) << ".";
138    if(client_connection.isConnected() && isSynched_){
139      COUT(4) << "popping partial gamestate: " << std::endl;
140      packet::Gamestate *gs = gamestate.getGamestate();
141      if(gs){
142        COUT(4) << "client tick: sending gs " << gs << std::endl;
143        if( !gs->send() )
144          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
145        // gs gets automatically deleted by enet callback
146      }
147    }
148    ENetEvent *event;
149    // stop if the packet queue is empty
150    while(!(client_connection.queueEmpty())){
151      event = client_connection.getEvent();
152      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
153      packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer);
154      bool b = packet->process();
155      assert(b);
156    }
157    if(gamestate.processGamestates())
158    {
159      if(!isSynched_)
160        isSynched_=true;
161    }
162    gamestate.cleanup();
163    return;
164  }
165
166}
Note: See TracBrowser for help on using the repository browser.