[223] | 1 | /* |
---|
| 2 | * Class contains functions to determine and decode incomming packages |
---|
[332] | 3 | * ->don't read this without the class PacketGenerator, since they belong together |
---|
[223] | 4 | * |
---|
| 5 | * Autor: Dumeni Manatschal |
---|
| 6 | * |
---|
| 7 | */ |
---|
| 8 | |
---|
[199] | 9 | #include "enet/enet.h" |
---|
| 10 | #include "PacketManager.h" |
---|
| 11 | #include <iostream> |
---|
| 12 | |
---|
| 13 | using namespace std; |
---|
| 14 | using namespace network; |
---|
| 15 | |
---|
| 16 | PacketDecoder::PacketDecoder(){} |
---|
| 17 | |
---|
[223] | 18 | //call this function out of an instance of PacketDecoder |
---|
| 19 | //it will determine the type id and call the right decode function |
---|
[203] | 20 | bool PacketDecoder::elaborate( ENetPacket* packet, int clientId ) |
---|
[199] | 21 | { |
---|
[203] | 22 | int client = clientId; |
---|
[332] | 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 |
---|
[199] | 25 | switch( id ) { |
---|
| 26 | case ACK: |
---|
[203] | 27 | acknowledgement( packet ); |
---|
[199] | 28 | return true; |
---|
| 29 | break; |
---|
| 30 | case MOUSE: |
---|
[203] | 31 | mousem( packet ); |
---|
[199] | 32 | return true; |
---|
| 33 | break; |
---|
| 34 | case KEYBOARD: |
---|
[203] | 35 | keystrike( packet ); |
---|
[199] | 36 | return true; |
---|
| 37 | break; |
---|
| 38 | case CHAT: |
---|
[203] | 39 | chatMessage( packet ); |
---|
[199] | 40 | return true; |
---|
| 41 | break; |
---|
[332] | 42 | case GAMESTATE: |
---|
| 43 | gstate( packet ); |
---|
| 44 | return true; |
---|
| 45 | break; |
---|
[199] | 46 | } |
---|
| 47 | return false; |
---|
| 48 | } |
---|
| 49 | |
---|
[223] | 50 | //following are the decode functions for the data of the packets |
---|
| 51 | |
---|
[199] | 52 | void PacketDecoder::acknowledgement( ENetPacket* packet ) |
---|
| 53 | { |
---|
| 54 | ack* a = new ack; |
---|
[332] | 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 |
---|
[199] | 61 | } |
---|
| 62 | |
---|
| 63 | void PacketDecoder::mousem( ENetPacket* packet ) |
---|
| 64 | { |
---|
| 65 | mouse* mouseMove = new mouse; |
---|
[332] | 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 |
---|
[199] | 73 | } |
---|
| 74 | |
---|
| 75 | void PacketDecoder::keystrike( ENetPacket* packet ) |
---|
| 76 | { |
---|
| 77 | keyboard* key = new keyboard; |
---|
[332] | 78 | *key = *(keyboard*)packet->data; //see above |
---|
| 79 | |
---|
| 80 | //clean memory |
---|
| 81 | enet_packet_destroy( packet ); |
---|
| 82 | |
---|
| 83 | printKey( key ); //debug info |
---|
| 84 | |
---|
[199] | 85 | } |
---|
| 86 | |
---|
| 87 | void PacketDecoder::chatMessage( ENetPacket* packet ) |
---|
| 88 | { |
---|
| 89 | chat* chatting = new chat; |
---|
[332] | 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* |
---|
[199] | 97 | chatting->message = reserve; |
---|
[332] | 98 | |
---|
| 99 | //clean memory |
---|
| 100 | enet_packet_destroy( packet ); |
---|
| 101 | |
---|
| 102 | printChat( chatting ); //debug info |
---|
| 103 | |
---|
[199] | 104 | } |
---|
[223] | 105 | |
---|
[332] | 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 ); |
---|
| 123 | } |
---|
| 124 | |
---|
[223] | 125 | //these are some print functions for test stuff |
---|
| 126 | |
---|
[199] | 127 | void PacketDecoder::printAck( ack* data ) |
---|
| 128 | { |
---|
| 129 | cout << "data id: " << data->id << endl; |
---|
| 130 | cout << "data: " << data->a << endl; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void PacketDecoder::printMouse( mouse* data ) |
---|
| 134 | { |
---|
| 135 | cout << "data id: " << data->id << endl; |
---|
| 136 | cout << "data: " << data->x << " " << data->y << endl; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | void PacketDecoder::printKey( keyboard* data ) |
---|
| 140 | { |
---|
| 141 | cout << "data id: " << data->id << endl; |
---|
| 142 | cout << "data: " << (char)data->press << endl; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void PacketDecoder::printChat( chat* data ) |
---|
| 146 | { |
---|
| 147 | cout << "data id: " << data->id << endl; |
---|
| 148 | cout << "data: " << data->message << endl; |
---|
| 149 | } |
---|
[332] | 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 | } |
---|