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