[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; |
---|
[369] | 24 | pck_gen = PacketGenerator(); |
---|
| 25 | gamestate = GameStateManager(); |
---|
[229] | 26 | } |
---|
[285] | 27 | |
---|
[229] | 28 | /** |
---|
| 29 | * Constructor for the Client class |
---|
| 30 | * @param address the server address |
---|
| 31 | * @param port port of the application on the server |
---|
| 32 | */ |
---|
| 33 | Client::Client(std::string address, int port) : client_connection(port, address){ |
---|
| 34 | isConnected=false; |
---|
[369] | 35 | pck_gen = PacketGenerator(); |
---|
| 36 | gamestate = GameStateManager(); |
---|
[229] | 37 | } |
---|
[285] | 38 | |
---|
[229] | 39 | /** |
---|
| 40 | * Constructor for the Client class |
---|
| 41 | * @param address the server address |
---|
| 42 | * @param port port of the application on the server |
---|
| 43 | */ |
---|
| 44 | Client::Client(const char *address, int port) : client_connection(port, address){ |
---|
| 45 | isConnected=false; |
---|
[369] | 46 | pck_gen = PacketGenerator(); |
---|
| 47 | gamestate = GameStateManager(); |
---|
[229] | 48 | } |
---|
[285] | 49 | |
---|
[229] | 50 | /** |
---|
| 51 | * Establish the Connection to the Server |
---|
| 52 | * @return true/false |
---|
| 53 | */ |
---|
| 54 | bool Client::establishConnection(){ |
---|
| 55 | isConnected=client_connection.createConnection(); |
---|
| 56 | return isConnected; |
---|
| 57 | } |
---|
[285] | 58 | |
---|
[229] | 59 | /** |
---|
| 60 | * closes the Connection to the Server |
---|
| 61 | * @return true/false |
---|
| 62 | */ |
---|
| 63 | bool Client::closeConnection(){ |
---|
| 64 | isConnected=false; |
---|
| 65 | return client_connection.closeConnection(); |
---|
| 66 | } |
---|
[285] | 67 | |
---|
[229] | 68 | /** |
---|
| 69 | * submits a MouseAction to the server |
---|
| 70 | * @param x x Coordinate |
---|
| 71 | * @param y y Coordinate |
---|
| 72 | * @return true/false |
---|
| 73 | */ |
---|
| 74 | bool Client::sendMouse(double x, double y){ |
---|
| 75 | // generate packet and add it to the queue |
---|
| 76 | if(!client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
| 77 | return false; |
---|
| 78 | // send packets |
---|
[369] | 79 | return client_connection.sendPackets(); |
---|
[229] | 80 | } |
---|
[285] | 81 | |
---|
[229] | 82 | /** |
---|
| 83 | * submits a Keystrike to the server |
---|
| 84 | * @param key_code code to submit |
---|
| 85 | * @return true/false |
---|
| 86 | */ |
---|
| 87 | bool Client::sendKeyboard(char key_code){ |
---|
| 88 | // generate packet and add it to queue |
---|
| 89 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
| 90 | return false; |
---|
| 91 | // send packets |
---|
[369] | 92 | return client_connection.sendPackets(); |
---|
[229] | 93 | } |
---|
[285] | 94 | |
---|
[229] | 95 | /** |
---|
| 96 | * Adds a MouseAction to the PacketQueue |
---|
| 97 | * @param x x Coordinate |
---|
| 98 | * @param y y Coordinate |
---|
| 99 | * @return true/false |
---|
| 100 | */ |
---|
| 101 | bool Client::addMouse(double x, double y){ |
---|
| 102 | // generate packet and add it to the queue |
---|
| 103 | if(client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
| 104 | return true; |
---|
| 105 | else |
---|
| 106 | return false; |
---|
| 107 | } |
---|
[285] | 108 | |
---|
[229] | 109 | /** |
---|
| 110 | * Adds a Keystrike to the PacketQueue |
---|
| 111 | * @param key_code |
---|
| 112 | * @return true/false |
---|
| 113 | */ |
---|
| 114 | bool Client::addKeyboard(char key_code){ |
---|
| 115 | // generate packet and add it to queue |
---|
| 116 | if(client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
| 117 | return true; |
---|
| 118 | else |
---|
| 119 | return false; |
---|
| 120 | } |
---|
[285] | 121 | |
---|
[369] | 122 | /** |
---|
| 123 | * Sends out all the packets queued by addXXX |
---|
| 124 | */ |
---|
| 125 | bool Client::sendPackets(){ |
---|
| 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){ |
---|
[369] | 149 | gamestate.loadSnapshot( *data ); |
---|
| 150 | return; |
---|
| 151 | } |
---|
| 152 | |
---|
[400] | 153 | void Client::processClassid(classid *clid){ |
---|
[401] | 154 | orxonox::Identifier *id; |
---|
| 155 | orxonox::ID(std::string(clid->message))->setNetworkID(clid->classid); |
---|
[400] | 156 | } |
---|
| 157 | |
---|
[229] | 158 | } |
---|