[514] | 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 | |
---|
[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 | |
---|
[285] | 40 | #include "Client.h" |
---|
[229] | 41 | |
---|
| 42 | namespace network{ |
---|
[285] | 43 | |
---|
[229] | 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 | } |
---|
[285] | 52 | |
---|
[229] | 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 | } |
---|
[285] | 61 | |
---|
[229] | 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 | } |
---|
[285] | 70 | |
---|
[229] | 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 | } |
---|
[285] | 79 | |
---|
[229] | 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 | } |
---|
[285] | 88 | |
---|
[229] | 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 |
---|
[425] | 97 | if(!isConnected) |
---|
| 98 | return false; |
---|
[229] | 99 | if(!client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
| 100 | return false; |
---|
| 101 | // send packets |
---|
[369] | 102 | return client_connection.sendPackets(); |
---|
[229] | 103 | } |
---|
[285] | 104 | |
---|
[229] | 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 |
---|
[425] | 112 | if(!isConnected) |
---|
| 113 | return false; |
---|
[229] | 114 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
| 115 | return false; |
---|
| 116 | // send packets |
---|
[369] | 117 | return client_connection.sendPackets(); |
---|
[229] | 118 | } |
---|
[514] | 119 | |
---|
[438] | 120 | /** |
---|
| 121 | * submits a chat message to the server |
---|
| 122 | * @param message message to send |
---|
| 123 | * @return true/false |
---|
| 124 | */ |
---|
[439] | 125 | bool Client::sendChat( std::string message ){ |
---|
[438] | 126 | // generate packet and add it to queue |
---|
| 127 | if(!isConnected) |
---|
| 128 | return false; |
---|
[565] | 129 | if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() ))) |
---|
[448] | 130 | return client_connection.sendPackets(); |
---|
[438] | 131 | // send packets |
---|
[448] | 132 | return false; |
---|
[438] | 133 | } |
---|
[285] | 134 | |
---|
[229] | 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 | } |
---|
[285] | 148 | |
---|
[229] | 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 | } |
---|
[285] | 161 | |
---|
[514] | 162 | /** |
---|
[369] | 163 | * Sends out all the packets queued by addXXX |
---|
| 164 | */ |
---|
| 165 | bool Client::sendPackets(){ |
---|
[425] | 166 | if(!isConnected) |
---|
| 167 | return false; |
---|
[369] | 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 | } |
---|
[514] | 176 | |
---|
[229] | 177 | /** |
---|
| 178 | * Performs a GameState update |
---|
| 179 | */ |
---|
[459] | 180 | void Client::tick(float time){ |
---|
[369] | 181 | ENetPacket *packet; |
---|
| 182 | // stop if the packet queue is empty |
---|
| 183 | while(!client_connection.queueEmpty()){ |
---|
| 184 | packet = client_connection.getPacket(); |
---|
| 185 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
---|
| 186 | } |
---|
| 187 | return; |
---|
[229] | 188 | } |
---|
[514] | 189 | |
---|
[405] | 190 | void Client::processGamestate( GameStateCompressed *data){ |
---|
[413] | 191 | gamestate.pushGameState(*data); |
---|
[425] | 192 | client_connection.addPacket(pck_gen.acknowledgement(data->id)); |
---|
| 193 | client_connection.sendPackets(); |
---|
[369] | 194 | return; |
---|
| 195 | } |
---|
[514] | 196 | |
---|
[400] | 197 | void Client::processClassid(classid *clid){ |
---|
[401] | 198 | orxonox::Identifier *id; |
---|
[496] | 199 | id=ID(std::string(clid->message)); |
---|
[413] | 200 | if(id!=NULL) |
---|
[415] | 201 | id->setNetworkID(clid->clid); |
---|
[413] | 202 | return; |
---|
[400] | 203 | } |
---|
[514] | 204 | |
---|
[425] | 205 | void Client::processChat( chat *data){ |
---|
| 206 | std::cout << "Server: " << data->message << std::endl; |
---|
| 207 | } |
---|
[514] | 208 | |
---|
[229] | 209 | } |
---|