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