| 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 |     isConnected=false; | 
|---|
| 24 |   } | 
|---|
| 25 |  | 
|---|
| 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 |   } | 
|---|
| 34 |  | 
|---|
| 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 |   } | 
|---|
| 43 |  | 
|---|
| 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 |   } | 
|---|
| 52 |  | 
|---|
| 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 |   } | 
|---|
| 61 |  | 
|---|
| 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(!isConnected) | 
|---|
| 71 |       return false; | 
|---|
| 72 |     if(!client_connection.addPacket(pck_gen.mousem(x, y))) | 
|---|
| 73 |         return false; | 
|---|
| 74 |     // send packets | 
|---|
| 75 |     return client_connection.sendPackets(); | 
|---|
| 76 |   } | 
|---|
| 77 |  | 
|---|
| 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 | 
|---|
| 85 |     if(!isConnected) | 
|---|
| 86 |       return false; | 
|---|
| 87 |     if(!client_connection.addPacket(pck_gen.keystrike(key_code))) | 
|---|
| 88 |         return false; | 
|---|
| 89 |     // send packets | 
|---|
| 90 |     return client_connection.sendPackets(); | 
|---|
| 91 |   } | 
|---|
| 92 |    | 
|---|
| 93 |   /** | 
|---|
| 94 |    * submits a chat message to the server | 
|---|
| 95 |    * @param message message to send | 
|---|
| 96 |    * @return true/false | 
|---|
| 97 |    */ | 
|---|
| 98 |   bool Client::sendChat( std::string message ){ | 
|---|
| 99 |     // generate packet and add it to queue | 
|---|
| 100 |     if(!isConnected) | 
|---|
| 101 |       return false; | 
|---|
| 102 |     if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() ))); | 
|---|
| 103 |       return client_connection.sendPackets(); | 
|---|
| 104 |     // send packets | 
|---|
| 105 |     return false; | 
|---|
| 106 |   } | 
|---|
| 107 |  | 
|---|
| 108 |   /** | 
|---|
| 109 |    * Adds a MouseAction to the PacketQueue | 
|---|
| 110 |    * @param x x Coordinate | 
|---|
| 111 |    * @param y y Coordinate | 
|---|
| 112 |    * @return true/false | 
|---|
| 113 |    */ | 
|---|
| 114 |   bool Client::addMouse(double x, double y){ | 
|---|
| 115 |     // generate packet and add it to the queue | 
|---|
| 116 |     if(client_connection.addPacket(pck_gen.mousem(x, y))) | 
|---|
| 117 |       return true; | 
|---|
| 118 |     else | 
|---|
| 119 |       return false; | 
|---|
| 120 |   } | 
|---|
| 121 |  | 
|---|
| 122 |   /** | 
|---|
| 123 |    * Adds a Keystrike to the PacketQueue | 
|---|
| 124 |    * @param key_code | 
|---|
| 125 |    * @return true/false | 
|---|
| 126 |    */ | 
|---|
| 127 |   bool Client::addKeyboard(char key_code){ | 
|---|
| 128 |     // generate packet and add it to queue | 
|---|
| 129 |     if(client_connection.addPacket(pck_gen.keystrike(key_code))) | 
|---|
| 130 |       return true; | 
|---|
| 131 |     else | 
|---|
| 132 |       return false; | 
|---|
| 133 |   } | 
|---|
| 134 |  | 
|---|
| 135 |   /**  | 
|---|
| 136 |    * Sends out all the packets queued by addXXX | 
|---|
| 137 |    */ | 
|---|
| 138 |   bool Client::sendPackets(){ | 
|---|
| 139 |     if(!isConnected) | 
|---|
| 140 |       return false; | 
|---|
| 141 |     ENetEvent event; | 
|---|
| 142 |     // send packets | 
|---|
| 143 |     client_connection.sendPackets(&event); | 
|---|
| 144 |     if(event.type==ENET_EVENT_TYPE_NONE) | 
|---|
| 145 |       return true; | 
|---|
| 146 |     else | 
|---|
| 147 |       return false; | 
|---|
| 148 |   } | 
|---|
| 149 |    | 
|---|
| 150 |   /** | 
|---|
| 151 |    * Performs a GameState update | 
|---|
| 152 |    */ | 
|---|
| 153 |   void Client::tick(float time){ | 
|---|
| 154 |     ENetPacket *packet; | 
|---|
| 155 |     // stop if the packet queue is empty | 
|---|
| 156 |     while(!client_connection.queueEmpty()){ | 
|---|
| 157 |       packet = client_connection.getPacket(); | 
|---|
| 158 |       elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) | 
|---|
| 159 |     } | 
|---|
| 160 |     return; | 
|---|
| 161 |   } | 
|---|
| 162 |    | 
|---|
| 163 |   void Client::processGamestate( GameStateCompressed *data){ | 
|---|
| 164 |     gamestate.pushGameState(*data); | 
|---|
| 165 |     client_connection.addPacket(pck_gen.acknowledgement(data->id)); | 
|---|
| 166 |     client_connection.sendPackets(); | 
|---|
| 167 |     return; | 
|---|
| 168 |   } | 
|---|
| 169 |    | 
|---|
| 170 |   void Client::processClassid(classid *clid){ | 
|---|
| 171 |     orxonox::Identifier *id; | 
|---|
| 172 |     id=orxonox::ID(std::string(clid->message)); | 
|---|
| 173 |     if(id!=NULL) | 
|---|
| 174 |       id->setNetworkID(clid->clid); | 
|---|
| 175 |     return; | 
|---|
| 176 |   } | 
|---|
| 177 |    | 
|---|
| 178 |   void Client::processChat( chat *data){ | 
|---|
| 179 |     std::cout << "Server: " << data->message << std::endl; | 
|---|
| 180 |   } | 
|---|
| 181 |    | 
|---|
| 182 | } | 
|---|