Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 477 was 440, checked in by scheusso, 17 years ago

added dummyserver3.cc (implements a chatserver, including main)

File size: 5.9 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, clientId );
28                return true;
29                break;
30        case MOUSE:
31                mousem( packet, clientId );
32                return true;
33                break;
34        case KEYBOARD:
35                keystrike( packet, clientId );
36                return true;
37                break;
38        case CHAT:
39                chatMessage( packet, clientId );
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, int clientId )
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, int clientId )
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, int clientId )
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, int clientId )
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, clientId ); //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 the packetgenerator was changed, due to a new parameter, change this function too
123        memcpy( (void*)&(currentState->diffed), (const void*)(data+4*sizeof(int)), sizeof(bool));
124        //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
125        currentState->data = (unsigned char*)(malloc( currentState->compsize ));
126        //copy the GameStateCompressed data
127        memcpy( (void*)(currentState->data), (const void*)(data+4*sizeof( int ) + sizeof(bool)), currentState->compsize );
128 
129        //clean memory
130        enet_packet_destroy( packet );
131  //run processGameStateCompressed
132  //TODO: not yet implemented!
133  //processGamestate(currentState);
134}
135
136void PacketDecoder::clid( ENetPacket *packet)
137{
138        classid* cid = new classid;
139        cid->length = ((classid*)(packet->data))->length;
140        cid->id = ((classid *)(packet->data))->id;
141        cid->clid = ((classid *)(packet->data))->clid;
142        cid->message = (const char *)malloc(cid->length);
143        enet_packet_destroy( packet );
144        processClassid(cid);
145}
146
147
148// now the data processing functions:
149
150void PacketDecoder::processChat( chat *data, int clientId){
151  printChat(data, clientId);
152}
153
154void PacketDecoder::processClassid( classid *cid){
155  printClassid(cid);
156  return;
157}
158
159
160
161//these are some print functions for test stuff
162
163void PacketDecoder::printAck( ack* data )
164{
165        cout << "data id: " << data->id << endl;
166        cout << "data:    " << data->a << endl;
167}
168
169void PacketDecoder::printMouse( mouse* data )
170{
171        cout << "data id: " << data->id << endl;
172        cout << "data:    " << data->x << " " << data->y << endl;
173}
174
175void PacketDecoder::printKey( keyboard* data )
176{
177        cout << "data id: " << data->id << endl;
178        cout << "data:    " << (char)data->press << endl;
179}
180
181void PacketDecoder::printChat( chat* data, int clientId )
182{
183  if(clientId!=CLIENTID_CLIENT)
184    cout << "client: " << clientId << endl;
185        cout << "data id: " << data->id << endl;
186        cout << "data:    " << data->message << endl;
187}
188
189void PacketDecoder::printGamestate( GameStateCompressed* data )
190{
191        cout << "id of GameStateCompressed:   " << data->id << endl;
192        cout << "size of GameStateCompressed: " << data->compsize << endl;
193}
194
195void PacketDecoder::printClassid( classid *cid)
196{
197        cout << "id of classid:    " << cid->id << endl;
198        cout << "size of classid:  " << cid->length << endl;
199        cout << "ID of classid:    " << cid->clid <<endl;
200        cout << "data of classid:  " << cid->message <<endl;
201}
Note: See TracBrowser for help on using the repository browser.