Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/Client.cc @ 7596

Last change on this file since 7596 was 7495, checked in by scheusso, 14 years ago

buffering incoming function calls for non-existing objects works now

  • Property svn:eol-style set to native
File size: 5.1 KB
RevLine 
[1502]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:
[3084]23 *      Oliver Scheuss
[1502]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
[3214]41#include "Client.h"
42
[2087]43#include <cassert>
44
[5929]45#include "util/Clock.h"
[3214]46#include "util/Debug.h"
[7284]47#include "util/ScopedSingletonManager.h"
[2662]48#include "synchronisable/Synchronisable.h"
[3214]49#include "packet/Chat.h"
50#include "packet/Gamestate.h"
[3084]51#include "FunctionCallManager.h"
[5929]52#include "core/CoreIncludes.h"
[7163]53#include "core/CommandLineParser.h"
[5929]54#include "core/Game.h"
[2087]55
[2171]56namespace orxonox
[1502]57{
[1752]58
[7163]59  ManageScopedSingleton( Client, ScopeID::Root, true );
[1752]60
[1502]61  /**
62  * Constructor for the Client class
63  * initializes the address and the port to default localhost:NETWORK_PORT
64  */
[3214]65  Client::Client():
[7163]66      gamestate(0),
[3214]67      isSynched_(false),
[6417]68      gameStateFailure_(false),
69      timeSinceLastUpdate_(0)
[3214]70  {
[7163]71    this->setDestination( CommandLineParser::getValue("dest").getString(), CommandLineParser::getValue("port") );
[1502]72  }
73
[7163]74  Client::~Client()
[3214]75  {
76    if ( ClientConnection::isConnected() )
[1502]77      closeConnection();
78  }
[1752]79
[1502]80  /**
81  * Establish the Connection to the Server
82  * @return true/false
83  */
[7163]84  bool Client::establishConnection()
85  {
[1502]86    Synchronisable::setClient(true);
[7163]87    this->gamestate = new GamestateClient();
88    if( ClientConnection::establishConnection() )
89    {
90      Host::setActive(true);
91      return true;
92    }
93    else
94      return false;
[1502]95  }
96
97  /**
98  * closes the Connection to the Server
99  * @return true/false
100  */
[7163]101  bool Client::closeConnection()
102  {
103    assert(this->gamestate);
104    delete this->gamestate;
105    this->gamestate = 0;
106    Host::setActive(false);
[3214]107    return ClientConnection::closeConnection();
[1502]108  }
[7284]109
[7163]110  void Client::setDestination(const std::string& serverAddress, unsigned int port)
111  {
112    ClientConnection::setServerAddress(serverAddress);
113    ClientConnection::setPort(port);
114  }
[1502]115
[7163]116  bool Client::queuePacket(ENetPacket *packet, int clientID)
117  {
[3214]118    bool b = ClientConnection::addPacket(packet);
119    assert(b);
120    return b;
[1735]121  }
[1752]122
[7163]123  bool Client::processChat(const std::string& message, unsigned int playerID)
124  {
[2087]125//    COUT(1) << "Player " << playerID << ": " << message << std::endl;
[1907]126    return true;
[1534]127  }
[6417]128
[7163]129  void Client::printRTT()
130  {
[5961]131    COUT(0) << "Round trip time to server is " << ClientConnection::getRTT() << " ms" << endl;
132  }
[2087]133
[1502]134  /**
[1907]135   * This function implements the method of sending a chat message to the server
[2087]136   * @param message message to be sent
[1907]137   * @return result(true/false)
138   */
[7163]139  bool Client::chat(const std::string& message)
140  {
[1907]141    packet::Chat *m = new packet::Chat(message, Host::getPlayerID());
142    return m->send();
[1502]143  }
144
[1907]145
[1502]146  /**
[1907]147   * Processes incoming packets, sends a gamestate to the server and does the cleanup
[2087]148   * @param time
[1907]149   */
[7163]150  void Client::update(const Clock& time)
151  {
[3084]152    //this steers our network frequency
153    timeSinceLastUpdate_+=time.getDeltaTime();
[3214]154    if(timeSinceLastUpdate_>=NETWORK_PERIOD)
155    {
[3084]156      timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
[6417]157      //     COUT(3) << '.';
[3214]158      if ( isConnected() && isSynched_ )
159      {
[3084]160        COUT(4) << "popping partial gamestate: " << std::endl;
[7163]161        packet::Gamestate *gs = gamestate->getGamestate();
[3084]162        //assert(gs); <--- there might be the case that no data has to be sent, so its commented out now
163        if(gs){
164          COUT(4) << "client tick: sending gs " << gs << std::endl;
165          if( !gs->send() )
166            COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
[1735]167        // gs gets automatically deleted by enet callback
[3084]168        }
169        FunctionCallManager::sendCalls();
[1751]170      }
[1502]171    }
[3214]172    sendPackets(); // flush the enet queue
[6417]173
[3214]174    Connection::processQueue();
[7163]175    if(gamestate->processGamestates())
[1769]176    {
[7495]177      FunctionCallManager::processBufferedFunctionCalls();
[1502]178      if(!isSynched_)
179        isSynched_=true;
[1907]180    }
[7163]181    gamestate->cleanup();
[5965]182    Connection::sendPackets();
[3084]183
[1502]184    return;
185  }
[6417]186
[5929]187  void Client::connectionClosed()
188  {
189    ObjectList<Synchronisable>::iterator it;
190    for(it = ObjectList<Synchronisable>::begin(); it; )
191    {
192      if( it->getSyncMode() != 0x0 )
193        (it++)->destroy();
194      else
195      {
196        ++it;
197      }
198    }
199    Game::getInstance().popState();
200    Game::getInstance().popState();
201  }
[1502]202
[7163]203
204
[1502]205}
Note: See TracBrowser for help on using the repository browser.