Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 381 was 374, checked in by rgrieder, 17 years ago
  • commented 2 functions to make the network source compile
File size: 4.4 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;
[199]46        }
47        return false;
48}
49
[223]50//following are the decode functions for the data of the packets
51
[199]52void PacketDecoder::acknowledgement( ENetPacket* packet )
53{
54        ack* a = new ack;
[332]55        *a = *(ack*)packet->data; //press pattern of ack on new data
56       
57        //clean memory
58        enet_packet_destroy( packet );
59       
60        printAck( a ); //debug info
[199]61}
62
63void PacketDecoder::mousem( ENetPacket* packet )
64{
65        mouse* mouseMove = new mouse;
[332]66        //copy data of packet->data to new struct
67        *mouseMove = *(mouse*)packet->data; 
68       
69        //clean memory
70        enet_packet_destroy( packet );
71       
72        printMouse( mouseMove ); //debug info
[199]73}
74
75void PacketDecoder::keystrike( ENetPacket* packet )
76{
77        keyboard* key = new keyboard;
[332]78        *key = *(keyboard*)packet->data; //see above
79       
80        //clean memory
81        enet_packet_destroy( packet );
82       
83        printKey( key ); //debug info
84
[199]85}
86
87void PacketDecoder::chatMessage( ENetPacket* packet )
88{
89        chat* chatting = new chat;
[332]90        chatting->id = (int)*packet->data; //first copy id into new struct
91        //since the chat message is a char*, allocate the memory needed
92        char* reserve = new char[packet->dataLength-4]; 
93        //copy the transmitted bytestream into the new generated char*,
94        //note the lenght of the message is represented as "packet->dataLength-sizeof( int )"
95        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) );
96        //put pointer of chatting struct to the begining of the new generated char*
[199]97        chatting->message = reserve;
[332]98       
99        //clean memory
100        enet_packet_destroy( packet );
101       
[369]102        processChat( chatting ); //debug info
[332]103       
[199]104}
[223]105
[332]106void PacketDecoder::gstate( ENetPacket* packet )
107{
108        GameState* currentState = new GameState;
109        //since it's not alowed to use void* for pointer arithmetic
110        unsigned char* data = (unsigned char*)packet->data;
111        //copy the gamestate id into the struct, which is located at second place data+sizeof( int )
112        memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) );
113        //copy the size of the gamestate data into the new gamestate struct, located at 3th
114        //position of the data stream, data+2*sizeof( int )
115        memcpy( (void*)&(currentState->size), (const void*)(data+2*sizeof( int )), sizeof( int) );
116        //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
117        currentState->data = (unsigned char*)(malloc( currentState->size ));
118        //copy the gamestate data
119        memcpy( (void*)(currentState->data), (const void*)(data+3*sizeof( int )), currentState->size );
[369]120 
[332]121        //clean memory
122        enet_packet_destroy( packet );
[369]123  //run processGamestate
[374]124  //TODO: not yet implemented!
125  //processGamestate(currentState);
[332]126}
127
[369]128// now the data processing functions:
129
130void PacketDecoder::processChat( chat *data){
131  printChat(data);
132}
133
[223]134//these are some print functions for test stuff
135
[199]136void PacketDecoder::printAck( ack* data )
137{
138        cout << "data id: " << data->id << endl;
139        cout << "data:    " << data->a << endl;
140}
141
142void PacketDecoder::printMouse( mouse* data )
143{
144        cout << "data id: " << data->id << endl;
145        cout << "data:    " << data->x << " " << data->y << endl;
146}
147
148void PacketDecoder::printKey( keyboard* data )
149{
150        cout << "data id: " << data->id << endl;
151        cout << "data:    " << (char)data->press << endl;
152}
153
154void PacketDecoder::printChat( chat* data )
155{
156        cout << "data id: " << data->id << endl;
157        cout << "data:    " << data->message << endl;
158}
[332]159
160void PacketDecoder::printGamestate( GameState* data )
161{
162        cout << "id of gamestate:   " << data->id << endl;
163        cout << "size of gamestate: " << data->size << endl;
164}
Note: See TracBrowser for help on using the repository browser.