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
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        }
47        return false;
48}
49
50//following are the decode functions for the data of the packets
51
52void PacketDecoder::acknowledgement( ENetPacket* packet )
53{
54        ack* a = new ack;
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
61}
62
63void PacketDecoder::mousem( ENetPacket* packet )
64{
65        mouse* mouseMove = new mouse;
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
73}
74
75void PacketDecoder::keystrike( ENetPacket* packet )
76{
77        keyboard* key = new keyboard;
78        *key = *(keyboard*)packet->data; //see above
79       
80        //clean memory
81        enet_packet_destroy( packet );
82       
83        printKey( key ); //debug info
84
85}
86
87void PacketDecoder::chatMessage( ENetPacket* packet )
88{
89        chat* chatting = new chat;
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*
97        chatting->message = reserve;
98       
99        //clean memory
100        enet_packet_destroy( packet );
101       
102        processChat( chatting ); //debug info
103       
104}
105
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 );
120 
121        //clean memory
122        enet_packet_destroy( packet );
123  //run processGamestate
124  //TODO: not yet implemented!
125  //processGamestate(currentState);
126}
127
128// now the data processing functions:
129
130void PacketDecoder::processChat( chat *data){
131  printChat(data);
132}
133
134//these are some print functions for test stuff
135
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}
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.