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