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