[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 | |
---|
[341] | 9 | #include <enet/enet.h> |
---|
[199] | 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; |
---|
[437] | 46 | case CLASSID: |
---|
| 47 | clid(packet); |
---|
| 48 | return true; |
---|
| 49 | break; |
---|
[199] | 50 | } |
---|
| 51 | return false; |
---|
| 52 | } |
---|
| 53 | |
---|
[223] | 54 | //following are the decode functions for the data of the packets |
---|
| 55 | |
---|
[199] | 56 | void PacketDecoder::acknowledgement( ENetPacket* packet ) |
---|
| 57 | { |
---|
| 58 | ack* a = new ack; |
---|
[332] | 59 | *a = *(ack*)packet->data; //press pattern of ack on new data |
---|
| 60 | |
---|
| 61 | //clean memory |
---|
| 62 | enet_packet_destroy( packet ); |
---|
| 63 | |
---|
| 64 | printAck( a ); //debug info |
---|
[199] | 65 | } |
---|
| 66 | |
---|
| 67 | void PacketDecoder::mousem( ENetPacket* packet ) |
---|
| 68 | { |
---|
| 69 | mouse* mouseMove = new mouse; |
---|
[332] | 70 | //copy data of packet->data to new struct |
---|
| 71 | *mouseMove = *(mouse*)packet->data; |
---|
| 72 | |
---|
| 73 | //clean memory |
---|
| 74 | enet_packet_destroy( packet ); |
---|
| 75 | |
---|
| 76 | printMouse( mouseMove ); //debug info |
---|
[199] | 77 | } |
---|
| 78 | |
---|
| 79 | void PacketDecoder::keystrike( ENetPacket* packet ) |
---|
| 80 | { |
---|
| 81 | keyboard* key = new keyboard; |
---|
[332] | 82 | *key = *(keyboard*)packet->data; //see above |
---|
| 83 | |
---|
| 84 | //clean memory |
---|
| 85 | enet_packet_destroy( packet ); |
---|
| 86 | |
---|
| 87 | printKey( key ); //debug info |
---|
| 88 | |
---|
[199] | 89 | } |
---|
| 90 | |
---|
| 91 | void PacketDecoder::chatMessage( ENetPacket* packet ) |
---|
| 92 | { |
---|
| 93 | chat* chatting = new chat; |
---|
[332] | 94 | chatting->id = (int)*packet->data; //first copy id into new struct |
---|
| 95 | //since the chat message is a char*, allocate the memory needed |
---|
| 96 | char* reserve = new char[packet->dataLength-4]; |
---|
| 97 | //copy the transmitted bytestream into the new generated char*, |
---|
| 98 | //note the lenght of the message is represented as "packet->dataLength-sizeof( int )" |
---|
| 99 | memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) ); |
---|
| 100 | //put pointer of chatting struct to the begining of the new generated char* |
---|
[199] | 101 | chatting->message = reserve; |
---|
[332] | 102 | |
---|
| 103 | //clean memory |
---|
| 104 | enet_packet_destroy( packet ); |
---|
| 105 | |
---|
[369] | 106 | processChat( chatting ); //debug info |
---|
[332] | 107 | |
---|
[199] | 108 | } |
---|
[223] | 109 | |
---|
[332] | 110 | void PacketDecoder::gstate( ENetPacket* packet ) |
---|
| 111 | { |
---|
[403] | 112 | GameStateCompressed* currentState = new GameStateCompressed; |
---|
[332] | 113 | //since it's not alowed to use void* for pointer arithmetic |
---|
| 114 | unsigned char* data = (unsigned char*)packet->data; |
---|
[403] | 115 | //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int ) |
---|
[332] | 116 | memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) ); |
---|
[403] | 117 | //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th |
---|
[332] | 118 | //position of the data stream, data+2*sizeof( int ) |
---|
[403] | 119 | memcpy( (void*)&(currentState->compsize), (const void*)(data+2*sizeof( int )), sizeof( int) ); |
---|
| 120 | //size of uncompressed data |
---|
| 121 | memcpy( (void*)&(currentState->normsize), (const void*)(data+3*sizeof( int )), sizeof( int ) ); |
---|
[437] | 122 | //since the packetgenerator was changed, due to a new parameter, change this function too |
---|
| 123 | memcpy( (void*)&(currentState->diffed), (const void*)(data+4*sizeof(int)), sizeof(bool)); |
---|
[332] | 124 | //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream |
---|
[405] | 125 | currentState->data = (unsigned char*)(malloc( currentState->compsize )); |
---|
[403] | 126 | //copy the GameStateCompressed data |
---|
[437] | 127 | memcpy( (void*)(currentState->data), (const void*)(data+4*sizeof( int ) + sizeof(bool)), currentState->compsize ); |
---|
[369] | 128 | |
---|
[332] | 129 | //clean memory |
---|
| 130 | enet_packet_destroy( packet ); |
---|
[403] | 131 | //run processGameStateCompressed |
---|
[374] | 132 | //TODO: not yet implemented! |
---|
| 133 | //processGamestate(currentState); |
---|
[332] | 134 | } |
---|
| 135 | |
---|
[400] | 136 | void PacketDecoder::clid( ENetPacket *packet) |
---|
| 137 | { |
---|
| 138 | classid* cid = new classid; |
---|
| 139 | cid->length = ((classid*)(packet->data))->length; |
---|
| 140 | cid->id = ((classid *)(packet->data))->id; |
---|
[415] | 141 | cid->clid = ((classid *)(packet->data))->clid; |
---|
[400] | 142 | cid->message = (const char *)malloc(cid->length); |
---|
| 143 | enet_packet_destroy( packet ); |
---|
[401] | 144 | processClassid(cid); |
---|
[400] | 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
[369] | 148 | // now the data processing functions: |
---|
| 149 | |
---|
| 150 | void PacketDecoder::processChat( chat *data){ |
---|
| 151 | printChat(data); |
---|
| 152 | } |
---|
| 153 | |
---|
[400] | 154 | void PacketDecoder::processClassid( classid *cid){ |
---|
| 155 | printClassid(cid); |
---|
| 156 | return; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | |
---|
[223] | 161 | //these are some print functions for test stuff |
---|
| 162 | |
---|
[199] | 163 | void PacketDecoder::printAck( ack* data ) |
---|
| 164 | { |
---|
| 165 | cout << "data id: " << data->id << endl; |
---|
| 166 | cout << "data: " << data->a << endl; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void PacketDecoder::printMouse( mouse* data ) |
---|
| 170 | { |
---|
| 171 | cout << "data id: " << data->id << endl; |
---|
| 172 | cout << "data: " << data->x << " " << data->y << endl; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void PacketDecoder::printKey( keyboard* data ) |
---|
| 176 | { |
---|
| 177 | cout << "data id: " << data->id << endl; |
---|
| 178 | cout << "data: " << (char)data->press << endl; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | void PacketDecoder::printChat( chat* data ) |
---|
| 182 | { |
---|
| 183 | cout << "data id: " << data->id << endl; |
---|
| 184 | cout << "data: " << data->message << endl; |
---|
| 185 | } |
---|
[332] | 186 | |
---|
[403] | 187 | void PacketDecoder::printGamestate( GameStateCompressed* data ) |
---|
[332] | 188 | { |
---|
[403] | 189 | cout << "id of GameStateCompressed: " << data->id << endl; |
---|
[405] | 190 | cout << "size of GameStateCompressed: " << data->compsize << endl; |
---|
[332] | 191 | } |
---|
[400] | 192 | |
---|
| 193 | void PacketDecoder::printClassid( classid *cid) |
---|
| 194 | { |
---|
| 195 | cout << "id of classid: " << cid->id << endl; |
---|
| 196 | cout << "size of classid: " << cid->length << endl; |
---|
[415] | 197 | cout << "ID of classid: " << cid->clid <<endl; |
---|
[400] | 198 | cout << "data of classid: " << cid->message <<endl; |
---|
| 199 | } |
---|