Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3203 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
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
52// #include "packet/Acknowledgement.h"
53
54namespace orxonox
55{
56//   SetConsoleCommandShortcut(Client, chat);
57
58
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  */
75  Client::Client(const std::string& address, int port) : client_connection(port, address){
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  }
96
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();
104    if(!isConnected)
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
118  bool Client::queuePacket(ENetPacket *packet, int clientID){
119    return client_connection.addPacket(packet);
120  }
121
122  bool Client::processChat(const std::string& message, unsigned int playerID){
123//    COUT(1) << "Player " << playerID << ": " << message << std::endl;
124    return true;
125  }
126
127  /**
128   * This function implements the method of sending a chat message to the server
129   * @param message message to be sent
130   * @return result(true/false)
131   */
132  bool Client::chat(const std::string& message){
133    packet::Chat *m = new packet::Chat(message, Host::getPlayerID());
134    return m->send();
135  }
136
137
138  /**
139   * Processes incoming packets, sends a gamestate to the server and does the cleanup
140   * @param time
141   */
142  void Client::update(const Clock& time){
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;
156        // gs gets automatically deleted by enet callback
157        }
158        FunctionCallManager::sendCalls();
159      }
160    }
161   
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;
167      packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer);
168      // note: packet commits suicide here except for the GameState. That is then deleted by a GamestateHandler
169      bool b = packet->process();
170      assert(b);
171      delete event;
172    }
173    if(gamestate.processGamestates())
174    {
175      if(!isSynched_)
176        isSynched_=true;
177    }
178    gamestate.cleanup();
179
180    return;
181  }
182
183}
Note: See TracBrowser for help on using the repository browser.