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
Line 
1/*
2 * Class contains functions to determine and decode incomming packages
3 * ->don't read this without the class PacketGenerator, since they belong together
4 *
5 * Autor: Dumeni Manatschal
6 *
7*/
8
9#include <enet/enet.h>
10#include "PacketManager.h"
11#include <iostream>
12
13using namespace std;
14using namespace network;
15
16PacketDecoder::PacketDecoder(){}
17
18//call this function out of an instance of PacketDecoder
19//it will determine the type id and call the right decode function
20bool PacketDecoder::elaborate( ENetPacket* packet, int clientId )
21{
22        int client = clientId;
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
25        switch( id ) {
26        case ACK:
27                acknowledgement( packet );
28                return true;
29                break;
30        case MOUSE:
31                mousem( packet );
32                return true;
33                break;
34        case KEYBOARD:
35                keystrike( packet );
36                return true;
37                break;
38        case CHAT:
39                chatMessage( packet );
40                return true;
41                break;
42        case GAMESTATE:
43                gstate( packet );
44                return true;
45                break;
46          case CLASSID:
47                clid(packet);
48                return true;
49                break;
50        }
51        return false;
52}
53
54//following are the decode functions for the data of the packets
55
56void PacketDecoder::acknowledgement( ENetPacket* packet )
57{
58        ack* a = new ack;
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
65}
66
67void PacketDecoder::mousem( ENetPacket* packet )
68{
69        mouse* mouseMove = new mouse;
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
77}
78
79void PacketDecoder::keystrike( ENetPacket* packet )
80{
81        keyboard* key = new keyboard;
82        *key = *(keyboard*)packet->data; //see above
83       
84        //clean memory
85        enet_packet_destroy( packet );
86       
87        printKey( key ); //debug info
88
89}
90
91void PacketDecoder::chatMessage( ENetPacket* packet )
92{
93        chat* chatting = new chat;
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*
101        chatting->message = reserve;
102       
103        //clean memory
104        enet_packet_destroy( packet );
105       
106        processChat( chatting ); //debug info
107       
108}
109
110void PacketDecoder::gstate( ENetPacket* packet )
111{
112        GameStateCompressed* currentState = new GameStateCompressed;
113        //since it's not alowed to use void* for pointer arithmetic
114        unsigned char* data = (unsigned char*)packet->data;
115        //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int )
116        memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) );
117        //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th
118        //position of the data stream, data+2*sizeof( int )
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 ) );
122        //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
123        currentState->data = (unsigned char*)(malloc( currentState->compsize ));
124        //copy the GameStateCompressed data
125        memcpy( (void*)(currentState->data), (const void*)(data+4*sizeof( int )), currentState->compsize );
126 
127        //clean memory
128        enet_packet_destroy( packet );
129  //run processGameStateCompressed
130  //TODO: not yet implemented!
131  //processGamestate(currentState);
132}
133
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;
139        cid->clid = ((classid *)(packet->data))->clid;
140        cid->message = (const char *)malloc(cid->length);
141        enet_packet_destroy( packet );
142        processClassid(cid);
143}
144
145
146// now the data processing functions:
147
148void PacketDecoder::processChat( chat *data){
149  printChat(data);
150}
151
152void PacketDecoder::processClassid( classid *cid){
153  printClassid(cid);
154  return;
155}
156
157
158
159//these are some print functions for test stuff
160
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}
184
185void PacketDecoder::printGamestate( GameStateCompressed* data )
186{
187        cout << "id of GameStateCompressed:   " << data->id << endl;
188        cout << "size of GameStateCompressed: " << data->compsize << endl;
189}
190
191void PacketDecoder::printClassid( classid *cid)
192{
193        cout << "id of classid:    " << cid->id << endl;
194        cout << "size of classid:  " << cid->length << endl;
195        cout << "ID of classid:    " << cid->clid <<endl;
196        cout << "data of classid:  " << cid->message <<endl;
197}
Note: See TracBrowser for help on using the repository browser.