| [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" | 
|---|
| [8858] | 46 | #include "util/Output.h" | 
|---|
| [2662] | 47 | #include "synchronisable/Synchronisable.h" | 
|---|
| [3214] | 48 | #include "packet/Chat.h" | 
|---|
|  | 49 | #include "packet/Gamestate.h" | 
|---|
| [3084] | 50 | #include "FunctionCallManager.h" | 
|---|
| [5929] | 51 | #include "core/CoreIncludes.h" | 
|---|
|  | 52 | #include "core/Game.h" | 
|---|
| [10624] | 53 | #include "core/commandline/CommandLineParser.h" | 
|---|
|  | 54 | #include "core/singleton/ScopedSingletonIncludes.h" | 
|---|
| [2087] | 55 |  | 
|---|
| [2171] | 56 | namespace orxonox | 
|---|
| [1502] | 57 | { | 
|---|
| [1752] | 58 |  | 
|---|
| [10624] | 59 | ManageScopedSingleton( Client, ScopeID::ROOT, false ); | 
|---|
| [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(): | 
|---|
|  | 66 | isSynched_(false), | 
|---|
| [6417] | 67 | gameStateFailure_(false), | 
|---|
|  | 68 | timeSinceLastUpdate_(0) | 
|---|
| [3214] | 69 | { | 
|---|
| [9550] | 70 | this->setDestination( CommandLineParser::getValue("dest").get<std::string>(), CommandLineParser::getValue("port") ); | 
|---|
| [1502] | 71 | } | 
|---|
|  | 72 |  | 
|---|
| [7163] | 73 | Client::~Client() | 
|---|
| [3214] | 74 | { | 
|---|
|  | 75 | if ( ClientConnection::isConnected() ) | 
|---|
| [1502] | 76 | closeConnection(); | 
|---|
|  | 77 | } | 
|---|
| [1752] | 78 |  | 
|---|
| [1502] | 79 | /** | 
|---|
|  | 80 | * Establish the Connection to the Server | 
|---|
|  | 81 | * @return true/false | 
|---|
|  | 82 | */ | 
|---|
| [7163] | 83 | bool Client::establishConnection() | 
|---|
|  | 84 | { | 
|---|
| [1502] | 85 | Synchronisable::setClient(true); | 
|---|
| [7163] | 86 | if( ClientConnection::establishConnection() ) | 
|---|
|  | 87 | { | 
|---|
|  | 88 | Host::setActive(true); | 
|---|
| [7801] | 89 | GamestateManager::addPeer(NETWORK_PEER_ID_SERVER); | 
|---|
| [7163] | 90 | return true; | 
|---|
|  | 91 | } | 
|---|
|  | 92 | else | 
|---|
|  | 93 | return false; | 
|---|
| [1502] | 94 | } | 
|---|
|  | 95 |  | 
|---|
|  | 96 | /** | 
|---|
|  | 97 | * closes the Connection to the Server | 
|---|
|  | 98 | * @return true/false | 
|---|
|  | 99 | */ | 
|---|
| [7163] | 100 | bool Client::closeConnection() | 
|---|
|  | 101 | { | 
|---|
|  | 102 | Host::setActive(false); | 
|---|
| [7801] | 103 | GamestateManager::removePeer(NETWORK_PEER_ID_SERVER); | 
|---|
| [3214] | 104 | return ClientConnection::closeConnection(); | 
|---|
| [1502] | 105 | } | 
|---|
| [7284] | 106 |  | 
|---|
| [7163] | 107 | void Client::setDestination(const std::string& serverAddress, unsigned int port) | 
|---|
|  | 108 | { | 
|---|
|  | 109 | ClientConnection::setServerAddress(serverAddress); | 
|---|
|  | 110 | ClientConnection::setPort(port); | 
|---|
|  | 111 | } | 
|---|
| [1502] | 112 |  | 
|---|
| [7801] | 113 | void Client::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) | 
|---|
| [7163] | 114 | { | 
|---|
| [7801] | 115 | ClientConnection::addPacket(packet, channelID); | 
|---|
| [1735] | 116 | } | 
|---|
| [1752] | 117 |  | 
|---|
| [8858] | 118 | void Client::printRTT() | 
|---|
| [7163] | 119 | { | 
|---|
| [8858] | 120 | orxout(message) << "Round trip time to server is " << ClientConnection::getRTT() << " ms" << endl; | 
|---|
| [1534] | 121 | } | 
|---|
| [6417] | 122 |  | 
|---|
| [8858] | 123 | /** | 
|---|
|  | 124 | * @brief Sends a chat message to the server. | 
|---|
|  | 125 | * @param message message to be sent | 
|---|
|  | 126 | * @param sourceID the ID of the sender | 
|---|
|  | 127 | * @param targetID the ID of the receiver | 
|---|
|  | 128 | */ | 
|---|
|  | 129 | void Client::doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) | 
|---|
| [7163] | 130 | { | 
|---|
| [8858] | 131 | // send the message to the server | 
|---|
|  | 132 | packet::Chat* packet = new packet::Chat(message, sourceID, targetID); | 
|---|
|  | 133 | packet->send(static_cast<Host*>(this)); | 
|---|
| [5961] | 134 | } | 
|---|
| [2087] | 135 |  | 
|---|
| [1502] | 136 | /** | 
|---|
| [8858] | 137 | * @brief Gets called if a packet::Chat packet is received. Calls the parent function which passes the message to the listeners. | 
|---|
| [1907] | 138 | */ | 
|---|
| [8858] | 139 | void Client::doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID) | 
|---|
| [7163] | 140 | { | 
|---|
| [8858] | 141 | // call the parent function which passes the message to the listeners | 
|---|
|  | 142 | Host::doReceiveChat(message, sourceID, targetID); | 
|---|
| [1502] | 143 | } | 
|---|
|  | 144 |  | 
|---|
|  | 145 | /** | 
|---|
| [1907] | 146 | * Processes incoming packets, sends a gamestate to the server and does the cleanup | 
|---|
| [2087] | 147 | * @param time | 
|---|
| [1907] | 148 | */ | 
|---|
| [7163] | 149 | void Client::update(const Clock& time) | 
|---|
|  | 150 | { | 
|---|
| [3084] | 151 | //this steers our network frequency | 
|---|
|  | 152 | timeSinceLastUpdate_+=time.getDeltaTime(); | 
|---|
| [3214] | 153 | if(timeSinceLastUpdate_>=NETWORK_PERIOD) | 
|---|
|  | 154 | { | 
|---|
| [3084] | 155 | timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD; | 
|---|
| [3214] | 156 | if ( isConnected() && isSynched_ ) | 
|---|
|  | 157 | { | 
|---|
| [8858] | 158 | orxout(verbose, context::network) << "popping partial gamestate: " << endl; | 
|---|
| [7801] | 159 | //         packet::Gamestate *gs = GamestateClient::getGamestate(); | 
|---|
| [8327] | 160 | if( GamestateManager::update() ) | 
|---|
| [7801] | 161 | { | 
|---|
| [8327] | 162 | std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates(); | 
|---|
|  | 163 | std::vector<packet::Gamestate*>::iterator it; | 
|---|
|  | 164 | for( it = gamestates.begin(); it != gamestates.end(); ++it ) | 
|---|
|  | 165 | { | 
|---|
|  | 166 | (*it)->send( static_cast<Host*>(this) ); | 
|---|
|  | 167 | } | 
|---|
| [7801] | 168 | } | 
|---|
| [3084] | 169 | //assert(gs); <--- there might be the case that no data has to be sent, so its commented out now | 
|---|
| [7801] | 170 | //         if(gs){ | 
|---|
| [8858] | 171 | //           orxout(verbose, context::network) << "client tick: sending gs " << gs << endl; | 
|---|
| [7801] | 172 | //           if( !gs->send() ) | 
|---|
| [8858] | 173 | //             orxout(internal_warning, context::network) << "Problem adding partial gamestate to queue" << endl; | 
|---|
| [7801] | 174 | //         // gs gets automatically deleted by enet callback | 
|---|
|  | 175 | //         } | 
|---|
|  | 176 | FunctionCallManager::sendCalls(static_cast<Host*>(this)); | 
|---|
| [1751] | 177 | } | 
|---|
| [1502] | 178 | } | 
|---|
| [7801] | 179 | //     sendPackets(); // flush the enet queue | 
|---|
| [6417] | 180 |  | 
|---|
| [3214] | 181 | Connection::processQueue(); | 
|---|
| [7801] | 182 | if(GamestateManager::processGamestates()) | 
|---|
| [1769] | 183 | { | 
|---|
| [7495] | 184 | FunctionCallManager::processBufferedFunctionCalls(); | 
|---|
| [1502] | 185 | if(!isSynched_) | 
|---|
|  | 186 | isSynched_=true; | 
|---|
| [1907] | 187 | } | 
|---|
| [7801] | 188 | //     GamestateManager::cleanup();; | 
|---|
|  | 189 | //     Connection::sendPackets(); | 
|---|
| [3084] | 190 |  | 
|---|
| [1502] | 191 | return; | 
|---|
|  | 192 | } | 
|---|
| [6417] | 193 |  | 
|---|
| [5929] | 194 | void Client::connectionClosed() | 
|---|
|  | 195 | { | 
|---|
|  | 196 | Game::getInstance().popState(); | 
|---|
|  | 197 | Game::getInstance().popState(); | 
|---|
|  | 198 | } | 
|---|
| [8858] | 199 |  | 
|---|
| [7801] | 200 | void Client::processPacket(packet::Packet* packet) | 
|---|
|  | 201 | { | 
|---|
|  | 202 | if( packet->isReliable() ) | 
|---|
|  | 203 | { | 
|---|
| [8327] | 204 | if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() ) | 
|---|
| [7801] | 205 | packet->process(static_cast<Host*>(this)); | 
|---|
|  | 206 | else | 
|---|
|  | 207 | this->packetQueue_.push_back(packet); | 
|---|
|  | 208 | } | 
|---|
|  | 209 | else | 
|---|
|  | 210 | packet->process(static_cast<Host*>(this)); | 
|---|
|  | 211 | } | 
|---|
| [1502] | 212 | } | 
|---|