[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 |
---|
| 70 | if(!client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
| 71 | return false; |
---|
| 72 | // send packets |
---|
[369] | 73 | return client_connection.sendPackets(); |
---|
[229] | 74 | } |
---|
[285] | 75 | |
---|
[229] | 76 | /** |
---|
| 77 | * submits a Keystrike to the server |
---|
| 78 | * @param key_code code to submit |
---|
| 79 | * @return true/false |
---|
| 80 | */ |
---|
| 81 | bool Client::sendKeyboard(char key_code){ |
---|
| 82 | // generate packet and add it to queue |
---|
| 83 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
| 84 | return false; |
---|
| 85 | // send packets |
---|
[369] | 86 | return client_connection.sendPackets(); |
---|
[229] | 87 | } |
---|
[285] | 88 | |
---|
[229] | 89 | /** |
---|
| 90 | * Adds a MouseAction to the PacketQueue |
---|
| 91 | * @param x x Coordinate |
---|
| 92 | * @param y y Coordinate |
---|
| 93 | * @return true/false |
---|
| 94 | */ |
---|
| 95 | bool Client::addMouse(double x, double y){ |
---|
| 96 | // generate packet and add it to the queue |
---|
| 97 | if(client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
| 98 | return true; |
---|
| 99 | else |
---|
| 100 | return false; |
---|
| 101 | } |
---|
[285] | 102 | |
---|
[229] | 103 | /** |
---|
| 104 | * Adds a Keystrike to the PacketQueue |
---|
| 105 | * @param key_code |
---|
| 106 | * @return true/false |
---|
| 107 | */ |
---|
| 108 | bool Client::addKeyboard(char key_code){ |
---|
| 109 | // generate packet and add it to queue |
---|
| 110 | if(client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
| 111 | return true; |
---|
| 112 | else |
---|
| 113 | return false; |
---|
| 114 | } |
---|
[285] | 115 | |
---|
[369] | 116 | /** |
---|
| 117 | * Sends out all the packets queued by addXXX |
---|
| 118 | */ |
---|
| 119 | bool Client::sendPackets(){ |
---|
| 120 | ENetEvent event; |
---|
| 121 | // send packets |
---|
| 122 | client_connection.sendPackets(&event); |
---|
| 123 | if(event.type==ENET_EVENT_TYPE_NONE) |
---|
| 124 | return true; |
---|
| 125 | else |
---|
| 126 | return false; |
---|
| 127 | } |
---|
| 128 | |
---|
[229] | 129 | /** |
---|
| 130 | * Performs a GameState update |
---|
| 131 | */ |
---|
| 132 | void Client::update(){ |
---|
[369] | 133 | ENetPacket *packet; |
---|
| 134 | // stop if the packet queue is empty |
---|
| 135 | while(!client_connection.queueEmpty()){ |
---|
| 136 | packet = client_connection.getPacket(); |
---|
| 137 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
---|
| 138 | } |
---|
| 139 | return; |
---|
[229] | 140 | } |
---|
[369] | 141 | |
---|
[405] | 142 | void Client::processGamestate( GameStateCompressed *data){ |
---|
[413] | 143 | gamestate.pushGameState(*data); |
---|
[369] | 144 | return; |
---|
| 145 | } |
---|
| 146 | |
---|
[400] | 147 | void Client::processClassid(classid *clid){ |
---|
[401] | 148 | orxonox::Identifier *id; |
---|
[413] | 149 | id=orxonox::ID(std::string(clid->message)); |
---|
| 150 | if(id!=NULL) |
---|
[415] | 151 | id->setNetworkID(clid->clid); |
---|
[413] | 152 | return; |
---|
[400] | 153 | } |
---|
| 154 | |
---|
[229] | 155 | } |
---|