[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" |
---|
[1666] | 42 | #include "Host.h" |
---|
[1502] | 43 | #include "Synchronisable.h" |
---|
| 44 | #include "core/CoreIncludes.h" |
---|
[1534] | 45 | #include "core/ConsoleCommand.h" |
---|
[1666] | 46 | #include "packet/Packet.h" |
---|
[1705] | 47 | #include "packet/Acknowledgement.h" |
---|
[1502] | 48 | |
---|
| 49 | namespace network |
---|
| 50 | { |
---|
[1666] | 51 | // SetConsoleCommandShortcut(Client, chat); |
---|
[1534] | 52 | |
---|
[1502] | 53 | |
---|
| 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; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Constructor for the Client class |
---|
| 67 | * @param address the server address |
---|
| 68 | * @param port port of the application on the server |
---|
| 69 | */ |
---|
| 70 | Client::Client(std::string address, int port) : client_connection(port, address){ |
---|
| 71 | isConnected=false; |
---|
| 72 | isSynched_=false; |
---|
| 73 | gameStateFailure_=false; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * Constructor for the Client class |
---|
| 78 | * @param address the server address |
---|
| 79 | * @param port port of the application on the server |
---|
| 80 | */ |
---|
| 81 | Client::Client(const char *address, int port) : client_connection(port, address){ |
---|
| 82 | isConnected=false; |
---|
| 83 | isSynched_=false; |
---|
| 84 | gameStateFailure_=false; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | Client::~Client(){ |
---|
| 88 | if(isConnected) |
---|
| 89 | closeConnection(); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Establish the Connection to the Server |
---|
| 94 | * @return true/false |
---|
| 95 | */ |
---|
| 96 | bool Client::establishConnection(){ |
---|
| 97 | Synchronisable::setClient(true); |
---|
| 98 | isConnected=client_connection.createConnection(); |
---|
| 99 | if(isConnected){ |
---|
| 100 | // COUT(3) << "sending connectrequest" << std::endl; |
---|
| 101 | // if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets()) |
---|
| 102 | // COUT(1) << "could not send connection request !!!!!!!!!" << std::endl; |
---|
| 103 | }else |
---|
| 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 | |
---|
[1666] | 117 | bool Client::queuePacket(ENetPacket *packet, int clientID){ |
---|
| 118 | return client_connection.addPacket(packet); |
---|
| 119 | } |
---|
[1502] | 120 | |
---|
[1666] | 121 | bool Client::processChat(packet::Chat *message, unsigned int clientID){ |
---|
| 122 | return message->process(); |
---|
[1534] | 123 | } |
---|
[1502] | 124 | |
---|
[1666] | 125 | bool Client::sendChat(packet::Chat *chat){ |
---|
| 126 | chat->process(); |
---|
| 127 | packet::Packet *p = new packet::Packet(chat); |
---|
| 128 | return p->send(); |
---|
| 129 | } |
---|
[1705] | 130 | |
---|
[1502] | 131 | |
---|
| 132 | /** |
---|
| 133 | * submits a chat message to the server |
---|
| 134 | * @param message message to send |
---|
| 135 | * @return true/false |
---|
| 136 | */ |
---|
| 137 | bool Client::sendChat( std::string message ){ |
---|
| 138 | // generate packet and add it to queue |
---|
| 139 | if(!isConnected) |
---|
| 140 | return false; |
---|
[1705] | 141 | packet::Packet p(new packet::Chat(message, clientID_)); |
---|
| 142 | return p.send(); |
---|
[1502] | 143 | //return client_connection.sendPackets(); |
---|
| 144 | // send packets |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * Performs a GameState update |
---|
| 149 | */ |
---|
| 150 | void Client::tick(float time){ |
---|
| 151 | // COUT(3) << "."; |
---|
| 152 | if(client_connection.isConnected() && isSynched_){ |
---|
| 153 | COUT(4) << "popping partial gamestate: " << std::endl; |
---|
[1705] | 154 | packet::Gamestate *gs = gamestate.getGamestate(); |
---|
[1502] | 155 | if(gs){ |
---|
| 156 | COUT(4) << "client tick: sending gs " << gs << std::endl; |
---|
[1710] | 157 | packet::Packet packet(gs); |
---|
| 158 | if( !packet.send() ) |
---|
[1502] | 159 | COUT(3) << "Problem adding partial gamestate to queue" << std::endl; |
---|
| 160 | // now delete it to save memory |
---|
| 161 | delete gs; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | ENetEvent *event; |
---|
| 165 | // stop if the packet queue is empty |
---|
| 166 | while(!(client_connection.queueEmpty())){ |
---|
| 167 | event = client_connection.getEvent(); |
---|
| 168 | COUT(5) << "tick packet size " << event->packet->dataLength << std::endl; |
---|
[1705] | 169 | packet::Packet packet(event->packet, event->peer); |
---|
| 170 | assert(packet.getPacketContent()->process()); |
---|
[1502] | 171 | } |
---|
[1705] | 172 | int gameStateID = gamestate.processGamestates(); |
---|
[1502] | 173 | if(gameStateID==GAMESTATEID_INITIAL) |
---|
| 174 | if(gameStateFailure_){ |
---|
[1710] | 175 | packet::Acknowledgement ack(GAMESTATEID_INITIAL, 0); |
---|
| 176 | packet::Packet packet(&ack); |
---|
[1705] | 177 | if(!packet.send()) |
---|
[1502] | 178 | COUT(3) << "could not (negatively) ack gamestate" << std::endl; |
---|
| 179 | else |
---|
| 180 | COUT(4) << "negatively acked a gamestate" << std::endl; |
---|
| 181 | } |
---|
| 182 | else |
---|
| 183 | gameStateFailure_=true; |
---|
| 184 | else if(gameStateID!=0){ |
---|
| 185 | // ack gamestate and set synched |
---|
| 186 | if(!isSynched_) |
---|
| 187 | isSynched_=true; |
---|
| 188 | gameStateFailure_=false; |
---|
[1710] | 189 | packet::Acknowledgement ack(gameStateID, 0); |
---|
| 190 | packet::Packet packet(&ack); |
---|
[1705] | 191 | if(!packet.send()) |
---|
[1502] | 192 | COUT(3) << "could not ack gamestate" << std::endl; |
---|
| 193 | }// otherwise we had no gamestate to load |
---|
| 194 | gamestate.cleanup(); |
---|
| 195 | return; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | } |
---|