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