[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" |
---|
[2907] | 47 | #include "core/Clock.h" |
---|
[1502] | 48 | #include "core/CoreIncludes.h" |
---|
[1735] | 49 | #include "packet/Packet.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 | */ |
---|
[2907] | 141 | void Client::update(const Clock& time){ |
---|
[1502] | 142 | // COUT(3) << "."; |
---|
| 143 | if(client_connection.isConnected() && isSynched_){ |
---|
| 144 | COUT(4) << "popping partial gamestate: " << std::endl; |
---|
[1751] | 145 | packet::Gamestate *gs = gamestate.getGamestate(); |
---|
[1502] | 146 | if(gs){ |
---|
| 147 | COUT(4) << "client tick: sending gs " << gs << std::endl; |
---|
[1735] | 148 | if( !gs->send() ) |
---|
[1502] | 149 | COUT(3) << "Problem adding partial gamestate to queue" << std::endl; |
---|
[1735] | 150 | // gs gets automatically deleted by enet callback |
---|
[1751] | 151 | } |
---|
[1502] | 152 | } |
---|
| 153 | ENetEvent *event; |
---|
| 154 | // stop if the packet queue is empty |
---|
| 155 | while(!(client_connection.queueEmpty())){ |
---|
| 156 | event = client_connection.getEvent(); |
---|
| 157 | COUT(5) << "tick packet size " << event->packet->dataLength << std::endl; |
---|
[1735] | 158 | packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer); |
---|
[2087] | 159 | // note: packet commits suicide here except for the GameState. That is then deleted by a GamestateHandler |
---|
[1907] | 160 | bool b = packet->process(); |
---|
| 161 | assert(b); |
---|
[1502] | 162 | } |
---|
[1769] | 163 | if(gamestate.processGamestates()) |
---|
| 164 | { |
---|
[1502] | 165 | if(!isSynched_) |
---|
| 166 | isSynched_=true; |
---|
[1907] | 167 | } |
---|
[1502] | 168 | gamestate.cleanup(); |
---|
| 169 | return; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | } |
---|