Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Packet Decoder/Generator und PacketManager, generates and decodes packets send and receved by enet

File size: 2.3 KB
Line 
1#include "enet/enet.h"
2#include "PacketManager.h"
3#include <iostream>
4
5using namespace std;
6using namespace network;
7
8PacketDecoder::PacketDecoder(){}
9
10bool PacketDecoder::elaborate( ENetEvent* event )
11{
12        int id = (int)*event->packet->data;
13        switch( id ) {
14        case ACK:
15                acknowledgement( event->packet );
16                return true;
17                break;
18        case MOUSE:
19                mousem( event->packet );
20                return true;
21                break;
22        case KEYBOARD:
23                keystrike( event->packet );
24                return true;
25                break;
26        case CHAT:
27                chatMessage( event->packet );
28                return true;
29                break;
30        }
31        return false;
32}
33
34void PacketDecoder::acknowledgement( ENetPacket* packet )
35{
36        ack* a = new ack;
37        *a = *(ack*)packet->data;
38        printAck( a );
39}
40
41void PacketDecoder::mousem( ENetPacket* packet )
42{
43        mouse* mouseMove = new mouse;
44        *mouseMove = *(mouse*)packet->data;
45        printMouse( mouseMove );
46}
47
48void PacketDecoder::keystrike( ENetPacket* packet )
49{
50        keyboard* key = new keyboard;
51        *key = *(keyboard*)packet->data;
52        printKey( key );
53}
54
55void PacketDecoder::chatMessage( ENetPacket* packet )
56{
57        chat* chatting = new chat;
58        chatting->id = (int)*packet->data;
59        char* reserve = new char[packet->dataLength-4];
60        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-4 );
61        chatting->message = reserve;
62        printChat( chatting );
63}
64
65void PacketDecoder::printPeer( ENetPeer* peer )
66{
67        cout << "number of chanels:   " << peer->channelCount << endl;
68        cout << "incomming bandwidth: " << peer->incomingBandwidth << endl;
69        cout << "outgoing bandwidth:  " << peer->outgoingBandwidth << endl;
70        cout << "peer id:             " << peer->sessionID << endl;
71        cout << "outgoing peer id:    " << peer->outgoingPeerID << endl;
72        cout << "incomming peer id:   " << peer->incomingPeerID << endl;
73        cout << "state of peer:       " << peer->state << endl;
74}
75
76void PacketDecoder::printAck( ack* data )
77{
78        cout << "data id: " << data->id << endl;
79        cout << "data:    " << data->a << endl;
80}
81
82void PacketDecoder::printMouse( mouse* data )
83{
84        cout << "data id: " << data->id << endl;
85        cout << "data:    " << data->x << " " << data->y << endl;
86}
87
88void PacketDecoder::printKey( keyboard* data )
89{
90        cout << "data id: " << data->id << endl;
91        cout << "data:    " << (char)data->press << endl;
92}
93
94void PacketDecoder::printChat( chat* data )
95{
96        cout << "data id: " << data->id << endl;
97        cout << "data:    " << data->message << endl;
98}
Note: See TracBrowser for help on using the repository browser.