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