Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 416 was 415, checked in by scheusso, 17 years ago

errorless

File size: 5.5 KB
RevLine 
[223]1/*
2 * Class contains functions to determine and decode incomming packages
[332]3 * ->don't read this without the class PacketGenerator, since they belong together
[223]4 *
5 * Autor: Dumeni Manatschal
6 *
7*/
8
[341]9#include <enet/enet.h>
[199]10#include "PacketManager.h"
11#include <iostream>
12
13using namespace std;
14using namespace network;
15
16PacketDecoder::PacketDecoder(){}
17
[223]18//call this function out of an instance of PacketDecoder
19//it will determine the type id and call the right decode function
[203]20bool PacketDecoder::elaborate( ENetPacket* packet, int clientId )
[199]21{
[203]22        int client = clientId;
[332]23        cout << "clientId: " << client << endl; //control cout, not important, just debugging info
24        int id = (int)*packet->data; //the first 4 bytes are always the enet packet id
[199]25        switch( id ) {
26        case ACK:
[203]27                acknowledgement( packet );
[199]28                return true;
29                break;
30        case MOUSE:
[203]31                mousem( packet );
[199]32                return true;
33                break;
34        case KEYBOARD:
[203]35                keystrike( packet );
[199]36                return true;
37                break;
38        case CHAT:
[203]39                chatMessage( packet );
[199]40                return true;
41                break;
[332]42        case GAMESTATE:
43                gstate( packet );
44                return true;
45                break;
[400]46          case CLASSID:
47                clid(packet);
48                return true;
49                break;
[199]50        }
51        return false;
52}
53
[223]54//following are the decode functions for the data of the packets
55
[199]56void PacketDecoder::acknowledgement( ENetPacket* packet )
57{
58        ack* a = new ack;
[332]59        *a = *(ack*)packet->data; //press pattern of ack on new data
60       
61        //clean memory
62        enet_packet_destroy( packet );
63       
64        printAck( a ); //debug info
[199]65}
66
67void PacketDecoder::mousem( ENetPacket* packet )
68{
69        mouse* mouseMove = new mouse;
[332]70        //copy data of packet->data to new struct
71        *mouseMove = *(mouse*)packet->data; 
72       
73        //clean memory
74        enet_packet_destroy( packet );
75       
76        printMouse( mouseMove ); //debug info
[199]77}
78
79void PacketDecoder::keystrike( ENetPacket* packet )
80{
81        keyboard* key = new keyboard;
[332]82        *key = *(keyboard*)packet->data; //see above
83       
84        //clean memory
85        enet_packet_destroy( packet );
86       
87        printKey( key ); //debug info
88
[199]89}
90
91void PacketDecoder::chatMessage( ENetPacket* packet )
92{
93        chat* chatting = new chat;
[332]94        chatting->id = (int)*packet->data; //first copy id into new struct
95        //since the chat message is a char*, allocate the memory needed
96        char* reserve = new char[packet->dataLength-4]; 
97        //copy the transmitted bytestream into the new generated char*,
98        //note the lenght of the message is represented as "packet->dataLength-sizeof( int )"
99        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) );
100        //put pointer of chatting struct to the begining of the new generated char*
[199]101        chatting->message = reserve;
[332]102       
103        //clean memory
104        enet_packet_destroy( packet );
105       
[369]106        processChat( chatting ); //debug info
[332]107       
[199]108}
[223]109
[332]110void PacketDecoder::gstate( ENetPacket* packet )
111{
[403]112        GameStateCompressed* currentState = new GameStateCompressed;
[332]113        //since it's not alowed to use void* for pointer arithmetic
114        unsigned char* data = (unsigned char*)packet->data;
[403]115        //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int )
[332]116        memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) );
[403]117        //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th
[332]118        //position of the data stream, data+2*sizeof( int )
[403]119        memcpy( (void*)&(currentState->compsize), (const void*)(data+2*sizeof( int )), sizeof( int) );
120        //size of uncompressed data
121        memcpy( (void*)&(currentState->normsize), (const void*)(data+3*sizeof( int )), sizeof( int ) );
[332]122        //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
[405]123        currentState->data = (unsigned char*)(malloc( currentState->compsize ));
[403]124        //copy the GameStateCompressed data
125        memcpy( (void*)(currentState->data), (const void*)(data+4*sizeof( int )), currentState->compsize );
[369]126 
[332]127        //clean memory
128        enet_packet_destroy( packet );
[403]129  //run processGameStateCompressed
[374]130  //TODO: not yet implemented!
131  //processGamestate(currentState);
[332]132}
133
[400]134void PacketDecoder::clid( ENetPacket *packet)
135{
136        classid* cid = new classid;
137        cid->length = ((classid*)(packet->data))->length;
138        cid->id = ((classid *)(packet->data))->id;
[415]139        cid->clid = ((classid *)(packet->data))->clid;
[400]140        cid->message = (const char *)malloc(cid->length);
141        enet_packet_destroy( packet );
[401]142        processClassid(cid);
[400]143}
144
145
[369]146// now the data processing functions:
147
148void PacketDecoder::processChat( chat *data){
149  printChat(data);
150}
151
[400]152void PacketDecoder::processClassid( classid *cid){
153  printClassid(cid);
154  return;
155}
156
157
158
[223]159//these are some print functions for test stuff
160
[199]161void PacketDecoder::printAck( ack* data )
162{
163        cout << "data id: " << data->id << endl;
164        cout << "data:    " << data->a << endl;
165}
166
167void PacketDecoder::printMouse( mouse* data )
168{
169        cout << "data id: " << data->id << endl;
170        cout << "data:    " << data->x << " " << data->y << endl;
171}
172
173void PacketDecoder::printKey( keyboard* data )
174{
175        cout << "data id: " << data->id << endl;
176        cout << "data:    " << (char)data->press << endl;
177}
178
179void PacketDecoder::printChat( chat* data )
180{
181        cout << "data id: " << data->id << endl;
182        cout << "data:    " << data->message << endl;
183}
[332]184
[403]185void PacketDecoder::printGamestate( GameStateCompressed* data )
[332]186{
[403]187        cout << "id of GameStateCompressed:   " << data->id << endl;
[405]188        cout << "size of GameStateCompressed: " << data->compsize << endl;
[332]189}
[400]190
191void PacketDecoder::printClassid( classid *cid)
192{
193        cout << "id of classid:    " << cid->id << endl;
194        cout << "size of classid:  " << cid->length << endl;
[415]195        cout << "ID of classid:    " << cid->clid <<endl;
[400]196        cout << "data of classid:  " << cid->message <<endl;
197}
Note: See TracBrowser for help on using the repository browser.