| 1 | // | 
|---|
| 2 | // C++ Implementation: Client | 
|---|
| 3 | // | 
|---|
| 4 | // Description: | 
|---|
| 5 | // | 
|---|
| 6 | // | 
|---|
| 7 | // Author:  Oliver Scheuss, (C) 2007 | 
|---|
| 8 | // | 
|---|
| 9 | // Copyright: See COPYING file that comes with this distribution | 
|---|
| 10 | // | 
|---|
| 11 | // | 
|---|
| 12 |  | 
|---|
| 13 | #include "Client.h" | 
|---|
| 14 |  | 
|---|
| 15 | namespace network{ | 
|---|
| 16 |  | 
|---|
| 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 |     //server_address="127.0.0.1"; | 
|---|
| 24 |     //port = NETWORK_PORT; | 
|---|
| 25 |     isConnected=false; | 
|---|
| 26 |   } | 
|---|
| 27 |  | 
|---|
| 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 |     //server_address=address.c_str(); | 
|---|
| 35 |     //this->port = port; | 
|---|
| 36 |     isConnected=false; | 
|---|
| 37 |   } | 
|---|
| 38 |  | 
|---|
| 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 |     //server_address = address; | 
|---|
| 46 |     //this->port = port; | 
|---|
| 47 |     isConnected=false; | 
|---|
| 48 |   } | 
|---|
| 49 |  | 
|---|
| 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 |   } | 
|---|
| 58 |  | 
|---|
| 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 |   } | 
|---|
| 67 |  | 
|---|
| 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 | 
|---|
| 80 |     client_connection.sendPackets(&event); | 
|---|
| 81 |     if(event.type==ENET_EVENT_TYPE_NONE) | 
|---|
| 82 |       return true; | 
|---|
| 83 |     else | 
|---|
| 84 |       return false; | 
|---|
| 85 |   } | 
|---|
| 86 |  | 
|---|
| 87 |   /** | 
|---|
| 88 |    * submits a Keystrike to the server | 
|---|
| 89 |    * @param key_code code to submit | 
|---|
| 90 |    * @return true/false | 
|---|
| 91 |    */ | 
|---|
| 92 |   bool Client::sendKeyboard(char key_code){ | 
|---|
| 93 |     ENetEvent event; | 
|---|
| 94 |     // generate packet and add it to queue | 
|---|
| 95 |     if(!client_connection.addPacket(pck_gen.keystrike(key_code))) | 
|---|
| 96 |         return false; | 
|---|
| 97 |     // send packets | 
|---|
| 98 |     client_connection.sendPackets(&event); | 
|---|
| 99 |     if(event.type==ENET_EVENT_TYPE_NONE) | 
|---|
| 100 |       return true; | 
|---|
| 101 |     else | 
|---|
| 102 |       return false; | 
|---|
| 103 |   } | 
|---|
| 104 |  | 
|---|
| 105 |   /** | 
|---|
| 106 |    * Adds a MouseAction to the PacketQueue | 
|---|
| 107 |    * @param x x Coordinate | 
|---|
| 108 |    * @param y y Coordinate | 
|---|
| 109 |    * @return true/false | 
|---|
| 110 |    */ | 
|---|
| 111 |   bool Client::addMouse(double x, double y){ | 
|---|
| 112 |     // generate packet and add it to the queue | 
|---|
| 113 |     if(client_connection.addPacket(pck_gen.mousem(x, y))) | 
|---|
| 114 |       return true; | 
|---|
| 115 |     else | 
|---|
| 116 |       return false; | 
|---|
| 117 |   } | 
|---|
| 118 |  | 
|---|
| 119 |   /** | 
|---|
| 120 |    * Adds a Keystrike to the PacketQueue | 
|---|
| 121 |    * @param key_code | 
|---|
| 122 |    * @return true/false | 
|---|
| 123 |    */ | 
|---|
| 124 |   bool Client::addKeyboard(char key_code){ | 
|---|
| 125 |     ENetEvent event; | 
|---|
| 126 |     // generate packet and add it to queue | 
|---|
| 127 |     if(client_connection.addPacket(pck_gen.keystrike(key_code))) | 
|---|
| 128 |       return true; | 
|---|
| 129 |     else | 
|---|
| 130 |       return false; | 
|---|
| 131 |   } | 
|---|
| 132 |  | 
|---|
| 133 |   /** | 
|---|
| 134 |    * Performs a GameState update | 
|---|
| 135 |    */ | 
|---|
| 136 |   void Client::update(){ | 
|---|
| 137 |     // to be implemented ================== | 
|---|
| 138 |  | 
|---|
| 139 |  | 
|---|
| 140 |   } | 
|---|
| 141 |  | 
|---|
| 142 | } | 
|---|