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