Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/Client.cc @ 3202

Last change on this file since 3202 was 3202, checked in by scheusso, 15 years ago

rest of the cleanup ( mostly client connection handling)
network is now single-threaded ( only in order to become multithreaded again, but thats another story ;) )

  • Property svn:eol-style set to native
File size: 4.1 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
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#include "FunctionCallManager.h"
51
52namespace orxonox
53{
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():
61      isSynched_(false),
62      gameStateFailure_(false)
63  {
64  }
65
66  /**
67  * Constructor for the Client class
68  * @param address the server address
69  * @param port port of the application on the server
70  */
71  Client::Client(const std::string& address, int port):
72      isSynched_(false),
73      gameStateFailure_(false)
74  {
75  }
76
77  Client::~Client(){
78    if ( ClientConnection::isConnected() )
79      closeConnection();
80  }
81
82  /**
83  * Establish the Connection to the Server
84  * @return true/false
85  */
86  bool Client::establishConnection(){
87    Synchronisable::setClient(true);
88    return ClientConnection::establishConnection();
89  }
90
91  /**
92  * closes the Connection to the Server
93  * @return true/false
94  */
95  bool Client::closeConnection(){
96    return ClientConnection::closeConnection();
97  }
98
99  bool Client::queuePacket(ENetPacket *packet, int clientID){
100    bool b = ClientConnection::addPacket(packet);
101    assert(b);
102    return b;
103  }
104
105  bool Client::processChat(const std::string& message, unsigned int playerID){
106//    COUT(1) << "Player " << playerID << ": " << message << std::endl;
107    return true;
108  }
109
110  /**
111   * This function implements the method of sending a chat message to the server
112   * @param message message to be sent
113   * @return result(true/false)
114   */
115  bool Client::chat(const std::string& message){
116    packet::Chat *m = new packet::Chat(message, Host::getPlayerID());
117    return m->send();
118  }
119
120
121  /**
122   * Processes incoming packets, sends a gamestate to the server and does the cleanup
123   * @param time
124   */
125  void Client::update(const Clock& time){
126    //this steers our network frequency
127    timeSinceLastUpdate_+=time.getDeltaTime();
128    if(timeSinceLastUpdate_>=NETWORK_PERIOD)
129    {
130      timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
131      //     COUT(3) << ".";
132      if ( isConnected() && isSynched_ )
133      {
134        COUT(4) << "popping partial gamestate: " << std::endl;
135        packet::Gamestate *gs = gamestate.getGamestate();
136        //assert(gs); <--- there might be the case that no data has to be sent, so its commented out now
137        if(gs){
138          COUT(4) << "client tick: sending gs " << gs << std::endl;
139          if( !gs->send() )
140            COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
141        // gs gets automatically deleted by enet callback
142        }
143        FunctionCallManager::sendCalls();
144      }
145    }
146    sendPackets(); // flush the enet queue
147   
148    Connection::processQueue();
149    if(gamestate.processGamestates())
150    {
151      if(!isSynched_)
152        isSynched_=true;
153    }
154    gamestate.cleanup();
155
156    return;
157  }
158
159}
Note: See TracBrowser for help on using the repository browser.