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