[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: |
---|
| 23 | * Oliver Scheuss, (C) 2007 |
---|
| 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 | |
---|
[2087] | 41 | #include <cassert> |
---|
[2773] | 42 | #include <enet/enet.h> |
---|
[2087] | 43 | |
---|
[1502] | 44 | #include "Client.h" |
---|
[1735] | 45 | #include "Host.h" |
---|
[2662] | 46 | #include "synchronisable/Synchronisable.h" |
---|
[1502] | 47 | #include "core/CoreIncludes.h" |
---|
[1735] | 48 | #include "packet/Packet.h" |
---|
[2948] | 49 | #include "FunctionCallManager.h" |
---|
[2087] | 50 | |
---|
[1769] | 51 | // #include "packet/Acknowledgement.h" |
---|
[1502] | 52 | |
---|
[2171] | 53 | namespace orxonox |
---|
[1502] | 54 | { |
---|
[1735] | 55 | // SetConsoleCommandShortcut(Client, chat); |
---|
[1752] | 56 | |
---|
| 57 | |
---|
[1502] | 58 | /** |
---|
| 59 | * Constructor for the Client class |
---|
| 60 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
| 61 | */ |
---|
| 62 | Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ |
---|
| 63 | // set server address to localhost |
---|
| 64 | isConnected=false; |
---|
| 65 | isSynched_=false; |
---|
| 66 | gameStateFailure_=false; |
---|
| 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 | */ |
---|
[2087] | 74 | Client::Client(const std::string& address, int port) : client_connection(port, address){ |
---|
[1502] | 75 | isConnected=false; |
---|
| 76 | isSynched_=false; |
---|
| 77 | gameStateFailure_=false; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Constructor for the Client class |
---|
| 82 | * @param address the server address |
---|
| 83 | * @param port port of the application on the server |
---|
| 84 | */ |
---|
| 85 | Client::Client(const char *address, int port) : client_connection(port, address){ |
---|
| 86 | isConnected=false; |
---|
| 87 | isSynched_=false; |
---|
| 88 | gameStateFailure_=false; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | Client::~Client(){ |
---|
| 92 | if(isConnected) |
---|
| 93 | closeConnection(); |
---|
| 94 | } |
---|
[1752] | 95 | |
---|
[1502] | 96 | /** |
---|
| 97 | * Establish the Connection to the Server |
---|
| 98 | * @return true/false |
---|
| 99 | */ |
---|
| 100 | bool Client::establishConnection(){ |
---|
| 101 | Synchronisable::setClient(true); |
---|
| 102 | isConnected=client_connection.createConnection(); |
---|
[1767] | 103 | if(!isConnected) |
---|
[1502] | 104 | COUT(1) << "could not create connection laber" << std::endl; |
---|
| 105 | return isConnected; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * closes the Connection to the Server |
---|
| 110 | * @return true/false |
---|
| 111 | */ |
---|
| 112 | bool Client::closeConnection(){ |
---|
| 113 | isConnected=false; |
---|
| 114 | return client_connection.closeConnection(); |
---|
| 115 | } |
---|
| 116 | |
---|
[1735] | 117 | bool Client::queuePacket(ENetPacket *packet, int clientID){ |
---|
| 118 | return client_connection.addPacket(packet); |
---|
| 119 | } |
---|
[1752] | 120 | |
---|
[2087] | 121 | bool Client::processChat(const std::string& message, unsigned int playerID){ |
---|
| 122 | // COUT(1) << "Player " << playerID << ": " << message << std::endl; |
---|
[1907] | 123 | return true; |
---|
[1534] | 124 | } |
---|
[2087] | 125 | |
---|
[1502] | 126 | /** |
---|
[1907] | 127 | * This function implements the method of sending a chat message to the server |
---|
[2087] | 128 | * @param message message to be sent |
---|
[1907] | 129 | * @return result(true/false) |
---|
| 130 | */ |
---|
[2087] | 131 | bool Client::chat(const std::string& message){ |
---|
[1907] | 132 | packet::Chat *m = new packet::Chat(message, Host::getPlayerID()); |
---|
| 133 | return m->send(); |
---|
[1502] | 134 | } |
---|
| 135 | |
---|
[1907] | 136 | |
---|
[1502] | 137 | /** |
---|
[1907] | 138 | * Processes incoming packets, sends a gamestate to the server and does the cleanup |
---|
[2087] | 139 | * @param time |
---|
[1907] | 140 | */ |
---|
[1502] | 141 | void Client::tick(float time){ |
---|
[2965] | 142 | //this steers our network frequency |
---|
| 143 | timeSinceLastUpdate_+=time; |
---|
| 144 | if(timeSinceLastUpdate_>=NETWORK_PERIOD){ |
---|
| 145 | timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD; |
---|
| 146 | // COUT(3) << "."; |
---|
| 147 | if(client_connection.isConnected() && isSynched_){ |
---|
| 148 | COUT(4) << "popping partial gamestate: " << std::endl; |
---|
| 149 | packet::Gamestate *gs = gamestate.getGamestate(); |
---|
| 150 | if(gs){ |
---|
| 151 | COUT(4) << "client tick: sending gs " << gs << std::endl; |
---|
| 152 | if( !gs->send() ) |
---|
| 153 | COUT(3) << "Problem adding partial gamestate to queue" << std::endl; |
---|
[1735] | 154 | // gs gets automatically deleted by enet callback |
---|
[2965] | 155 | } |
---|
| 156 | FunctionCallManager::sendCalls(); |
---|
[1751] | 157 | } |
---|
[2965] | 158 | ENetEvent *event; |
---|
[1502] | 159 | // stop if the packet queue is empty |
---|
[2965] | 160 | while(!(client_connection.queueEmpty())){ |
---|
| 161 | event = client_connection.getEvent(); |
---|
| 162 | COUT(5) << "tick packet size " << event->packet->dataLength << std::endl; |
---|
| 163 | packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer); |
---|
[2087] | 164 | // note: packet commits suicide here except for the GameState. That is then deleted by a GamestateHandler |
---|
[2965] | 165 | bool b = packet->process(); |
---|
| 166 | assert(b); |
---|
| 167 | } |
---|
| 168 | if(gamestate.processGamestates()) |
---|
| 169 | { |
---|
| 170 | if(!isSynched_) |
---|
| 171 | isSynched_=true; |
---|
| 172 | } |
---|
| 173 | gamestate.cleanup(); |
---|
[1502] | 174 | } |
---|
[2965] | 175 | |
---|
[1502] | 176 | return; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | } |
---|