Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 346 was 341, checked in by rgrieder, 17 years ago
  • removed folder src/class_hierarchy
  • removed a few unnec. files in src/
  • added a few files in src/orxonox to make weapon system and hud work
  • restructured all CMLs
  • adjusted all the #include directives (only one include directory now: src)
  • moved ENET library linkage to CML in src/orxonox
File size: 4.2 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        printChat( 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}
124
125//these are some print functions for test stuff
126
127void PacketDecoder::printAck( ack* data )
128{
129        cout << "data id: " << data->id << endl;
130        cout << "data:    " << data->a << endl;
131}
132
133void PacketDecoder::printMouse( mouse* data )
134{
135        cout << "data id: " << data->id << endl;
136        cout << "data:    " << data->x << " " << data->y << endl;
137}
138
139void PacketDecoder::printKey( keyboard* data )
140{
141        cout << "data id: " << data->id << endl;
142        cout << "data:    " << (char)data->press << endl;
143}
144
145void PacketDecoder::printChat( chat* data )
146{
147        cout << "data id: " << data->id << endl;
148        cout << "data:    " << data->message << endl;
149}
150
151void PacketDecoder::printGamestate( GameState* data )
152{
153        cout << "id of gamestate:   " << data->id << endl;
154        cout << "size of gamestate: " << data->size << endl;
155}
Note: See TracBrowser for help on using the repository browser.