| 1 | /* |
|---|
| 2 | * Class contains functions to determine and decode incomming packages |
|---|
| 3 | * ->don't read this without the class PacketGenerator, since they belong together |
|---|
| 4 | * |
|---|
| 5 | * Autor: Dumeni Manatschal |
|---|
| 6 | * |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 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 | |
|---|
| 18 | //call this function out of an instance of PacketDecoder |
|---|
| 19 | //it will determine the type id and call the right decode function |
|---|
| 20 | bool PacketDecoder::elaborate( ENetPacket* packet, int clientId ) |
|---|
| 21 | { |
|---|
| 22 | int client = clientId; |
|---|
| 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 | switch( id ) { |
|---|
| 26 | case ACK: |
|---|
| 27 | acknowledgement( packet ); |
|---|
| 28 | return true; |
|---|
| 29 | break; |
|---|
| 30 | case MOUSE: |
|---|
| 31 | mousem( packet ); |
|---|
| 32 | return true; |
|---|
| 33 | break; |
|---|
| 34 | case KEYBOARD: |
|---|
| 35 | keystrike( packet ); |
|---|
| 36 | return true; |
|---|
| 37 | break; |
|---|
| 38 | case CHAT: |
|---|
| 39 | chatMessage( packet ); |
|---|
| 40 | return true; |
|---|
| 41 | break; |
|---|
| 42 | case GAMESTATE: |
|---|
| 43 | gstate( packet ); |
|---|
| 44 | return true; |
|---|
| 45 | break; |
|---|
| 46 | case CLASSID: |
|---|
| 47 | clid(packet); |
|---|
| 48 | return true; |
|---|
| 49 | break; |
|---|
| 50 | } |
|---|
| 51 | return false; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | //following are the decode functions for the data of the packets |
|---|
| 55 | |
|---|
| 56 | void PacketDecoder::acknowledgement( ENetPacket* packet ) |
|---|
| 57 | { |
|---|
| 58 | ack* a = new ack; |
|---|
| 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 |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void PacketDecoder::mousem( ENetPacket* packet ) |
|---|
| 68 | { |
|---|
| 69 | mouse* mouseMove = new mouse; |
|---|
| 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 |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | void PacketDecoder::keystrike( ENetPacket* packet ) |
|---|
| 80 | { |
|---|
| 81 | keyboard* key = new keyboard; |
|---|
| 82 | *key = *(keyboard*)packet->data; //see above |
|---|
| 83 | |
|---|
| 84 | //clean memory |
|---|
| 85 | enet_packet_destroy( packet ); |
|---|
| 86 | |
|---|
| 87 | printKey( key ); //debug info |
|---|
| 88 | |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void PacketDecoder::chatMessage( ENetPacket* packet ) |
|---|
| 92 | { |
|---|
| 93 | chat* chatting = new chat; |
|---|
| 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* |
|---|
| 101 | chatting->message = reserve; |
|---|
| 102 | |
|---|
| 103 | //clean memory |
|---|
| 104 | enet_packet_destroy( packet ); |
|---|
| 105 | |
|---|
| 106 | processChat( chatting ); //debug info |
|---|
| 107 | |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void PacketDecoder::gstate( ENetPacket* packet ) |
|---|
| 111 | { |
|---|
| 112 | GameState* currentState = new GameState; |
|---|
| 113 | //since it's not alowed to use void* for pointer arithmetic |
|---|
| 114 | unsigned char* data = (unsigned char*)packet->data; |
|---|
| 115 | //copy the gamestate id into the struct, which is located at second place data+sizeof( int ) |
|---|
| 116 | memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) ); |
|---|
| 117 | //copy the size of the gamestate data into the new gamestate struct, located at 3th |
|---|
| 118 | //position of the data stream, data+2*sizeof( int ) |
|---|
| 119 | memcpy( (void*)&(currentState->size), (const void*)(data+2*sizeof( int )), sizeof( int) ); |
|---|
| 120 | //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream |
|---|
| 121 | currentState->data = (unsigned char*)(malloc( currentState->size )); |
|---|
| 122 | //copy the gamestate data |
|---|
| 123 | memcpy( (void*)(currentState->data), (const void*)(data+3*sizeof( int )), currentState->size ); |
|---|
| 124 | |
|---|
| 125 | //clean memory |
|---|
| 126 | enet_packet_destroy( packet ); |
|---|
| 127 | //run processGamestate |
|---|
| 128 | //TODO: not yet implemented! |
|---|
| 129 | //processGamestate(currentState); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void PacketDecoder::clid( ENetPacket *packet) |
|---|
| 133 | { |
|---|
| 134 | classid* cid = new classid; |
|---|
| 135 | cid->length = ((classid*)(packet->data))->length; |
|---|
| 136 | cid->id = ((classid *)(packet->data))->id; |
|---|
| 137 | cid->classid = ((classid *)(packet->data))->classid; |
|---|
| 138 | cid->message = (const char *)malloc(cid->length); |
|---|
| 139 | enet_packet_destroy( packet ); |
|---|
| 140 | processClassid(cid); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | // now the data processing functions: |
|---|
| 145 | |
|---|
| 146 | void PacketDecoder::processChat( chat *data){ |
|---|
| 147 | printChat(data); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | void PacketDecoder::processClassid( classid *cid){ |
|---|
| 151 | printClassid(cid); |
|---|
| 152 | return; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | //these are some print functions for test stuff |
|---|
| 158 | |
|---|
| 159 | void PacketDecoder::printAck( ack* data ) |
|---|
| 160 | { |
|---|
| 161 | cout << "data id: " << data->id << endl; |
|---|
| 162 | cout << "data: " << data->a << endl; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | void PacketDecoder::printMouse( mouse* data ) |
|---|
| 166 | { |
|---|
| 167 | cout << "data id: " << data->id << endl; |
|---|
| 168 | cout << "data: " << data->x << " " << data->y << endl; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void PacketDecoder::printKey( keyboard* data ) |
|---|
| 172 | { |
|---|
| 173 | cout << "data id: " << data->id << endl; |
|---|
| 174 | cout << "data: " << (char)data->press << endl; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | void PacketDecoder::printChat( chat* data ) |
|---|
| 178 | { |
|---|
| 179 | cout << "data id: " << data->id << endl; |
|---|
| 180 | cout << "data: " << data->message << endl; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | void PacketDecoder::printGamestate( GameState* data ) |
|---|
| 184 | { |
|---|
| 185 | cout << "id of gamestate: " << data->id << endl; |
|---|
| 186 | cout << "size of gamestate: " << data->size << endl; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | void PacketDecoder::printClassid( classid *cid) |
|---|
| 190 | { |
|---|
| 191 | cout << "id of classid: " << cid->id << endl; |
|---|
| 192 | cout << "size of classid: " << cid->length << endl; |
|---|
| 193 | cout << "ID of classid: " << cid->classid <<endl; |
|---|
| 194 | cout << "data of classid: " << cid->message <<endl; |
|---|
| 195 | } |
|---|