[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 | |
---|
[223] | 45 | //call this function out of an instance of PacketDecoder |
---|
| 46 | //it will determine the type id and call the right decode function |
---|
[203] | 47 | bool PacketDecoder::elaborate( ENetPacket* packet, int clientId ) |
---|
[199] | 48 | { |
---|
[203] | 49 | int client = clientId; |
---|
[332] | 50 | cout << "clientId: " << client << endl; //control cout, not important, just debugging info |
---|
| 51 | int id = (int)*packet->data; //the first 4 bytes are always the enet packet id |
---|
[199] | 52 | switch( id ) { |
---|
| 53 | case ACK: |
---|
[440] | 54 | acknowledgement( packet, clientId ); |
---|
[199] | 55 | return true; |
---|
| 56 | break; |
---|
| 57 | case MOUSE: |
---|
[440] | 58 | mousem( packet, clientId ); |
---|
[199] | 59 | return true; |
---|
| 60 | break; |
---|
| 61 | case KEYBOARD: |
---|
[440] | 62 | keystrike( packet, clientId ); |
---|
[199] | 63 | return true; |
---|
| 64 | break; |
---|
| 65 | case CHAT: |
---|
[440] | 66 | chatMessage( packet, clientId ); |
---|
[199] | 67 | return true; |
---|
| 68 | break; |
---|
[332] | 69 | case GAMESTATE: |
---|
| 70 | gstate( packet ); |
---|
| 71 | return true; |
---|
| 72 | break; |
---|
[437] | 73 | case CLASSID: |
---|
| 74 | clid(packet); |
---|
| 75 | return true; |
---|
| 76 | break; |
---|
[199] | 77 | } |
---|
| 78 | return false; |
---|
| 79 | } |
---|
| 80 | |
---|
[223] | 81 | //following are the decode functions for the data of the packets |
---|
| 82 | |
---|
[440] | 83 | void PacketDecoder::acknowledgement( ENetPacket* packet, int clientId ) |
---|
[199] | 84 | { |
---|
| 85 | ack* a = new ack; |
---|
[332] | 86 | *a = *(ack*)packet->data; //press pattern of ack on new data |
---|
[514] | 87 | |
---|
[332] | 88 | //clean memory |
---|
| 89 | enet_packet_destroy( packet ); |
---|
[514] | 90 | |
---|
[332] | 91 | printAck( a ); //debug info |
---|
[199] | 92 | } |
---|
| 93 | |
---|
[440] | 94 | void PacketDecoder::mousem( ENetPacket* packet, int clientId ) |
---|
[199] | 95 | { |
---|
| 96 | mouse* mouseMove = new mouse; |
---|
[332] | 97 | //copy data of packet->data to new struct |
---|
[514] | 98 | *mouseMove = *(mouse*)packet->data; |
---|
| 99 | |
---|
[332] | 100 | //clean memory |
---|
| 101 | enet_packet_destroy( packet ); |
---|
[514] | 102 | |
---|
[332] | 103 | printMouse( mouseMove ); //debug info |
---|
[199] | 104 | } |
---|
| 105 | |
---|
[440] | 106 | void PacketDecoder::keystrike( ENetPacket* packet, int clientId ) |
---|
[199] | 107 | { |
---|
| 108 | keyboard* key = new keyboard; |
---|
[332] | 109 | *key = *(keyboard*)packet->data; //see above |
---|
[514] | 110 | |
---|
[332] | 111 | //clean memory |
---|
| 112 | enet_packet_destroy( packet ); |
---|
[514] | 113 | |
---|
[332] | 114 | printKey( key ); //debug info |
---|
| 115 | |
---|
[199] | 116 | } |
---|
| 117 | |
---|
[440] | 118 | void PacketDecoder::chatMessage( ENetPacket* packet, int clientId ) |
---|
[199] | 119 | { |
---|
| 120 | chat* chatting = new chat; |
---|
[332] | 121 | chatting->id = (int)*packet->data; //first copy id into new struct |
---|
| 122 | //since the chat message is a char*, allocate the memory needed |
---|
[514] | 123 | char* reserve = new char[packet->dataLength-4]; |
---|
[332] | 124 | //copy the transmitted bytestream into the new generated char*, |
---|
[514] | 125 | //note the lenght of the message is represented as "packet->dataLength-sizeof( int )" |
---|
[332] | 126 | memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) ); |
---|
| 127 | //put pointer of chatting struct to the begining of the new generated char* |
---|
[199] | 128 | chatting->message = reserve; |
---|
[514] | 129 | |
---|
[332] | 130 | //clean memory |
---|
| 131 | enet_packet_destroy( packet ); |
---|
[514] | 132 | |
---|
[440] | 133 | processChat( chatting, clientId ); //debug info |
---|
[514] | 134 | |
---|
[199] | 135 | } |
---|
[223] | 136 | |
---|
[332] | 137 | void PacketDecoder::gstate( ENetPacket* packet ) |
---|
| 138 | { |
---|
[403] | 139 | GameStateCompressed* currentState = new GameStateCompressed; |
---|
[332] | 140 | //since it's not alowed to use void* for pointer arithmetic |
---|
| 141 | unsigned char* data = (unsigned char*)packet->data; |
---|
[403] | 142 | //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int ) |
---|
[332] | 143 | memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) ); |
---|
[403] | 144 | //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th |
---|
[332] | 145 | //position of the data stream, data+2*sizeof( int ) |
---|
[403] | 146 | memcpy( (void*)&(currentState->compsize), (const void*)(data+2*sizeof( int )), sizeof( int) ); |
---|
| 147 | //size of uncompressed data |
---|
| 148 | memcpy( (void*)&(currentState->normsize), (const void*)(data+3*sizeof( int )), sizeof( int ) ); |
---|
[437] | 149 | //since the packetgenerator was changed, due to a new parameter, change this function too |
---|
| 150 | memcpy( (void*)&(currentState->diffed), (const void*)(data+4*sizeof(int)), sizeof(bool)); |
---|
[332] | 151 | //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream |
---|
[405] | 152 | currentState->data = (unsigned char*)(malloc( currentState->compsize )); |
---|
[403] | 153 | //copy the GameStateCompressed data |
---|
[437] | 154 | memcpy( (void*)(currentState->data), (const void*)(data+4*sizeof( int ) + sizeof(bool)), currentState->compsize ); |
---|
[514] | 155 | |
---|
[332] | 156 | //clean memory |
---|
| 157 | enet_packet_destroy( packet ); |
---|
[403] | 158 | //run processGameStateCompressed |
---|
[374] | 159 | //TODO: not yet implemented! |
---|
| 160 | //processGamestate(currentState); |
---|
[332] | 161 | } |
---|
| 162 | |
---|
[400] | 163 | void PacketDecoder::clid( ENetPacket *packet) |
---|
| 164 | { |
---|
| 165 | classid* cid = new classid; |
---|
| 166 | cid->length = ((classid*)(packet->data))->length; |
---|
| 167 | cid->id = ((classid *)(packet->data))->id; |
---|
[415] | 168 | cid->clid = ((classid *)(packet->data))->clid; |
---|
[400] | 169 | cid->message = (const char *)malloc(cid->length); |
---|
| 170 | enet_packet_destroy( packet ); |
---|
[401] | 171 | processClassid(cid); |
---|
[400] | 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
[369] | 175 | // now the data processing functions: |
---|
| 176 | |
---|
[440] | 177 | void PacketDecoder::processChat( chat *data, int clientId){ |
---|
| 178 | printChat(data, clientId); |
---|
[369] | 179 | } |
---|
| 180 | |
---|
[400] | 181 | void PacketDecoder::processClassid( classid *cid){ |
---|
| 182 | printClassid(cid); |
---|
| 183 | return; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | |
---|
[223] | 188 | //these are some print functions for test stuff |
---|
| 189 | |
---|
[199] | 190 | void PacketDecoder::printAck( ack* data ) |
---|
| 191 | { |
---|
| 192 | cout << "data id: " << data->id << endl; |
---|
| 193 | cout << "data: " << data->a << endl; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | void PacketDecoder::printMouse( mouse* data ) |
---|
| 197 | { |
---|
| 198 | cout << "data id: " << data->id << endl; |
---|
| 199 | cout << "data: " << data->x << " " << data->y << endl; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | void PacketDecoder::printKey( keyboard* data ) |
---|
| 203 | { |
---|
| 204 | cout << "data id: " << data->id << endl; |
---|
| 205 | cout << "data: " << (char)data->press << endl; |
---|
| 206 | } |
---|
| 207 | |
---|
[440] | 208 | void PacketDecoder::printChat( chat* data, int clientId ) |
---|
[199] | 209 | { |
---|
[440] | 210 | if(clientId!=CLIENTID_CLIENT) |
---|
| 211 | cout << "client: " << clientId << endl; |
---|
[199] | 212 | cout << "data id: " << data->id << endl; |
---|
| 213 | cout << "data: " << data->message << endl; |
---|
| 214 | } |
---|
[332] | 215 | |
---|
[403] | 216 | void PacketDecoder::printGamestate( GameStateCompressed* data ) |
---|
[332] | 217 | { |
---|
[403] | 218 | cout << "id of GameStateCompressed: " << data->id << endl; |
---|
[405] | 219 | cout << "size of GameStateCompressed: " << data->compsize << endl; |
---|
[332] | 220 | } |
---|
[400] | 221 | |
---|
| 222 | void PacketDecoder::printClassid( classid *cid) |
---|
| 223 | { |
---|
| 224 | cout << "id of classid: " << cid->id << endl; |
---|
| 225 | cout << "size of classid: " << cid->length << endl; |
---|
[415] | 226 | cout << "ID of classid: " << cid->clid <<endl; |
---|
[400] | 227 | cout << "data of classid: " << cid->message <<endl; |
---|
| 228 | } |
---|