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