[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" |
---|
[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" |
---|
[2087] | 53 | |
---|
[2171] | 54 | namespace orxonox |
---|
[1502] | 55 | { |
---|
[1752] | 56 | |
---|
| 57 | |
---|
[1502] | 58 | /** |
---|
| 59 | * Constructor for the Client class |
---|
| 60 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
| 61 | */ |
---|
[3214] | 62 | Client::Client(): |
---|
| 63 | isSynched_(false), |
---|
[6134] | 64 | gameStateFailure_(false), |
---|
| 65 | timeSinceLastUpdate_(0) |
---|
[3214] | 66 | { |
---|
[1502] | 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 | */ |
---|
[3214] | 74 | Client::Client(const std::string& address, int port): |
---|
| 75 | isSynched_(false), |
---|
[6134] | 76 | gameStateFailure_(false), |
---|
| 77 | timeSinceLastUpdate_(0) |
---|
[3214] | 78 | { |
---|
| 79 | setPort( port ); |
---|
| 80 | setServerAddress( address ); |
---|
[1502] | 81 | } |
---|
| 82 | |
---|
| 83 | Client::~Client(){ |
---|
[3214] | 84 | if ( ClientConnection::isConnected() ) |
---|
[1502] | 85 | closeConnection(); |
---|
| 86 | } |
---|
[1752] | 87 | |
---|
[1502] | 88 | /** |
---|
| 89 | * Establish the Connection to the Server |
---|
| 90 | * @return true/false |
---|
| 91 | */ |
---|
| 92 | bool Client::establishConnection(){ |
---|
| 93 | Synchronisable::setClient(true); |
---|
[3214] | 94 | return ClientConnection::establishConnection(); |
---|
[1502] | 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * closes the Connection to the Server |
---|
| 99 | * @return true/false |
---|
| 100 | */ |
---|
| 101 | bool Client::closeConnection(){ |
---|
[3214] | 102 | return ClientConnection::closeConnection(); |
---|
[1502] | 103 | } |
---|
| 104 | |
---|
[1735] | 105 | bool Client::queuePacket(ENetPacket *packet, int clientID){ |
---|
[3214] | 106 | bool b = ClientConnection::addPacket(packet); |
---|
| 107 | assert(b); |
---|
| 108 | return b; |
---|
[1735] | 109 | } |
---|
[1752] | 110 | |
---|
[2087] | 111 | bool Client::processChat(const std::string& message, unsigned int playerID){ |
---|
| 112 | // COUT(1) << "Player " << playerID << ": " << message << std::endl; |
---|
[1907] | 113 | return true; |
---|
[1534] | 114 | } |
---|
[5961] | 115 | |
---|
| 116 | void Client::printRTT(){ |
---|
| 117 | COUT(0) << "Round trip time to server is " << ClientConnection::getRTT() << " ms" << endl; |
---|
| 118 | } |
---|
[2087] | 119 | |
---|
[1502] | 120 | /** |
---|
[1907] | 121 | * This function implements the method of sending a chat message to the server |
---|
[2087] | 122 | * @param message message to be sent |
---|
[1907] | 123 | * @return result(true/false) |
---|
| 124 | */ |
---|
[2087] | 125 | bool Client::chat(const std::string& message){ |
---|
[1907] | 126 | packet::Chat *m = new packet::Chat(message, Host::getPlayerID()); |
---|
| 127 | return m->send(); |
---|
[1502] | 128 | } |
---|
| 129 | |
---|
[1907] | 130 | |
---|
[1502] | 131 | /** |
---|
[1907] | 132 | * Processes incoming packets, sends a gamestate to the server and does the cleanup |
---|
[2087] | 133 | * @param time |
---|
[1907] | 134 | */ |
---|
[2896] | 135 | void Client::update(const Clock& time){ |
---|
[3084] | 136 | //this steers our network frequency |
---|
| 137 | timeSinceLastUpdate_+=time.getDeltaTime(); |
---|
[3214] | 138 | if(timeSinceLastUpdate_>=NETWORK_PERIOD) |
---|
| 139 | { |
---|
[3084] | 140 | timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD; |
---|
| 141 | // COUT(3) << "."; |
---|
[3214] | 142 | if ( isConnected() && isSynched_ ) |
---|
| 143 | { |
---|
[3084] | 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; |
---|
[1735] | 151 | // gs gets automatically deleted by enet callback |
---|
[3084] | 152 | } |
---|
| 153 | FunctionCallManager::sendCalls(); |
---|
[1751] | 154 | } |
---|
[1502] | 155 | } |
---|
[3214] | 156 | sendPackets(); // flush the enet queue |
---|
[3084] | 157 | |
---|
[3214] | 158 | Connection::processQueue(); |
---|
[1769] | 159 | if(gamestate.processGamestates()) |
---|
| 160 | { |
---|
[1502] | 161 | if(!isSynched_) |
---|
| 162 | isSynched_=true; |
---|
[1907] | 163 | } |
---|
[1502] | 164 | gamestate.cleanup(); |
---|
[5965] | 165 | Connection::sendPackets(); |
---|
[3084] | 166 | |
---|
[1502] | 167 | return; |
---|
| 168 | } |
---|
[5929] | 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 | } |
---|
[1502] | 185 | |
---|
| 186 | } |
---|