Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged netp4 back to trunk

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