| [1056] | 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 | */ |
|---|
| [514] | 28 | |
|---|
| 29 | /* |
|---|
| [777] | 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 | * |
|---|
| [223] | 35 | */ |
|---|
| 36 | |
|---|
| [1062] | 37 | #include "PacketTypes.h" |
|---|
| 38 | #include "PacketManager.h" |
|---|
| 39 | |
|---|
| [199] | 40 | #include <iostream> |
|---|
| 41 | |
|---|
| [1055] | 42 | #include "core/Debug.h" |
|---|
| [199] | 43 | |
|---|
| [777] | 44 | namespace network |
|---|
| 45 | { |
|---|
| [199] | 46 | |
|---|
| [777] | 47 | PacketDecoder::PacketDecoder(){} |
|---|
| [531] | 48 | |
|---|
| [777] | 49 | PacketDecoder::~PacketDecoder(){} |
|---|
| [199] | 50 | |
|---|
| [777] | 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; |
|---|
| [1021] | 56 | COUT(5) << "clientId: " << client << std::endl; //control cout, not important, just debugging info |
|---|
| [777] | 57 | int id = (int)*packet->data; //the first 4 bytes are always the enet packet id |
|---|
| [1021] | 58 | COUT(5) << "packet id: " << id << std::endl; |
|---|
| 59 | // COUT(5) << "packet size inside packetdecoder: " << packet->dataLength << std::endl; |
|---|
| [777] | 60 | switch( id ) { |
|---|
| 61 | case ACK: |
|---|
| 62 | acknowledgement( packet, clientId ); |
|---|
| 63 | return true; |
|---|
| 64 | break; |
|---|
| 65 | case MOUSE: |
|---|
| 66 | mousem( packet, clientId ); |
|---|
| 67 | return true; |
|---|
| 68 | break; |
|---|
| 69 | case KEYBOARD: |
|---|
| 70 | keystrike( packet, clientId ); |
|---|
| 71 | return true; |
|---|
| 72 | break; |
|---|
| 73 | case CHAT: |
|---|
| 74 | chatMessage( packet, clientId ); |
|---|
| 75 | return true; |
|---|
| 76 | break; |
|---|
| 77 | case GAMESTATE: |
|---|
| 78 | gstate( packet ); |
|---|
| 79 | return true; |
|---|
| 80 | break; |
|---|
| 81 | case CLASSID: |
|---|
| 82 | clid(packet); |
|---|
| 83 | return true; |
|---|
| 84 | break; |
|---|
| 85 | } |
|---|
| 86 | return false; |
|---|
| 87 | } |
|---|
| [223] | 88 | |
|---|
| [777] | 89 | //following are the decode functions for the data of the packets |
|---|
| [514] | 90 | |
|---|
| [777] | 91 | void PacketDecoder::acknowledgement( ENetPacket* packet, int clientId ) |
|---|
| 92 | { |
|---|
| 93 | ack* a = new ack; |
|---|
| 94 | *a = *(ack*)packet->data; //press pattern of ack on new data |
|---|
| [514] | 95 | |
|---|
| [199] | 96 | |
|---|
| [1021] | 97 | COUT(5) << "got ack id: " << a->id << std::endl; |
|---|
| [777] | 98 | processAck( a, clientId ); //debug info |
|---|
| [514] | 99 | |
|---|
| [777] | 100 | //clean memory |
|---|
| 101 | enet_packet_destroy( packet ); |
|---|
| 102 | } |
|---|
| [514] | 103 | |
|---|
| [777] | 104 | void PacketDecoder::mousem( ENetPacket* packet, int clientId ) |
|---|
| 105 | { |
|---|
| 106 | mouse* mouseMove = new mouse; |
|---|
| 107 | //copy data of packet->data to new struct |
|---|
| 108 | *mouseMove = *(mouse*)packet->data; |
|---|
| [199] | 109 | |
|---|
| [777] | 110 | //clean memory |
|---|
| 111 | enet_packet_destroy( packet ); |
|---|
| [514] | 112 | |
|---|
| [777] | 113 | printMouse( mouseMove ); //debug info |
|---|
| 114 | } |
|---|
| [514] | 115 | |
|---|
| [777] | 116 | void PacketDecoder::keystrike( ENetPacket* packet, int clientId ) |
|---|
| 117 | { |
|---|
| 118 | keyboard* key = new keyboard; |
|---|
| 119 | *key = *(keyboard*)packet->data; //see above |
|---|
| [332] | 120 | |
|---|
| [777] | 121 | //clean memory |
|---|
| 122 | enet_packet_destroy( packet ); |
|---|
| [199] | 123 | |
|---|
| [777] | 124 | printKey( key ); //debug info |
|---|
| [514] | 125 | |
|---|
| [777] | 126 | } |
|---|
| [514] | 127 | |
|---|
| [777] | 128 | void PacketDecoder::chatMessage( ENetPacket* packet, int clientId ) |
|---|
| 129 | { |
|---|
| 130 | chat* chatting = new chat; |
|---|
| 131 | chatting->id = (int)*packet->data; //first copy id into new struct |
|---|
| 132 | //since the chat message is a char*, allocate the memory needed |
|---|
| 133 | char* reserve = new char[packet->dataLength-4]; |
|---|
| 134 | //copy the transmitted bytestream into the new generated char*, |
|---|
| 135 | //note the lenght of the message is represented as "packet->dataLength-sizeof( int )" |
|---|
| 136 | memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) ); |
|---|
| 137 | //put pointer of chatting struct to the begining of the new generated char* |
|---|
| 138 | chatting->message = reserve; |
|---|
| [514] | 139 | |
|---|
| [777] | 140 | //clean memory |
|---|
| 141 | enet_packet_destroy( packet ); |
|---|
| [223] | 142 | |
|---|
| [777] | 143 | processChat( chatting, clientId ); //debug info |
|---|
| 144 | } |
|---|
| [514] | 145 | |
|---|
| [777] | 146 | void PacketDecoder::gstate( ENetPacket* packet ) |
|---|
| 147 | { |
|---|
| [1021] | 148 | GameStateCompressed* currentState = NULL; |
|---|
| 149 | currentState = new GameStateCompressed; |
|---|
| 150 | if(currentState == NULL){ |
|---|
| 151 | COUT(3) << "could not generate new GameStateCompressed" << std::endl; |
|---|
| 152 | return; |
|---|
| 153 | } |
|---|
| [777] | 154 | //since it's not alowed to use void* for pointer arithmetic |
|---|
| [1064] | 155 | //FIXME: variable never used |
|---|
| [1021] | 156 | unsigned char* data = (unsigned char *)(packet->data); |
|---|
| [777] | 157 | //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int ) |
|---|
| 158 | //memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) ); |
|---|
| [1021] | 159 | //currentState->id = *((int *)packet->data+sizeof(int)); |
|---|
| 160 | memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) ); |
|---|
| 161 | COUT(5) << "decoder: received gs id: " << currentState->id << std::endl; |
|---|
| 162 | // std::cout << "id: " << currentState->id << std::endl; |
|---|
| [777] | 163 | //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th |
|---|
| 164 | //position of the data stream, data+2*sizeof( int ) |
|---|
| [1021] | 165 | memcpy( (void*)&(currentState->compsize), (const void*)(packet->data+2*sizeof( int )), sizeof( int) ); |
|---|
| [777] | 166 | //currentState->compsize = (int)*(data+2*sizeof(int)); |
|---|
| [1021] | 167 | // std::cout << "compsize: " << currentState->compsize << std::endl; |
|---|
| [777] | 168 | //size of uncompressed data |
|---|
| [1021] | 169 | memcpy( (void*)&(currentState->normsize), (const void*)(packet->data+3*sizeof( int )), sizeof( int ) ); |
|---|
| [777] | 170 | //currentState->normsize = (int)*(data+3*sizeof(int)); |
|---|
| [1021] | 171 | // std::cout << "normsize. " << currentState->normsize << std::endl; |
|---|
| [777] | 172 | //since the packetgenerator was changed, due to a new parameter, change this function too |
|---|
| [1021] | 173 | memcpy( (void*)&(currentState->diffed), (const void*)(packet->data+4*sizeof(int)), sizeof(bool)); |
|---|
| [777] | 174 | //currentState->diffed = (bool)*(data+4*sizeof(int)); |
|---|
| [1021] | 175 | // std::cout << "diffed: " << currentState->diffed << std::endl; |
|---|
| [777] | 176 | //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream |
|---|
| [1021] | 177 | if(currentState->compsize==0) |
|---|
| 178 | COUT(2) << "compsize is 0" << std::endl; |
|---|
| [777] | 179 | currentState->data = (unsigned char*)(malloc( currentState->compsize )); |
|---|
| 180 | if(currentState->data==NULL) |
|---|
| [1021] | 181 | COUT(2) << "Gamestatepacket-decoder: memory leak" << std::endl; |
|---|
| [777] | 182 | //copy the GameStateCompressed data |
|---|
| 183 | //std::cout << "packet size (enet): " << packet->dataLength << std::endl; |
|---|
| 184 | //std::cout << "totallen: " << 4*sizeof(int)+sizeof(bool)+currentState->compsize << std::endl; |
|---|
| [1021] | 185 | memcpy( (void*)(currentState->data), (const void*)(packet->data+4*sizeof( int ) + sizeof(bool)), currentState->compsize ); |
|---|
| [332] | 186 | |
|---|
| [777] | 187 | //clean memory |
|---|
| 188 | enet_packet_destroy( packet ); |
|---|
| 189 | processGamestate(currentState); |
|---|
| 190 | } |
|---|
| [400] | 191 | |
|---|
| [777] | 192 | void PacketDecoder::clid( ENetPacket *packet) |
|---|
| 193 | { |
|---|
| 194 | classid* cid = new classid; |
|---|
| 195 | cid->length = ((classid*)(packet->data))->length; |
|---|
| 196 | cid->id = ((classid *)(packet->data))->id; |
|---|
| 197 | cid->clid = ((classid *)(packet->data))->clid; |
|---|
| 198 | cid->message = (const char *)malloc(cid->length); |
|---|
| 199 | void *data = (void *)cid->message; |
|---|
| 200 | memcpy(data, (const void*)(packet->data+3*sizeof(int)), cid->length); |
|---|
| [1021] | 201 | COUT(4) << "classid: " << cid->clid << ", name: " << cid->message << std::endl; |
|---|
| [777] | 202 | enet_packet_destroy( packet ); |
|---|
| 203 | processClassid(cid); |
|---|
| 204 | } |
|---|
| [400] | 205 | |
|---|
| [369] | 206 | |
|---|
| [777] | 207 | // now the data processing functions: |
|---|
| [369] | 208 | |
|---|
| [777] | 209 | void PacketDecoder::processChat( chat *data, int clientId) |
|---|
| 210 | { |
|---|
| 211 | printChat(data, clientId); |
|---|
| 212 | } |
|---|
| [620] | 213 | |
|---|
| [777] | 214 | void PacketDecoder::processGamestate( GameStateCompressed *state ) |
|---|
| 215 | { |
|---|
| 216 | } |
|---|
| [400] | 217 | |
|---|
| [777] | 218 | void PacketDecoder::processClassid( classid *cid) |
|---|
| 219 | { |
|---|
| 220 | printClassid(cid); |
|---|
| 221 | return; |
|---|
| 222 | } |
|---|
| [400] | 223 | |
|---|
| [777] | 224 | void PacketDecoder::processAck( ack *data, int clientID) |
|---|
| 225 | { |
|---|
| 226 | printAck(data); |
|---|
| 227 | return; |
|---|
| 228 | } |
|---|
| [400] | 229 | |
|---|
| [223] | 230 | |
|---|
| [777] | 231 | //these are some print functions for test stuff |
|---|
| [199] | 232 | |
|---|
| [777] | 233 | void PacketDecoder::printAck( ack* data ) |
|---|
| 234 | { |
|---|
| [1021] | 235 | COUT(5) << "data id: " << data->id << std::endl; |
|---|
| 236 | COUT(5) << "data: " << data->a << std::endl; |
|---|
| [777] | 237 | } |
|---|
| [199] | 238 | |
|---|
| [777] | 239 | void PacketDecoder::printMouse( mouse* data ) |
|---|
| 240 | { |
|---|
| [1021] | 241 | COUT(5) << "data id: " << data->id << std::endl; |
|---|
| 242 | COUT(5) << "data: " << data->x << " " << data->y << std::endl; |
|---|
| [777] | 243 | } |
|---|
| [199] | 244 | |
|---|
| [777] | 245 | void PacketDecoder::printKey( keyboard* data ) |
|---|
| 246 | { |
|---|
| [1021] | 247 | COUT(5) << "data id: " << data->id << std::endl; |
|---|
| 248 | COUT(5) << "data: " << (char)data->press << std::endl; |
|---|
| [777] | 249 | } |
|---|
| [332] | 250 | |
|---|
| [777] | 251 | void PacketDecoder::printChat( chat* data, int clientId ) |
|---|
| 252 | { |
|---|
| 253 | if(clientId!=CLIENTID_CLIENT) |
|---|
| [1021] | 254 | COUT(5) << "client: " << clientId << std::endl; |
|---|
| 255 | COUT(5) << "data id: " << data->id << std::endl; |
|---|
| 256 | COUT(5) << "data: " << data->message << std::endl; |
|---|
| [777] | 257 | } |
|---|
| [400] | 258 | |
|---|
| [777] | 259 | void PacketDecoder::printGamestate( GameStateCompressed* data ) |
|---|
| 260 | { |
|---|
| [1021] | 261 | COUT(5) << "id of GameStateCompressed: " << data->id << std::endl; |
|---|
| 262 | COUT(5) << "size of GameStateCompressed: " << data->compsize << std::endl; |
|---|
| [777] | 263 | } |
|---|
| 264 | |
|---|
| 265 | void PacketDecoder::printClassid( classid *cid) |
|---|
| 266 | { |
|---|
| [1021] | 267 | COUT(5) << "id of classid: " << cid->id << std::endl; |
|---|
| 268 | COUT(5) << "size of classid: " << cid->length << std::endl; |
|---|
| 269 | COUT(5) << "ID of classid: " << cid->clid << std::endl; |
|---|
| 270 | COUT(5) << "data of classid: " << cid->message << std::endl; |
|---|
| [777] | 271 | } |
|---|
| 272 | |
|---|
| [400] | 273 | } |
|---|