[1056] | 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 | */ |
---|
[514] | 28 | |
---|
[229] | 29 | // |
---|
| 30 | // C++ Implementation: Client |
---|
| 31 | // |
---|
[285] | 32 | // Description: |
---|
[229] | 33 | // |
---|
| 34 | // |
---|
[230] | 35 | // Author: Oliver Scheuss, (C) 2007 |
---|
[229] | 36 | // |
---|
| 37 | // Copyright: See COPYING file that comes with this distribution |
---|
| 38 | // |
---|
| 39 | // |
---|
| 40 | |
---|
[1062] | 41 | #include "Client.h" |
---|
[777] | 42 | #include "core/CoreIncludes.h" |
---|
[229] | 43 | |
---|
[777] | 44 | namespace network |
---|
| 45 | { |
---|
[229] | 46 | /** |
---|
[777] | 47 | * Constructor for the Client class |
---|
| 48 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
| 49 | */ |
---|
[229] | 50 | Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ |
---|
| 51 | // set server address to localhost |
---|
| 52 | isConnected=false; |
---|
| 53 | } |
---|
[285] | 54 | |
---|
[229] | 55 | /** |
---|
[777] | 56 | * Constructor for the Client class |
---|
| 57 | * @param address the server address |
---|
| 58 | * @param port port of the application on the server |
---|
| 59 | */ |
---|
[229] | 60 | Client::Client(std::string address, int port) : client_connection(port, address){ |
---|
| 61 | isConnected=false; |
---|
| 62 | } |
---|
[285] | 63 | |
---|
[229] | 64 | /** |
---|
[777] | 65 | * Constructor for the Client class |
---|
| 66 | * @param address the server address |
---|
| 67 | * @param port port of the application on the server |
---|
| 68 | */ |
---|
[229] | 69 | Client::Client(const char *address, int port) : client_connection(port, address){ |
---|
| 70 | isConnected=false; |
---|
| 71 | } |
---|
[285] | 72 | |
---|
[229] | 73 | /** |
---|
[777] | 74 | * Establish the Connection to the Server |
---|
| 75 | * @return true/false |
---|
| 76 | */ |
---|
[229] | 77 | bool Client::establishConnection(){ |
---|
| 78 | isConnected=client_connection.createConnection(); |
---|
| 79 | return isConnected; |
---|
| 80 | } |
---|
[285] | 81 | |
---|
[229] | 82 | /** |
---|
[777] | 83 | * closes the Connection to the Server |
---|
| 84 | * @return true/false |
---|
| 85 | */ |
---|
[229] | 86 | bool Client::closeConnection(){ |
---|
| 87 | isConnected=false; |
---|
| 88 | return client_connection.closeConnection(); |
---|
| 89 | } |
---|
[285] | 90 | |
---|
[229] | 91 | /** |
---|
[777] | 92 | * submits a MouseAction to the server |
---|
| 93 | * @param x x Coordinate |
---|
| 94 | * @param y y Coordinate |
---|
| 95 | * @return true/false |
---|
| 96 | */ |
---|
[229] | 97 | bool Client::sendMouse(double x, double y){ |
---|
| 98 | // generate packet and add it to the queue |
---|
[425] | 99 | if(!isConnected) |
---|
| 100 | return false; |
---|
[229] | 101 | if(!client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
[777] | 102 | return false; |
---|
[229] | 103 | // send packets |
---|
[369] | 104 | return client_connection.sendPackets(); |
---|
[229] | 105 | } |
---|
[285] | 106 | |
---|
[229] | 107 | /** |
---|
[777] | 108 | * submits a Keystrike to the server |
---|
| 109 | * @param key_code code to submit |
---|
| 110 | * @return true/false |
---|
| 111 | */ |
---|
[229] | 112 | bool Client::sendKeyboard(char key_code){ |
---|
| 113 | // generate packet and add it to queue |
---|
[425] | 114 | if(!isConnected) |
---|
| 115 | return false; |
---|
[229] | 116 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
[777] | 117 | return false; |
---|
[229] | 118 | // send packets |
---|
[369] | 119 | return client_connection.sendPackets(); |
---|
[229] | 120 | } |
---|
[514] | 121 | |
---|
[438] | 122 | /** |
---|
[777] | 123 | * submits a chat message to the server |
---|
| 124 | * @param message message to send |
---|
| 125 | * @return true/false |
---|
| 126 | */ |
---|
[439] | 127 | bool Client::sendChat( std::string message ){ |
---|
[438] | 128 | // generate packet and add it to queue |
---|
| 129 | if(!isConnected) |
---|
| 130 | return false; |
---|
[565] | 131 | if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() ))) |
---|
[448] | 132 | return client_connection.sendPackets(); |
---|
[438] | 133 | // send packets |
---|
[448] | 134 | return false; |
---|
[438] | 135 | } |
---|
[285] | 136 | |
---|
[229] | 137 | /** |
---|
[777] | 138 | * Adds a MouseAction to the PacketQueue |
---|
| 139 | * @param x x Coordinate |
---|
| 140 | * @param y y Coordinate |
---|
| 141 | * @return true/false |
---|
| 142 | */ |
---|
[229] | 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 | } |
---|
[285] | 150 | |
---|
[229] | 151 | /** |
---|
[777] | 152 | * Adds a Keystrike to the PacketQueue |
---|
| 153 | * @param key_code |
---|
| 154 | * @return true/false |
---|
| 155 | */ |
---|
[229] | 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 | } |
---|
[285] | 163 | |
---|
[514] | 164 | /** |
---|
[777] | 165 | * Sends out all the packets queued by addXXX |
---|
| 166 | */ |
---|
[369] | 167 | bool Client::sendPackets(){ |
---|
[425] | 168 | if(!isConnected) |
---|
| 169 | return false; |
---|
[369] | 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 | } |
---|
[514] | 178 | |
---|
[229] | 179 | /** |
---|
[777] | 180 | * Performs a GameState update |
---|
| 181 | */ |
---|
[459] | 182 | void Client::tick(float time){ |
---|
[369] | 183 | ENetPacket *packet; |
---|
| 184 | // stop if the packet queue is empty |
---|
[601] | 185 | while(!(client_connection.queueEmpty())){ |
---|
[369] | 186 | packet = client_connection.getPacket(); |
---|
[1021] | 187 | COUT(5) << "tick packet size " << packet->dataLength << std::endl; |
---|
[369] | 188 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
---|
| 189 | } |
---|
| 190 | return; |
---|
[229] | 191 | } |
---|
[514] | 192 | |
---|
[405] | 193 | void Client::processGamestate( GameStateCompressed *data){ |
---|
[1021] | 194 | int id = data->id; |
---|
| 195 | COUT(5) << "received gamestate id: " << data->id << std::endl; |
---|
[620] | 196 | gamestate.pushGameState(data); |
---|
[1021] | 197 | client_connection.addPacket(pck_gen.acknowledgement(id)); |
---|
[425] | 198 | client_connection.sendPackets(); |
---|
[369] | 199 | return; |
---|
| 200 | } |
---|
[514] | 201 | |
---|
[400] | 202 | void Client::processClassid(classid *clid){ |
---|
[401] | 203 | orxonox::Identifier *id; |
---|
[496] | 204 | id=ID(std::string(clid->message)); |
---|
[413] | 205 | if(id!=NULL) |
---|
[415] | 206 | id->setNetworkID(clid->clid); |
---|
[1021] | 207 | COUT(4) << "received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl; |
---|
[413] | 208 | return; |
---|
[400] | 209 | } |
---|
[514] | 210 | |
---|
[425] | 211 | void Client::processChat( chat *data){ |
---|
[1021] | 212 | COUT(0) << "Server: " << data->message << std::endl; |
---|
[425] | 213 | } |
---|
[514] | 214 | |
---|
[229] | 215 | } |
---|