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