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