Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added gamestates to packetCode/Decode classes

File size: 2.7 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        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;
56        printAck( a );
57}
58
59void PacketDecoder::mousem( ENetPacket* packet )
60{
61        mouse* mouseMove = new mouse;
62        *mouseMove = *(mouse*)packet->data;
63        printMouse( mouseMove );
64}
65
66void PacketDecoder::keystrike( ENetPacket* packet )
67{
68        keyboard* key = new keyboard;
69        *key = *(keyboard*)packet->data;
70        printKey( key );
71}
72
73void PacketDecoder::chatMessage( ENetPacket* packet )
74{
75        chat* chatting = new chat;
76        chatting->id = (int)*packet->data;
77        char* reserve = new char[packet->dataLength-4];
78        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-4 );
79        chatting->message = reserve;
80        printChat( chatting );
81}
82
83void PacketDecoder::gstate( ENetPacket* packet )
84{
85        GameState* currentState = new GameState;
86        unsigned char* data = (unsigned char*)packet->data;
87        memcpy( (void*)&(currentState->size), (const void*)(data+sizeof( int )), sizeof(int) );
88        memcpy( (void*)(currentState->data), (const void*)(data+2*sizeof( int )), currentState->size );
89}
90
91//these are some print functions for test stuff
92
93void PacketDecoder::printAck( ack* data )
94{
95        cout << "data id: " << data->id << endl;
96        cout << "data:    " << data->a << endl;
97}
98
99void PacketDecoder::printMouse( mouse* data )
100{
101        cout << "data id: " << data->id << endl;
102        cout << "data:    " << data->x << " " << data->y << endl;
103}
104
105void PacketDecoder::printKey( keyboard* data )
106{
107        cout << "data id: " << data->id << endl;
108        cout << "data:    " << (char)data->press << endl;
109}
110
111void PacketDecoder::printChat( chat* data )
112{
113        cout << "data id: " << data->id << endl;
114        cout << "data:    " << data->message << endl;
115}
116
117void PacketDecoder::printGamestate( GameState* data )
118{
119        cout << "size of gamestate: " << data->size << endl;
120}
Note: See TracBrowser for help on using the repository browser.