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 | |
---|
13 | using namespace std; |
---|
14 | using namespace network; |
---|
15 | |
---|
16 | PacketDecoder::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 |
---|
20 | bool 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 | |
---|
48 | void PacketDecoder::acknowledgement( ENetPacket* packet ) |
---|
49 | { |
---|
50 | ack* a = new ack; |
---|
51 | *a = *(ack*)packet->data; |
---|
52 | printAck( a ); |
---|
53 | } |
---|
54 | |
---|
55 | void PacketDecoder::mousem( ENetPacket* packet ) |
---|
56 | { |
---|
57 | mouse* mouseMove = new mouse; |
---|
58 | *mouseMove = *(mouse*)packet->data; |
---|
59 | printMouse( mouseMove ); |
---|
60 | } |
---|
61 | |
---|
62 | void PacketDecoder::keystrike( ENetPacket* packet ) |
---|
63 | { |
---|
64 | keyboard* key = new keyboard; |
---|
65 | *key = *(keyboard*)packet->data; |
---|
66 | printKey( key ); |
---|
67 | } |
---|
68 | |
---|
69 | void 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 | |
---|
81 | void PacketDecoder::printAck( ack* data ) |
---|
82 | { |
---|
83 | cout << "data id: " << data->id << endl; |
---|
84 | cout << "data: " << data->a << endl; |
---|
85 | } |
---|
86 | |
---|
87 | void PacketDecoder::printMouse( mouse* data ) |
---|
88 | { |
---|
89 | cout << "data id: " << data->id << endl; |
---|
90 | cout << "data: " << data->x << " " << data->y << endl; |
---|
91 | } |
---|
92 | |
---|
93 | void PacketDecoder::printKey( keyboard* data ) |
---|
94 | { |
---|
95 | cout << "data id: " << data->id << endl; |
---|
96 | cout << "data: " << (char)data->press << endl; |
---|
97 | } |
---|
98 | |
---|
99 | void PacketDecoder::printChat( chat* data ) |
---|
100 | { |
---|
101 | cout << "data id: " << data->id << endl; |
---|
102 | cout << "blablabla" << endl; |
---|
103 | cout << "data: " << data->message << endl; |
---|
104 | } |
---|