| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * |
|---|
| 4 | * |
|---|
| 5 | * License notice: |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or |
|---|
| 8 | * modify it under the terms of the GNU General Public License |
|---|
| 9 | * as published by the Free Software Foundation; either version 2 |
|---|
| 10 | * of the License, or (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program; if not, write to the Free Software |
|---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 20 | * |
|---|
| 21 | * Author: |
|---|
| 22 | * Oliver Scheuss, (C) 2007 |
|---|
| 23 | * Co-authors: |
|---|
| 24 | * ... |
|---|
| 25 | * |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | // |
|---|
| 29 | // C++ Implementation: Client |
|---|
| 30 | // |
|---|
| 31 | // Description: |
|---|
| 32 | // |
|---|
| 33 | // |
|---|
| 34 | // Author: Oliver Scheuss, (C) 2007 |
|---|
| 35 | // |
|---|
| 36 | // Copyright: See COPYING file that comes with this distribution |
|---|
| 37 | // |
|---|
| 38 | // |
|---|
| 39 | |
|---|
| 40 | #include "Client.h" |
|---|
| 41 | |
|---|
| 42 | namespace network{ |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Constructor for the Client class |
|---|
| 46 | * initializes the address and the port to default localhost:NETWORK_PORT |
|---|
| 47 | */ |
|---|
| 48 | Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ |
|---|
| 49 | // set server address to localhost |
|---|
| 50 | isConnected=false; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Constructor for the Client class |
|---|
| 55 | * @param address the server address |
|---|
| 56 | * @param port port of the application on the server |
|---|
| 57 | */ |
|---|
| 58 | Client::Client(std::string address, int port) : client_connection(port, address){ |
|---|
| 59 | isConnected=false; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Constructor for the Client class |
|---|
| 64 | * @param address the server address |
|---|
| 65 | * @param port port of the application on the server |
|---|
| 66 | */ |
|---|
| 67 | Client::Client(const char *address, int port) : client_connection(port, address){ |
|---|
| 68 | isConnected=false; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Establish the Connection to the Server |
|---|
| 73 | * @return true/false |
|---|
| 74 | */ |
|---|
| 75 | bool Client::establishConnection(){ |
|---|
| 76 | isConnected=client_connection.createConnection(); |
|---|
| 77 | return isConnected; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * closes the Connection to the Server |
|---|
| 82 | * @return true/false |
|---|
| 83 | */ |
|---|
| 84 | bool Client::closeConnection(){ |
|---|
| 85 | isConnected=false; |
|---|
| 86 | return client_connection.closeConnection(); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * submits a MouseAction to the server |
|---|
| 91 | * @param x x Coordinate |
|---|
| 92 | * @param y y Coordinate |
|---|
| 93 | * @return true/false |
|---|
| 94 | */ |
|---|
| 95 | bool Client::sendMouse(double x, double y){ |
|---|
| 96 | // generate packet and add it to the queue |
|---|
| 97 | if(!isConnected) |
|---|
| 98 | return false; |
|---|
| 99 | if(!client_connection.addPacket(pck_gen.mousem(x, y))) |
|---|
| 100 | return false; |
|---|
| 101 | // send packets |
|---|
| 102 | return client_connection.sendPackets(); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * submits a Keystrike to the server |
|---|
| 107 | * @param key_code code to submit |
|---|
| 108 | * @return true/false |
|---|
| 109 | */ |
|---|
| 110 | bool Client::sendKeyboard(char key_code){ |
|---|
| 111 | // generate packet and add it to queue |
|---|
| 112 | if(!isConnected) |
|---|
| 113 | return false; |
|---|
| 114 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
|---|
| 115 | return false; |
|---|
| 116 | // send packets |
|---|
| 117 | return client_connection.sendPackets(); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * submits a chat message to the server |
|---|
| 122 | * @param message message to send |
|---|
| 123 | * @return true/false |
|---|
| 124 | */ |
|---|
| 125 | bool Client::sendChat( std::string message ){ |
|---|
| 126 | // generate packet and add it to queue |
|---|
| 127 | if(!isConnected) |
|---|
| 128 | return false; |
|---|
| 129 | if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() ))) |
|---|
| 130 | return client_connection.sendPackets(); |
|---|
| 131 | // send packets |
|---|
| 132 | return false; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | /** |
|---|
| 136 | * Adds a MouseAction to the PacketQueue |
|---|
| 137 | * @param x x Coordinate |
|---|
| 138 | * @param y y Coordinate |
|---|
| 139 | * @return true/false |
|---|
| 140 | */ |
|---|
| 141 | bool Client::addMouse(double x, double y){ |
|---|
| 142 | // generate packet and add it to the queue |
|---|
| 143 | if(client_connection.addPacket(pck_gen.mousem(x, y))) |
|---|
| 144 | return true; |
|---|
| 145 | else |
|---|
| 146 | return false; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * Adds a Keystrike to the PacketQueue |
|---|
| 151 | * @param key_code |
|---|
| 152 | * @return true/false |
|---|
| 153 | */ |
|---|
| 154 | bool Client::addKeyboard(char key_code){ |
|---|
| 155 | // generate packet and add it to queue |
|---|
| 156 | if(client_connection.addPacket(pck_gen.keystrike(key_code))) |
|---|
| 157 | return true; |
|---|
| 158 | else |
|---|
| 159 | return false; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * Sends out all the packets queued by addXXX |
|---|
| 164 | */ |
|---|
| 165 | bool Client::sendPackets(){ |
|---|
| 166 | if(!isConnected) |
|---|
| 167 | return false; |
|---|
| 168 | ENetEvent event; |
|---|
| 169 | // send packets |
|---|
| 170 | client_connection.sendPackets(&event); |
|---|
| 171 | if(event.type==ENET_EVENT_TYPE_NONE) |
|---|
| 172 | return true; |
|---|
| 173 | else |
|---|
| 174 | return false; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | * Performs a GameState update |
|---|
| 179 | */ |
|---|
| 180 | void Client::tick(float time){ |
|---|
| 181 | ENetPacket *packet; |
|---|
| 182 | // stop if the packet queue is empty |
|---|
| 183 | while(!(client_connection.queueEmpty())){ |
|---|
| 184 | packet = client_connection.getPacket(); |
|---|
| 185 | //std::cout << "tick packet size " << packet->dataLength << std::endl; |
|---|
| 186 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
|---|
| 187 | } |
|---|
| 188 | return; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | void Client::processGamestate( GameStateCompressed *data){ |
|---|
| 192 | gamestate.pushGameState(data); |
|---|
| 193 | std::cout << "received gamestate id: " << data->id << std::endl; |
|---|
| 194 | client_connection.addPacket(pck_gen.acknowledgement(data->id)); |
|---|
| 195 | client_connection.sendPackets(); |
|---|
| 196 | return; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | void Client::processClassid(classid *clid){ |
|---|
| 200 | orxonox::Identifier *id; |
|---|
| 201 | id=ID(std::string(clid->message)); |
|---|
| 202 | if(id!=NULL) |
|---|
| 203 | id->setNetworkID(clid->clid); |
|---|
| 204 | return; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | void Client::processChat( chat *data){ |
|---|
| 208 | std::cout << "Server: " << data->message << std::endl; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | } |
|---|