Changeset 332 for code/branches/merger/src/network/PacketDecoder.cc
- Timestamp:
- Nov 28, 2007, 5:20:23 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/merger/src/network/PacketDecoder.cc
r278 r332 1 1 /* 2 2 * Class contains functions to determine and decode incomming packages 3 * ->don't read this without the class PacketGenerator, since they belong together 3 4 * 4 5 * Autor: Dumeni Manatschal 5 6 * 6 7 */ 7 8 8 9 9 #include "enet/enet.h" … … 21 21 { 22 22 int client = clientId; 23 cout << "clientId: " << client << endl; 24 int id = (int)*packet->data; 23 cout << "clientId: " << client << endl; //control cout, not important, just debugging info 24 int id = (int)*packet->data; //the first 4 bytes are always the enet packet id 25 25 switch( id ) { 26 26 case ACK: … … 40 40 return true; 41 41 break; 42 case GAMESTATE: 43 gstate( packet ); 44 return true; 45 break; 42 46 } 43 47 return false; … … 49 53 { 50 54 ack* a = new ack; 51 *a = *(ack*)packet->data; 52 printAck( a ); 55 *a = *(ack*)packet->data; //press pattern of ack on new data 56 57 //clean memory 58 enet_packet_destroy( packet ); 59 60 printAck( a ); //debug info 53 61 } 54 62 … … 56 64 { 57 65 mouse* mouseMove = new mouse; 58 *mouseMove = *(mouse*)packet->data; 59 printMouse( mouseMove ); 66 //copy data of packet->data to new struct 67 *mouseMove = *(mouse*)packet->data; 68 69 //clean memory 70 enet_packet_destroy( packet ); 71 72 printMouse( mouseMove ); //debug info 60 73 } 61 74 … … 63 76 { 64 77 keyboard* key = new keyboard; 65 *key = *(keyboard*)packet->data; 66 printKey( key ); 78 *key = *(keyboard*)packet->data; //see above 79 80 //clean memory 81 enet_packet_destroy( packet ); 82 83 printKey( key ); //debug info 84 67 85 } 68 86 … … 70 88 { 71 89 chat* chatting = new chat; 72 chatting->id = (int)*packet->data; 73 char* reserve = new char[packet->dataLength-4]; 74 memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-4 ); 90 chatting->id = (int)*packet->data; //first copy id into new struct 91 //since the chat message is a char*, allocate the memory needed 92 char* reserve = new char[packet->dataLength-4]; 93 //copy the transmitted bytestream into the new generated char*, 94 //note the lenght of the message is represented as "packet->dataLength-sizeof( int )" 95 memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) ); 96 //put pointer of chatting struct to the begining of the new generated char* 75 97 chatting->message = reserve; 76 printChat( chatting ); 98 99 //clean memory 100 enet_packet_destroy( packet ); 101 102 printChat( chatting ); //debug info 103 104 } 105 106 void PacketDecoder::gstate( ENetPacket* packet ) 107 { 108 GameState* currentState = new GameState; 109 //since it's not alowed to use void* for pointer arithmetic 110 unsigned char* data = (unsigned char*)packet->data; 111 //copy the gamestate id into the struct, which is located at second place data+sizeof( int ) 112 memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) ); 113 //copy the size of the gamestate data into the new gamestate struct, located at 3th 114 //position of the data stream, data+2*sizeof( int ) 115 memcpy( (void*)&(currentState->size), (const void*)(data+2*sizeof( int )), sizeof( int) ); 116 //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream 117 currentState->data = (unsigned char*)(malloc( currentState->size )); 118 //copy the gamestate data 119 memcpy( (void*)(currentState->data), (const void*)(data+3*sizeof( int )), currentState->size ); 120 121 //clean memory 122 enet_packet_destroy( packet ); 77 123 } 78 124 … … 100 146 { 101 147 cout << "data id: " << data->id << endl; 102 cout << "blablabla" << endl;103 148 cout << "data: " << data->message << endl; 104 149 } 150 151 void PacketDecoder::printGamestate( GameState* data ) 152 { 153 cout << "id of gamestate: " << data->id << endl; 154 cout << "size of gamestate: " << data->size << endl; 155 }
Note: See TracChangeset
for help on using the changeset viewer.