[1168] | 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" |
---|
| 42 | #include "core/CoreIncludes.h" |
---|
| 43 | |
---|
| 44 | namespace network |
---|
| 45 | { |
---|
| 46 | /** |
---|
| 47 | * Constructor for the Client class |
---|
| 48 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
| 49 | */ |
---|
| 50 | Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ |
---|
| 51 | // set server address to localhost |
---|
| 52 | isConnected=false; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Constructor for the Client class |
---|
| 57 | * @param address the server address |
---|
| 58 | * @param port port of the application on the server |
---|
| 59 | */ |
---|
| 60 | Client::Client(std::string address, int port) : client_connection(port, address){ |
---|
| 61 | isConnected=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(const char *address, int port) : client_connection(port, address){ |
---|
| 70 | isConnected=false; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Establish the Connection to the Server |
---|
| 75 | * @return true/false |
---|
| 76 | */ |
---|
| 77 | bool Client::establishConnection(){ |
---|
| 78 | isConnected=client_connection.createConnection(); |
---|
| 79 | return isConnected; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * closes the Connection to the Server |
---|
| 84 | * @return true/false |
---|
| 85 | */ |
---|
| 86 | bool Client::closeConnection(){ |
---|
| 87 | isConnected=false; |
---|
| 88 | return client_connection.closeConnection(); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * submits a MouseAction to the server |
---|
| 93 | * @param x x Coordinate |
---|
| 94 | * @param y y Coordinate |
---|
| 95 | * @return true/false |
---|
| 96 | */ |
---|
| 97 | bool Client::sendMouse(double x, double y){ |
---|
| 98 | // generate packet and add it to the queue |
---|
| 99 | if(!isConnected) |
---|
| 100 | return false; |
---|
| 101 | if(!client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
| 102 | return false; |
---|
| 103 | // send packets |
---|
| 104 | return client_connection.sendPackets(); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * submits a Keystrike to the server |
---|
| 109 | * @param key_code code to submit |
---|
| 110 | * @return true/false |
---|
| 111 | */ |
---|
| 112 | bool Client::sendKeyboard(char key_code){ |
---|
| 113 | // generate packet and add it to queue |
---|
| 114 | if(!isConnected) |
---|
| 115 | return false; |
---|
| 116 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
| 117 | return false; |
---|
| 118 | // send packets |
---|
| 119 | return client_connection.sendPackets(); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * submits a chat message to the server |
---|
| 124 | * @param message message to send |
---|
| 125 | * @return true/false |
---|
| 126 | */ |
---|
| 127 | bool Client::sendChat( std::string message ){ |
---|
| 128 | // generate packet and add it to queue |
---|
| 129 | if(!isConnected) |
---|
| 130 | return false; |
---|
| 131 | if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() ))) |
---|
| 132 | return client_connection.sendPackets(); |
---|
| 133 | // send packets |
---|
| 134 | return false; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * Adds a MouseAction to the PacketQueue |
---|
| 139 | * @param x x Coordinate |
---|
| 140 | * @param y y Coordinate |
---|
| 141 | * @return true/false |
---|
| 142 | */ |
---|
| 143 | bool Client::addMouse(double x, double y){ |
---|
| 144 | // generate packet and add it to the queue |
---|
| 145 | if(client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
| 146 | return true; |
---|
| 147 | else |
---|
| 148 | return false; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Adds a Keystrike to the PacketQueue |
---|
| 153 | * @param key_code |
---|
| 154 | * @return true/false |
---|
| 155 | */ |
---|
| 156 | bool Client::addKeyboard(char key_code){ |
---|
| 157 | // generate packet and add it to queue |
---|
| 158 | if(client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
| 159 | return true; |
---|
| 160 | else |
---|
| 161 | return false; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | * Sends out all the packets queued by addXXX |
---|
| 166 | */ |
---|
| 167 | bool Client::sendPackets(){ |
---|
| 168 | if(!isConnected) |
---|
| 169 | return false; |
---|
| 170 | ENetEvent event; |
---|
| 171 | // send packets |
---|
| 172 | client_connection.sendPackets(&event); |
---|
| 173 | if(event.type==ENET_EVENT_TYPE_NONE) |
---|
| 174 | return true; |
---|
| 175 | else |
---|
| 176 | return false; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * Performs a GameState update |
---|
| 181 | */ |
---|
| 182 | void Client::tick(float time){ |
---|
| 183 | ENetPacket *packet; |
---|
| 184 | // stop if the packet queue is empty |
---|
| 185 | while(!(client_connection.queueEmpty())){ |
---|
| 186 | packet = client_connection.getPacket(); |
---|
| 187 | COUT(5) << "tick packet size " << packet->dataLength << std::endl; |
---|
| 188 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
---|
| 189 | } |
---|
| 190 | return; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | void Client::processGamestate( GameStateCompressed *data){ |
---|
| 194 | int id = data->id; |
---|
| 195 | COUT(5) << "received gamestate id: " << data->id << std::endl; |
---|
| 196 | gamestate.pushGameState(data); |
---|
| 197 | client_connection.addPacket(pck_gen.acknowledgement(id)); |
---|
| 198 | client_connection.sendPackets(); |
---|
| 199 | return; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | void Client::processClassid(classid *clid){ |
---|
| 203 | orxonox::Identifier *id; |
---|
| 204 | id=ID(std::string(clid->message)); |
---|
| 205 | if(id!=NULL) |
---|
| 206 | id->setNetworkID(clid->clid); |
---|
| 207 | COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl; |
---|
| 208 | return; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void Client::processChat( chat *data){ |
---|
| 212 | COUT(0) << "Server: " << data->message << std::endl; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | } |
---|