[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" |
---|
[1534] | 45 | #include "core/ConsoleCommand.h" |
---|
[1735] | 46 | #include "packet/Packet.h" |
---|
[1769] | 47 | // #include "packet/Acknowledgement.h" |
---|
[1502] | 48 | |
---|
| 49 | namespace network |
---|
| 50 | { |
---|
[1735] | 51 | // SetConsoleCommandShortcut(Client, chat); |
---|
[1752] | 52 | |
---|
| 53 | |
---|
[1502] | 54 | /** |
---|
| 55 | * Constructor for the Client class |
---|
| 56 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
| 57 | */ |
---|
| 58 | Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ |
---|
| 59 | // set server address to localhost |
---|
| 60 | isConnected=false; |
---|
| 61 | isSynched_=false; |
---|
| 62 | gameStateFailure_=false; |
---|
[1751] | 63 | isServer_ = false; |
---|
[1502] | 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Constructor for the Client class |
---|
| 68 | * @param address the server address |
---|
| 69 | * @param port port of the application on the server |
---|
| 70 | */ |
---|
| 71 | Client::Client(std::string address, int port) : client_connection(port, address){ |
---|
| 72 | isConnected=false; |
---|
| 73 | isSynched_=false; |
---|
| 74 | gameStateFailure_=false; |
---|
[1751] | 75 | isServer_ = false; |
---|
[1502] | 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; |
---|
[1751] | 87 | isServer_ = false; |
---|
[1502] | 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 | |
---|
[1735] | 120 | bool Client::processChat(packet::Chat *message, unsigned int clientID){ |
---|
| 121 | return message->process(); |
---|
[1534] | 122 | } |
---|
[1752] | 123 | |
---|
[1735] | 124 | /*bool Client::sendChat(packet::Chat *chat){ |
---|
| 125 | chat->process(); |
---|
| 126 | packet::Packet *p = new packet::Packet(chat); |
---|
| 127 | return p->send(); |
---|
| 128 | }*/ |
---|
[1502] | 129 | |
---|
[1752] | 130 | |
---|
[1502] | 131 | /** |
---|
| 132 | * submits a chat message to the server |
---|
| 133 | * @param message message to send |
---|
| 134 | * @return true/false |
---|
| 135 | */ |
---|
| 136 | bool Client::sendChat( std::string message ){ |
---|
| 137 | // generate packet and add it to queue |
---|
| 138 | if(!isConnected) |
---|
| 139 | return false; |
---|
[1735] | 140 | packet::Chat chat(message, 0); |
---|
| 141 | return chat.send(); |
---|
[1502] | 142 | // send packets |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * Performs a GameState update |
---|
| 147 | */ |
---|
| 148 | void Client::tick(float time){ |
---|
| 149 | // COUT(3) << "."; |
---|
| 150 | if(client_connection.isConnected() && isSynched_){ |
---|
| 151 | COUT(4) << "popping partial gamestate: " << std::endl; |
---|
[1751] | 152 | packet::Gamestate *gs = gamestate.getGamestate(); |
---|
[1502] | 153 | if(gs){ |
---|
| 154 | COUT(4) << "client tick: sending gs " << gs << std::endl; |
---|
[1735] | 155 | if( !gs->send() ) |
---|
[1502] | 156 | COUT(3) << "Problem adding partial gamestate to queue" << std::endl; |
---|
[1735] | 157 | // gs gets automatically deleted by enet callback |
---|
[1751] | 158 | } |
---|
[1502] | 159 | } |
---|
| 160 | ENetEvent *event; |
---|
| 161 | // stop if the packet queue is empty |
---|
| 162 | while(!(client_connection.queueEmpty())){ |
---|
| 163 | event = client_connection.getEvent(); |
---|
| 164 | COUT(5) << "tick packet size " << event->packet->dataLength << std::endl; |
---|
[1735] | 165 | packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer); |
---|
| 166 | assert(packet->process()); |
---|
[1502] | 167 | } |
---|
[1769] | 168 | if(gamestate.processGamestates()) |
---|
| 169 | { |
---|
[1502] | 170 | if(!isSynched_) |
---|
| 171 | isSynched_=true; |
---|
[1769] | 172 | }else |
---|
| 173 | COUT(3) << "gamestate has not been processed sucessfully" << std::endl; |
---|
[1502] | 174 | gamestate.cleanup(); |
---|
| 175 | return; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | } |
---|