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