Changeset 1440 for code/branches/network
- Timestamp:
- May 27, 2008, 11:23:32 AM (17 years ago)
- Location:
- code/branches/network/src/network
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/network/Client.cc
r1431 r1440 140 140 } 141 141 142 /** 143 * submits a MouseAction to the server 144 * @param x x Coordinate 145 * @param y y Coordinate 146 * @return true/false 147 */ 148 bool Client::sendMouse(double x, double y){ 149 // generate packet and add it to the queue 150 if(!isConnected) 151 return false; 152 if(!client_connection.addPacket(pck_gen.mousem(x, y))) 153 return false; 154 // send packets 155 return client_connection.sendPackets(); 156 } 157 158 /** 159 * submits a Keystrike to the server 160 * @param key_code code to submit 161 * @return true/false 162 */ 163 bool Client::sendKeyboard(char key_code){ 164 // generate packet and add it to queue 165 if(!isConnected) 166 return false; 167 if(!client_connection.addPacket(pck_gen.keystrike(key_code))) 168 return false; 169 // send packets 170 return client_connection.sendPackets(); 171 } 142 143 144 172 145 173 146 /** … … 186 159 } 187 160 188 /**189 * Adds a MouseAction to the PacketQueue190 * @param x x Coordinate191 * @param y y Coordinate192 * @return true/false193 */194 bool Client::addMouse(double x, double y){195 // generate packet and add it to the queue196 if(client_connection.addPacket(pck_gen.mousem(x, y)))197 return true;198 else199 return false;200 }201 202 /**203 * Adds a Keystrike to the PacketQueue204 * @param key_code205 * @return true/false206 */207 bool Client::addKeyboard(char key_code){208 // generate packet and add it to queue209 if(client_connection.addPacket(pck_gen.keystrike(key_code)))210 return true;211 else212 return false;213 }214 161 215 162 /** -
code/branches/network/src/network/Client.h
r1431 r1440 75 75 bool closeConnection(); 76 76 77 bool sendMouse(double x, double y);78 bool sendKeyboard(char key_code);79 77 bool sendChat( std::string message ); 80 81 bool addMouse(double x, double y); 82 bool addKeyboard(char key_code); 83 78 84 79 bool sendPackets(); 85 80 -
code/branches/network/src/network/PacketDecoder.cc
r1432 r1440 68 68 case COMMAND: 69 69 return command( packet, clientId ); 70 case MOUSE:71 mousem( packet, clientId );72 return true;73 case KEYBOARD:74 keystrike( packet, clientId );75 return true;76 70 case CHAT: 77 71 chatMessage( packet, clientId ); … … 135 129 } 136 130 137 void PacketDecoder::mousem( ENetPacket* packet, int clientId )138 {139 mouse* mouseMove = new mouse;140 //copy data of packet->data to new struct141 *mouseMove = *(mouse*)packet->data;142 143 //clean memory144 enet_packet_destroy( packet );145 printMouse( mouseMove ); //debug info146 }147 148 void PacketDecoder::keystrike( ENetPacket* packet, int clientId )149 {150 keyboard* key = new keyboard;151 *key = *(keyboard*)packet->data; //see above152 153 //clean memory154 enet_packet_destroy( packet );155 printKey( key ); //debug info156 157 }158 159 131 void PacketDecoder::chatMessage( ENetPacket* packet, int clientId ) 160 132 { … … 189 161 return; 190 162 } 191 //since it's not alowed to use void* for pointer arithmetic192 //FIXME: variable never used193 unsigned char* data = (unsigned char *)(packet->data);194 163 //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int ) 195 164 memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) ); … … 307 276 } 308 277 309 void PacketDecoder::printMouse( mouse* data )310 {311 COUT(5) << "data id: " << data->id << std::endl;312 COUT(5) << "data: " << data->x << " " << data->y << std::endl;313 }314 315 void PacketDecoder::printKey( keyboard* data )316 {317 COUT(5) << "data id: " << data->id << std::endl;318 COUT(5) << "data: " << (char)data->press << std::endl;319 }320 278 321 279 void PacketDecoder::printChat( chat* data, int clientId ) -
code/branches/network/src/network/PacketGenerator.cc
r1432 r1440 78 78 { 79 79 COUT(4) << "PacketGenerator: generating new acknowledgement, id: " << state << std::endl; 80 ack* ackreq = new ack; 80 81 ENetPacket *packet = enet_packet_create( NULL , sizeof( ack ), reliable ); 82 ack* ackreq = (ack *)packet->data; 81 83 ackreq->id = ACK; 82 84 ackreq->a = state; 83 84 ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable ); 85 delete ackreq; 85 //delete ackreq; 86 86 return packet; 87 87 } … … 103 103 } 104 104 105 /*### mouseupdates */106 ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )107 {108 COUT(4) << "PacketGenerator: generating new mouse" << std::endl;109 mouse* mousemove = new mouse;110 mousemove->id = MOUSE;111 mousemove->x = x;112 mousemove->y = y;113 114 ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable );115 delete mousemove;116 return packet;117 }118 119 /*### keystrikes updates */120 ENetPacket* PacketGenerator::keystrike( char press, int reliable )121 {122 COUT(4) << "PacketGenerator: generating new keyboard" << std::endl;123 keyboard* key = new keyboard;124 key->id = KEYBOARD;125 key->press = press;126 127 ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable );128 delete key;129 return packet;130 }131 105 132 106 /*### chat messages packet */ … … 154 128 if(totalLen==0) 155 129 return NULL; 156 unsigned char *data = new unsigned char[totalLen]; 130 ENetPacket *packet = enet_packet_create( NULL , totalLen+sizeof(uint32_t), reliable ); 131 //unsigned char *data = new unsigned char[totalLen]; 132 unsigned char *data = packet->data; 157 133 memcpy( (void*)(data), (const void*)&gid, sizeof( int ) ); //this is the enet id 158 134 memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id … … 167 143 //create an enet packet with the generated bytestream 168 144 COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl; 169 ENetPacket *packet = enet_packet_create( data , totalLen, reliable ); 170 delete[] data; 145 //delete[] data; 171 146 if(!addCRC(packet)) 172 147 COUT(3) << "could not add crc to gamestate packet" << std::endl; … … 212 187 bool PacketGenerator::addCRC( ENetPacket *packet){ 213 188 unsigned char *data = packet->data; 214 uint32_t crc32=calcCRC(data, packet->dataLength );189 uint32_t crc32=calcCRC(data, packet->dataLength-sizeof(uint32_t)); 215 190 // now append the crc to the packet data 216 int oldlength = packet->dataLength ;217 if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){191 int oldlength = packet->dataLength-sizeof(uint32_t); 192 //if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){ 218 193 memcpy(&packet->data[oldlength], &crc32, sizeof(uint32_t)); 219 194 return true; 220 }else{221 COUT(3) << "could not add crc to gamestate" << std::endl;222 return false;223 }195 //}else{ 196 // COUT(3) << "could not add crc to gamestate" << std::endl; 197 // return false; 198 //} 224 199 } 225 200 -
code/branches/network/src/network/PacketManager.h
r1360 r1440 59 59 ENetPacket* acknowledgement( int state, int reliable = 0 ); // we do not want reliability 60 60 ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE ); 61 ENetPacket* mousem( double x, double y, int reliable = ENET_PACKET_FLAG_RELIABLE );62 ENetPacket* keystrike( char press, int reliable = ENET_PACKET_FLAG_RELIABLE );63 61 ENetPacket* chatMessage( const char* message, int reliable = ENET_PACKET_FLAG_RELIABLE ); 64 62 ENetPacket* gstate( GameStateCompressed *states, int reliable = 0 ); // we do not want reliability of gamestates … … 94 92 void acknowledgement( ENetPacket* packet, int clientId = CLIENTID_CLIENT ); 95 93 bool command( ENetPacket* packet, int clientId ); 96 void mousem( ENetPacket* packet, int clientId = CLIENTID_CLIENT );97 void keystrike( ENetPacket* packet, int clientId = CLIENTID_CLIENT );98 94 void chatMessage( ENetPacket* packet, int clientId = CLIENTID_CLIENT ); 99 95 void gstate( ENetPacket* packet, int clientID = CLIENTID_CLIENT ); … … 113 109 //print functions 114 110 void printAck( ack* data ); 115 void printMouse( mouse* data );116 void printKey( keyboard* data );117 111 void printChat( chat* data, int clientId ); 118 112 void printGamestate( GameStateCompressed *data ); -
code/branches/network/src/network/PacketTypes.h
r1293 r1440 48 48 enum packet_id { 49 49 ACK, 50 MOUSE,51 50 COMMAND, 52 KEYBOARD,53 51 CHAT, 54 52 GAMESTATE , … … 104 102 }; 105 103 106 struct mouse {107 int id;108 double x;109 double y;110 };111 112 struct keyboard {113 int id;114 char press;115 };116 104 //only in this class, not PacketGenerator, used as pattern to put incoming 117 105 //bytes inside
Note: See TracChangeset
for help on using the changeset viewer.