Changeset 313
- Timestamp:
- Nov 28, 2007, 2:39:12 PM (17 years ago)
- Location:
- code/branches/network/src/network
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/network/PacketDecoder.cc
r290 r313 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: … … 53 53 { 54 54 ack* a = new ack; 55 *a = *(ack*)packet->data; 56 printAck( a ); 55 *a = *(ack*)packet->data; //press pattern of ack on new data 56 printAck( a ); //debug info 57 57 } 58 58 … … 60 60 { 61 61 mouse* mouseMove = new mouse; 62 *mouseMove = *(mouse*)packet->data; 63 printMouse( mouseMove ); 62 //copy data of packet->data to new struct 63 *mouseMove = *(mouse*)packet->data; 64 printMouse( mouseMove ); //debug info 64 65 } 65 66 … … 67 68 { 68 69 keyboard* key = new keyboard; 69 *key = *(keyboard*)packet->data; 70 printKey( key ); 70 *key = *(keyboard*)packet->data; //see above 71 printKey( key ); //debug info 71 72 } 72 73 … … 74 75 { 75 76 chat* chatting = new chat; 76 chatting->id = (int)*packet->data; 77 char* reserve = new char[packet->dataLength-4]; 78 memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-4 ); 77 chatting->id = (int)*packet->data; //first copy id into new struct 78 //since the chat message is a char*, allocate the memory needed 79 char* reserve = new char[packet->dataLength-4]; 80 //copy the transmitted bytestream into the new generated char*, 81 //note the lenght of the message is represented as "packet->dataLength-sizeof( int )" 82 memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) ); 83 //put pointer of chatting struct to the begining of the new generated char* 79 84 chatting->message = reserve; 80 printChat( chatting ); 85 printChat( chatting ); //debug info 81 86 } 82 87 … … 84 89 { 85 90 GameState* currentState = new GameState; 91 //since it's not alowed to use void* for pointer arithmetic 86 92 unsigned char* data = (unsigned char*)packet->data; 87 memcpy( (void*)&(currentState->size), (const void*)(data+sizeof( int )), sizeof(int) ); 88 memcpy( (void*)(currentState->data), (const void*)(data+2*sizeof( int )), currentState->size ); 93 //copy the gamestate id into the struct, which is located at second place data+sizeof( int ) 94 memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) ); 95 //copy the size of the gamestate data into the new gamestate struct, located at 3th 96 //position of the data stream, data+2*sizeof( int ) 97 memcpy( (void*)&(currentState->size), (const void*)(data+2*sizeof( int )), sizeof( int) ); 98 //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream 99 currentState->data = (unsigned char*)(malloc( currentState->size )); 100 //copy the gamestate data 101 memcpy( (void*)(currentState->data), (const void*)(data+3*sizeof( int )), currentState->size ); 89 102 } 90 103 … … 117 130 void PacketDecoder::printGamestate( GameState* data ) 118 131 { 132 cout << "id of gamestate: " << data->id << endl; 119 133 cout << "size of gamestate: " << data->size << endl; 120 134 } -
code/branches/network/src/network/PacketGenerator.cc
r290 r313 1 1 /* 2 2 *Class generates packets that can be send by enet 3 * ->don't read this without the class PacketDecoder, since they belong together 3 4 * 4 5 * Autor: Dumeni Manatschal … … 71 72 ENetPacket* PacketGenerator::gstate( GameState* states, int reliable ) 72 73 { 73 int* gid; *gid = GAMESTATE; 74 int totalLen = sizeof( int ) + states->size; 75 unsigned char* data = (unsigned char*)malloc( totalLen ); 76 memcpy( (void*)(data), (const void*)gid, sizeof( int ) ); 77 memcpy( (void*)(data+sizeof( int )), (const void*)states->data, states->size ); 74 int* gid; *gid = GAMESTATE; //first assign the correct enet id 75 int totalLen = 3*sizeof( int ) + states->size; //calculate the total size of the datastream memory 76 unsigned char* data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream 77 memcpy( (void*)(data), (const void*)gid, sizeof( int ) ); //this is the enet id 78 memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the gamestate id 79 //this is the size of the gamestate data, place at 3th position of the enet datastream 80 memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->size), sizeof(int)); 81 //place the gamestate data at the end of the enet datastream 82 memcpy( (void*)(data+3*sizeof( int )), (const void*)states->data, states->size ); 83 //create an enet packet with the generated bytestream 78 84 ENetPacket *packet = enet_packet_create( data , totalLen, reliable ); 79 85 -
code/branches/network/src/network/PacketManager.h
r290 r313 19 19 * class to generate packets 20 20 * 21 * Autor: Dumeni Manatschal21 * @autor: Dumeni Manatschal 22 22 * 23 23 */ … … 54 54 * class used to decode incoming packets 55 55 * 56 * Autor: Dumeni Manatschal56 * @autor: Dumeni Manatschal 57 57 * 58 58 */
Note: See TracChangeset
for help on using the changeset viewer.