[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 | } |
---|
[285] | 92 | |
---|
[229] | 93 | /** |
---|
| 94 | * Adds a MouseAction to the PacketQueue |
---|
| 95 | * @param x x Coordinate |
---|
| 96 | * @param y y Coordinate |
---|
| 97 | * @return true/false |
---|
| 98 | */ |
---|
| 99 | bool Client::addMouse(double x, double y){ |
---|
| 100 | // generate packet and add it to the queue |
---|
| 101 | if(client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
| 102 | return true; |
---|
| 103 | else |
---|
| 104 | return false; |
---|
| 105 | } |
---|
[285] | 106 | |
---|
[229] | 107 | /** |
---|
| 108 | * Adds a Keystrike to the PacketQueue |
---|
| 109 | * @param key_code |
---|
| 110 | * @return true/false |
---|
| 111 | */ |
---|
| 112 | bool Client::addKeyboard(char key_code){ |
---|
| 113 | // generate packet and add it to queue |
---|
| 114 | if(client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
| 115 | return true; |
---|
| 116 | else |
---|
| 117 | return false; |
---|
| 118 | } |
---|
[285] | 119 | |
---|
[369] | 120 | /** |
---|
| 121 | * Sends out all the packets queued by addXXX |
---|
| 122 | */ |
---|
| 123 | bool Client::sendPackets(){ |
---|
[425] | 124 | if(!isConnected) |
---|
| 125 | return false; |
---|
[369] | 126 | ENetEvent event; |
---|
| 127 | // send packets |
---|
| 128 | client_connection.sendPackets(&event); |
---|
| 129 | if(event.type==ENET_EVENT_TYPE_NONE) |
---|
| 130 | return true; |
---|
| 131 | else |
---|
| 132 | return false; |
---|
| 133 | } |
---|
| 134 | |
---|
[229] | 135 | /** |
---|
| 136 | * Performs a GameState update |
---|
| 137 | */ |
---|
| 138 | void Client::update(){ |
---|
[369] | 139 | ENetPacket *packet; |
---|
| 140 | // stop if the packet queue is empty |
---|
| 141 | while(!client_connection.queueEmpty()){ |
---|
| 142 | packet = client_connection.getPacket(); |
---|
| 143 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
---|
| 144 | } |
---|
| 145 | return; |
---|
[229] | 146 | } |
---|
[369] | 147 | |
---|
[405] | 148 | void Client::processGamestate( GameStateCompressed *data){ |
---|
[413] | 149 | gamestate.pushGameState(*data); |
---|
[425] | 150 | client_connection.addPacket(pck_gen.acknowledgement(data->id)); |
---|
| 151 | client_connection.sendPackets(); |
---|
[369] | 152 | return; |
---|
| 153 | } |
---|
| 154 | |
---|
[400] | 155 | void Client::processClassid(classid *clid){ |
---|
[401] | 156 | orxonox::Identifier *id; |
---|
[413] | 157 | id=orxonox::ID(std::string(clid->message)); |
---|
| 158 | if(id!=NULL) |
---|
[415] | 159 | id->setNetworkID(clid->clid); |
---|
[413] | 160 | return; |
---|
[400] | 161 | } |
---|
| 162 | |
---|
[425] | 163 | void Client::processChat( chat *data){ |
---|
| 164 | std::cout << "Server: " << data->message << std::endl; |
---|
| 165 | } |
---|
| 166 | |
---|
[229] | 167 | } |
---|