Changeset 1502 for code/trunk/src/network/PacketDecoder.cc
- Timestamp:
- Jun 1, 2008, 3:54:20 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/PacketDecoder.cc
r1360 r1502 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 ); … … 90 84 return false; 91 85 } 92 86 93 87 bool PacketDecoder::testAndRemoveCRC(ENetPacket *packet){ 94 88 uint32_t submittetcrc; … … 108 102 return false; 109 103 } 110 104 111 105 // ATTENTION: TODO watch, that arguments we pass over to the processFunction gets deleted in THE PROCESSXXX function 112 106 … … 124 118 enet_packet_destroy( packet ); 125 119 } 126 120 127 121 bool PacketDecoder::command( ENetPacket* packet, int clientId ){ 128 122 int length = *(int*)((unsigned char *)packet->data+sizeof(int)); 123 if(length<=0) 124 return false; 129 125 void *data = (void *)new unsigned char[length]; 130 126 memcpy(data, (void *)(packet->data+2*sizeof(int)), length); … … 133 129 } 134 130 135 void PacketDecoder::mousem( ENetPacket* packet, int clientId )136 {137 mouse* mouseMove = new mouse;138 //copy data of packet->data to new struct139 *mouseMove = *(mouse*)packet->data;140 141 //clean memory142 enet_packet_destroy( packet );143 printMouse( mouseMove ); //debug info144 }145 146 void PacketDecoder::keystrike( ENetPacket* packet, int clientId )147 {148 keyboard* key = new keyboard;149 *key = *(keyboard*)packet->data; //see above150 151 //clean memory152 enet_packet_destroy( packet );153 printKey( key ); //debug info154 155 }156 157 131 void PacketDecoder::chatMessage( ENetPacket* packet, int clientId ) 158 132 { 159 133 chat* chatting = new chat; 134 if(packet->dataLength==4) 135 return; 160 136 chatting->id = (int)*packet->data; //first copy id into new struct 161 137 //since the chat message is a char*, allocate the memory needed … … 185 161 return; 186 162 } 187 //since it's not alowed to use void* for pointer arithmetic188 //FIXME: variable never used189 unsigned char* data = (unsigned char *)(packet->data);190 163 //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int ) 191 164 memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) ); … … 202 175 //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream 203 176 if(currentState->compsize==0) 177 { 204 178 COUT(2) << "PacketDecoder: compsize is 0" << std::endl; 179 } 205 180 // currentState->data = (unsigned char*)(malloc( currentState->compsize )); 181 if(currentState->compsize==0) 182 return; 206 183 currentState->data = new unsigned char[currentState->compsize]; 207 184 if(currentState->data==NULL) 185 { 208 186 COUT(2) << "PacketDecoder: Gamestatepacket-decoder: memory leak" << std::endl; 187 } 209 188 //copy the GameStateCompressed data 210 189 memcpy( (void*)(currentState->data), (const void*)(packet->data+5*sizeof( int ) + 2*sizeof(bool)), currentState->compsize ); … … 221 200 cid->id = ((classid *)(packet->data))->id; 222 201 cid->clid = ((classid *)(packet->data))->clid; 202 if(cid->length==0) 203 return; 223 204 // cid->message = (const char *)malloc(cid->length); 224 205 cid->message = new char[cid->length]; … … 229 210 processClassid(cid); 230 211 } 231 232 212 213 233 214 bool PacketDecoder::decodeWelcome( ENetPacket* packet, int clientID ){ 234 215 welcome *w = new welcome; … … 240 221 return processWelcome(w); 241 222 } 242 223 243 224 bool PacketDecoder::decodeConnectRequest( ENetPacket *packet, int clientID ){ 244 225 connectRequest *con = new connectRequest; … … 279 260 return; 280 261 } 281 262 282 263 bool PacketDecoder::processWelcome( welcome *w ){ 283 264 delete w; 284 265 return true; 285 266 } 286 267 287 268 bool PacketDecoder::processConnectRequest( connectRequest *con, int clientID ){ 288 269 COUT(3) << "packetdecoder: processing connectRequest" << std::endl; … … 299 280 } 300 281 301 void PacketDecoder::printMouse( mouse* data )302 {303 COUT(5) << "data id: " << data->id << std::endl;304 COUT(5) << "data: " << data->x << " " << data->y << std::endl;305 }306 307 void PacketDecoder::printKey( keyboard* data )308 {309 COUT(5) << "data id: " << data->id << std::endl;310 COUT(5) << "data: " << (char)data->press << std::endl;311 }312 282 313 283 void PacketDecoder::printChat( chat* data, int clientId ) 314 284 { 315 285 if(clientId!=CLIENTID_CLIENT) 286 { 316 287 COUT(5) << "client: " << clientId << std::endl; 288 } 317 289 COUT(5) << "data id: " << data->id << std::endl; 318 290 COUT(5) << "data: " << data->message << std::endl;
Note: See TracChangeset
for help on using the changeset viewer.