Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/PacketDecoder.cc @ 630

Last change on this file since 630 was 624, checked in by scheusso, 18 years ago

changes in gamestatehandling

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