Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/PacketDecoder.cc @ 223

Last change on this file since 223 was 223, checked in by dumenim, 16 years ago

dumenim added some comment to the code

File size: 2.2 KB
Line 
1/*
2 * Class contains functions to determine and decode incomming packages
3 *
4 * Autor: Dumeni Manatschal
5 *
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;
24        int id = (int)*packet->data;
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        }
43        return false;
44}
45
46//following are the decode functions for the data of the packets
47
48void PacketDecoder::acknowledgement( ENetPacket* packet )
49{
50        ack* a = new ack;
51        *a = *(ack*)packet->data;
52        printAck( a );
53}
54
55void PacketDecoder::mousem( ENetPacket* packet )
56{
57        mouse* mouseMove = new mouse;
58        *mouseMove = *(mouse*)packet->data;
59        printMouse( mouseMove );
60}
61
62void PacketDecoder::keystrike( ENetPacket* packet )
63{
64        keyboard* key = new keyboard;
65        *key = *(keyboard*)packet->data;
66        printKey( key );
67}
68
69void PacketDecoder::chatMessage( ENetPacket* packet )
70{
71        chat* chatting = new chat;
72        chatting->id = (int)*packet->data;
73        char* reserve = new char[packet->dataLength-4];
74        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-4 );
75        chatting->message = reserve;
76        printChat( chatting );
77}
78
79//these are some print functions for test stuff
80
81void PacketDecoder::printAck( ack* data )
82{
83        cout << "data id: " << data->id << endl;
84        cout << "data:    " << data->a << endl;
85}
86
87void PacketDecoder::printMouse( mouse* data )
88{
89        cout << "data id: " << data->id << endl;
90        cout << "data:    " << data->x << " " << data->y << endl;
91}
92
93void PacketDecoder::printKey( keyboard* data )
94{
95        cout << "data id: " << data->id << endl;
96        cout << "data:    " << (char)data->press << endl;
97}
98
99void PacketDecoder::printChat( chat* data )
100{
101        cout << "data id: " << data->id << endl;
102        cout << "data:    " << data->message << endl;
103}
Note: See TracBrowser for help on using the repository browser.