| [223] | 1 | /* | 
|---|
| [514] | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  * | 
|---|
 | 4 |  * | 
|---|
 | 5 |  *   License notice: | 
|---|
 | 6 |  * | 
|---|
 | 7 |  *   This program is free software; you can redistribute it and/or | 
|---|
 | 8 |  *   modify it under the terms of the GNU General Public License | 
|---|
 | 9 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
 | 10 |  *   of the License, or (at your option) any later version. | 
|---|
 | 11 |  * | 
|---|
 | 12 |  *   This program is distributed in the hope that it will be useful, | 
|---|
 | 13 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
 | 14 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
 | 15 |  *   GNU General Public License for more details. | 
|---|
 | 16 |  * | 
|---|
 | 17 |  *   You should have received a copy of the GNU General Public License | 
|---|
 | 18 |  *   along with this program; if not, write to the Free Software | 
|---|
 | 19 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
 | 20 |  * | 
|---|
 | 21 |  *   Author: | 
|---|
 | 22 |  *      Dumeni Manatschal, (C) 2007 | 
|---|
 | 23 |  *   Co-authors: | 
|---|
 | 24 |  *      ... | 
|---|
 | 25 |  * | 
|---|
 | 26 |  */ | 
|---|
 | 27 |  | 
|---|
 | 28 | /* | 
|---|
| [223] | 29 |  * Class contains functions to determine and decode incomming packages | 
|---|
| [332] | 30 |  * ->don't read this without the class PacketGenerator, since they belong together | 
|---|
| [514] | 31 |  * | 
|---|
| [223] | 32 |  * Autor: Dumeni Manatschal | 
|---|
| [514] | 33 |  * | 
|---|
| [223] | 34 | */ | 
|---|
 | 35 |  | 
|---|
| [341] | 36 | #include <enet/enet.h> | 
|---|
| [199] | 37 | #include "PacketManager.h" | 
|---|
 | 38 | #include <iostream> | 
|---|
 | 39 |  | 
|---|
 | 40 | using namespace std; | 
|---|
 | 41 | using namespace network; | 
|---|
 | 42 |  | 
|---|
 | 43 | PacketDecoder::PacketDecoder(){} | 
|---|
 | 44 |  | 
|---|
| [531] | 45 | PacketDecoder::~PacketDecoder(){} | 
|---|
 | 46 |  | 
|---|
| [223] | 47 | //call this function out of an instance of PacketDecoder | 
|---|
 | 48 | //it will determine the type id and call the right decode function | 
|---|
| [203] | 49 | bool PacketDecoder::elaborate( ENetPacket* packet, int clientId ) | 
|---|
| [199] | 50 | { | 
|---|
| [203] | 51 |         int client = clientId; | 
|---|
| [332] | 52 |         cout << "clientId: " << client << endl; //control cout, not important, just debugging info | 
|---|
 | 53 |         int id = (int)*packet->data; //the first 4 bytes are always the enet packet id | 
|---|
| [632] | 54 |         std::cout << "packet id: " << id << std::endl; | 
|---|
 | 55 |         std::cout << "packet size inside packetdecoder: " << packet->dataLength << std::endl; | 
|---|
| [199] | 56 |         switch( id ) { | 
|---|
 | 57 |         case ACK: | 
|---|
| [440] | 58 |                 acknowledgement( packet, clientId ); | 
|---|
| [199] | 59 |                 return true; | 
|---|
 | 60 |                 break; | 
|---|
 | 61 |         case MOUSE: | 
|---|
| [440] | 62 |                 mousem( packet, clientId ); | 
|---|
| [199] | 63 |                 return true; | 
|---|
 | 64 |                 break; | 
|---|
 | 65 |         case KEYBOARD: | 
|---|
| [440] | 66 |                 keystrike( packet, clientId ); | 
|---|
| [199] | 67 |                 return true; | 
|---|
 | 68 |                 break; | 
|---|
 | 69 |         case CHAT: | 
|---|
| [440] | 70 |                 chatMessage( packet, clientId ); | 
|---|
| [199] | 71 |                 return true; | 
|---|
 | 72 |                 break; | 
|---|
| [332] | 73 |         case GAMESTATE: | 
|---|
 | 74 |                 gstate( packet ); | 
|---|
 | 75 |                 return true; | 
|---|
 | 76 |                 break; | 
|---|
| [437] | 77 |     case CLASSID: | 
|---|
 | 78 |         clid(packet); | 
|---|
 | 79 |         return true; | 
|---|
 | 80 |         break; | 
|---|
| [199] | 81 |         } | 
|---|
 | 82 |         return false; | 
|---|
 | 83 | } | 
|---|
 | 84 |  | 
|---|
| [223] | 85 | //following are the decode functions for the data of the packets | 
|---|
 | 86 |  | 
|---|
| [440] | 87 | void PacketDecoder::acknowledgement( ENetPacket* packet, int clientId ) | 
|---|
| [199] | 88 | { | 
|---|
 | 89 |         ack* a = new ack; | 
|---|
| [332] | 90 |         *a = *(ack*)packet->data; //press pattern of ack on new data | 
|---|
| [514] | 91 |  | 
|---|
 | 92 |  | 
|---|
| [624] | 93 |   std::cout << "got ack id: " << a->id << std::endl; | 
|---|
| [620] | 94 |   processAck( a, clientId ); //debug info | 
|---|
 | 95 |    | 
|---|
 | 96 |   //clean memory | 
|---|
 | 97 |   enet_packet_destroy( packet ); | 
|---|
| [199] | 98 | } | 
|---|
 | 99 |  | 
|---|
| [440] | 100 | void PacketDecoder::mousem( ENetPacket* packet, int clientId ) | 
|---|
| [199] | 101 | { | 
|---|
 | 102 |         mouse* mouseMove = new mouse; | 
|---|
| [332] | 103 |         //copy data of packet->data to new struct | 
|---|
| [514] | 104 |         *mouseMove = *(mouse*)packet->data; | 
|---|
 | 105 |  | 
|---|
| [332] | 106 |         //clean memory | 
|---|
 | 107 |         enet_packet_destroy( packet ); | 
|---|
| [514] | 108 |  | 
|---|
| [332] | 109 |         printMouse( mouseMove ); //debug info | 
|---|
| [199] | 110 | } | 
|---|
 | 111 |  | 
|---|
| [440] | 112 | void PacketDecoder::keystrike( ENetPacket* packet, int clientId ) | 
|---|
| [199] | 113 | { | 
|---|
 | 114 |         keyboard* key = new keyboard; | 
|---|
| [332] | 115 |         *key = *(keyboard*)packet->data; //see above | 
|---|
| [514] | 116 |  | 
|---|
| [332] | 117 |         //clean memory | 
|---|
 | 118 |         enet_packet_destroy( packet ); | 
|---|
| [514] | 119 |  | 
|---|
| [332] | 120 |         printKey( key ); //debug info | 
|---|
 | 121 |  | 
|---|
| [199] | 122 | } | 
|---|
 | 123 |  | 
|---|
| [440] | 124 | void PacketDecoder::chatMessage( ENetPacket* packet, int clientId ) | 
|---|
| [199] | 125 | { | 
|---|
 | 126 |         chat* chatting = new chat; | 
|---|
| [332] | 127 |         chatting->id = (int)*packet->data; //first copy id into new struct | 
|---|
 | 128 |         //since the chat message is a char*, allocate the memory needed | 
|---|
| [514] | 129 |         char* reserve = new char[packet->dataLength-4]; | 
|---|
| [332] | 130 |         //copy the transmitted bytestream into the new generated char*, | 
|---|
| [514] | 131 |         //note the lenght of the message is represented as "packet->dataLength-sizeof( int )" | 
|---|
| [332] | 132 |         memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) ); | 
|---|
 | 133 |         //put pointer of chatting struct to the begining of the new generated char* | 
|---|
| [199] | 134 |         chatting->message = reserve; | 
|---|
| [514] | 135 |  | 
|---|
| [332] | 136 |         //clean memory | 
|---|
 | 137 |         enet_packet_destroy( packet ); | 
|---|
| [514] | 138 |  | 
|---|
| [440] | 139 |         processChat( chatting, clientId ); //debug info | 
|---|
| [514] | 140 |  | 
|---|
| [199] | 141 | } | 
|---|
| [223] | 142 |  | 
|---|
| [332] | 143 | void PacketDecoder::gstate( ENetPacket* packet ) | 
|---|
 | 144 | { | 
|---|
| [403] | 145 |         GameStateCompressed* currentState = new GameStateCompressed; | 
|---|
| [332] | 146 |         //since it's not alowed to use void* for pointer arithmetic | 
|---|
 | 147 |         unsigned char* data = (unsigned char*)packet->data; | 
|---|
| [403] | 148 |         //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int ) | 
|---|
| [632] | 149 |         //memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) ); | 
|---|
 | 150 |         currentState->id = (int)*(data+sizeof(int)); | 
|---|
 | 151 |          std::cout << "id: " << currentState->id << std::endl; | 
|---|
| [403] | 152 |         //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th | 
|---|
| [332] | 153 |         //position of the data stream, data+2*sizeof( int ) | 
|---|
| [632] | 154 |         memcpy( (void*)&(currentState->compsize), (const void*)(data+2*sizeof( int )), sizeof( int) ); | 
|---|
 | 155 |         //currentState->compsize = (int)*(data+2*sizeof(int)); | 
|---|
 | 156 |         std::cout << "compsize: " << currentState->compsize << std::endl; | 
|---|
| [403] | 157 |         //size of uncompressed data | 
|---|
| [632] | 158 |         memcpy( (void*)&(currentState->normsize), (const void*)(data+3*sizeof( int )), sizeof( int ) ); | 
|---|
 | 159 |         //currentState->normsize = (int)*(data+3*sizeof(int)); | 
|---|
 | 160 |         std::cout << "normsize. " << currentState->normsize << std::endl; | 
|---|
| [437] | 161 |         //since the packetgenerator was changed, due to a new parameter, change this function too | 
|---|
| [632] | 162 |         memcpy( (void*)&(currentState->diffed), (const void*)(data+4*sizeof(int)), sizeof(bool)); | 
|---|
 | 163 |         //currentState->diffed = (bool)*(data+4*sizeof(int)); | 
|---|
 | 164 |         std::cout << "diffed: " << currentState->diffed << std::endl; | 
|---|
| [332] | 165 |         //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream | 
|---|
| [405] | 166 |         currentState->data = (unsigned char*)(malloc( currentState->compsize )); | 
|---|
| [632] | 167 |         if(currentState->data==NULL) | 
|---|
 | 168 |                 std::cout << "memory leak" << std::endl; | 
|---|
| [403] | 169 |         //copy the GameStateCompressed data | 
|---|
| [632] | 170 |         //std::cout << "packet size (enet): " << packet->dataLength << std::endl; | 
|---|
 | 171 |         //std::cout << "totallen: " << 4*sizeof(int)+sizeof(bool)+currentState->compsize << std::endl; | 
|---|
| [437] | 172 |         memcpy( (void*)(currentState->data), (const void*)(data+4*sizeof( int ) + sizeof(bool)), currentState->compsize ); | 
|---|
| [514] | 173 |  | 
|---|
| [332] | 174 |         //clean memory | 
|---|
 | 175 |         enet_packet_destroy( packet ); | 
|---|
| [632] | 176 |         //run processGameStateCompressed | 
|---|
 | 177 |         //TODO: not yet implemented! | 
|---|
 | 178 |         processGamestate(currentState); | 
|---|
| [332] | 179 | } | 
|---|
 | 180 |  | 
|---|
| [400] | 181 | void PacketDecoder::clid( ENetPacket *packet) | 
|---|
 | 182 | { | 
|---|
 | 183 |         classid* cid = new classid; | 
|---|
 | 184 |         cid->length = ((classid*)(packet->data))->length; | 
|---|
 | 185 |         cid->id = ((classid *)(packet->data))->id; | 
|---|
| [415] | 186 |         cid->clid = ((classid *)(packet->data))->clid; | 
|---|
| [400] | 187 |         cid->message = (const char *)malloc(cid->length); | 
|---|
| [636] | 188 |         void *data  = (void *)cid->message; | 
|---|
 | 189 |         memcpy(data, (const void*)(packet->data+3*sizeof(int)), cid->length); | 
|---|
 | 190 |         std::cout << "classid: " << cid->clid << ", name: " << cid->message << std::endl; | 
|---|
| [400] | 191 |         enet_packet_destroy( packet ); | 
|---|
| [401] | 192 |         processClassid(cid); | 
|---|
| [400] | 193 | } | 
|---|
 | 194 |  | 
|---|
 | 195 |  | 
|---|
| [369] | 196 | // now the data processing functions: | 
|---|
 | 197 |  | 
|---|
| [440] | 198 | void PacketDecoder::processChat( chat *data, int clientId){ | 
|---|
 | 199 |   printChat(data, clientId); | 
|---|
| [369] | 200 | } | 
|---|
 | 201 |  | 
|---|
| [620] | 202 | void PacketDecoder::processGamestate( GameStateCompressed *state ){ | 
|---|
 | 203 |    | 
|---|
 | 204 | } | 
|---|
 | 205 |  | 
|---|
| [400] | 206 | void PacketDecoder::processClassid( classid *cid){ | 
|---|
 | 207 |   printClassid(cid); | 
|---|
 | 208 |   return; | 
|---|
 | 209 | } | 
|---|
 | 210 |  | 
|---|
| [620] | 211 | void PacketDecoder::processAck( ack *data, int clientID){ | 
|---|
 | 212 |   printAck(data); | 
|---|
 | 213 |   return; | 
|---|
 | 214 | } | 
|---|
| [400] | 215 |  | 
|---|
 | 216 |  | 
|---|
| [223] | 217 | //these are some print functions for test stuff | 
|---|
 | 218 |  | 
|---|
| [199] | 219 | void PacketDecoder::printAck( ack* data ) | 
|---|
 | 220 | { | 
|---|
 | 221 |         cout << "data id: " << data->id << endl; | 
|---|
 | 222 |         cout << "data:    " << data->a << endl; | 
|---|
 | 223 | } | 
|---|
 | 224 |  | 
|---|
 | 225 | void PacketDecoder::printMouse( mouse* data ) | 
|---|
 | 226 | { | 
|---|
 | 227 |         cout << "data id: " << data->id << endl; | 
|---|
 | 228 |         cout << "data:    " << data->x << " " << data->y << endl; | 
|---|
 | 229 | } | 
|---|
 | 230 |  | 
|---|
 | 231 | void PacketDecoder::printKey( keyboard* data ) | 
|---|
 | 232 | { | 
|---|
 | 233 |         cout << "data id: " << data->id << endl; | 
|---|
 | 234 |         cout << "data:    " << (char)data->press << endl; | 
|---|
 | 235 | } | 
|---|
 | 236 |  | 
|---|
| [440] | 237 | void PacketDecoder::printChat( chat* data, int clientId ) | 
|---|
| [199] | 238 | { | 
|---|
| [440] | 239 |   if(clientId!=CLIENTID_CLIENT) | 
|---|
 | 240 |     cout << "client: " << clientId << endl; | 
|---|
| [199] | 241 |         cout << "data id: " << data->id << endl; | 
|---|
 | 242 |         cout << "data:    " << data->message << endl; | 
|---|
 | 243 | } | 
|---|
| [332] | 244 |  | 
|---|
| [403] | 245 | void PacketDecoder::printGamestate( GameStateCompressed* data ) | 
|---|
| [332] | 246 | { | 
|---|
| [403] | 247 |         cout << "id of GameStateCompressed:   " << data->id << endl; | 
|---|
| [405] | 248 |         cout << "size of GameStateCompressed: " << data->compsize << endl; | 
|---|
| [332] | 249 | } | 
|---|
| [400] | 250 |  | 
|---|
 | 251 | void PacketDecoder::printClassid( classid *cid) | 
|---|
 | 252 | { | 
|---|
 | 253 |         cout << "id of classid:    " << cid->id << endl; | 
|---|
 | 254 |         cout << "size of classid:  " << cid->length << endl; | 
|---|
| [415] | 255 |         cout << "ID of classid:    " << cid->clid <<endl; | 
|---|
| [400] | 256 |         cout << "data of classid:  " << cid->message <<endl; | 
|---|
 | 257 | } | 
|---|