Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 525 was 514, checked in by nicolasc, 17 years ago

added copyright notice

File size: 6.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Dumeni Manatschal, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/*
29 * Class contains functions to determine and decode incomming packages
30 * ->don't read this without the class PacketGenerator, since they belong together
31 *
32 * Autor: Dumeni Manatschal
33 *
34*/
35
36#include <enet/enet.h>
37#include "PacketManager.h"
38#include <iostream>
39
40using namespace std;
41using namespace network;
42
43PacketDecoder::PacketDecoder(){}
44
45//call this function out of an instance of PacketDecoder
46//it will determine the type id and call the right decode function
47bool PacketDecoder::elaborate( ENetPacket* packet, int clientId )
48{
49        int client = clientId;
50        cout << "clientId: " << client << endl; //control cout, not important, just debugging info
51        int id = (int)*packet->data; //the first 4 bytes are always the enet packet id
52        switch( id ) {
53        case ACK:
54                acknowledgement( packet, clientId );
55                return true;
56                break;
57        case MOUSE:
58                mousem( packet, clientId );
59                return true;
60                break;
61        case KEYBOARD:
62                keystrike( packet, clientId );
63                return true;
64                break;
65        case CHAT:
66                chatMessage( packet, clientId );
67                return true;
68                break;
69        case GAMESTATE:
70                gstate( packet );
71                return true;
72                break;
73    case CLASSID:
74        clid(packet);
75        return true;
76        break;
77        }
78        return false;
79}
80
81//following are the decode functions for the data of the packets
82
83void PacketDecoder::acknowledgement( ENetPacket* packet, int clientId )
84{
85        ack* a = new ack;
86        *a = *(ack*)packet->data; //press pattern of ack on new data
87
88        //clean memory
89        enet_packet_destroy( packet );
90
91        printAck( a ); //debug info
92}
93
94void PacketDecoder::mousem( ENetPacket* packet, int clientId )
95{
96        mouse* mouseMove = new mouse;
97        //copy data of packet->data to new struct
98        *mouseMove = *(mouse*)packet->data;
99
100        //clean memory
101        enet_packet_destroy( packet );
102
103        printMouse( mouseMove ); //debug info
104}
105
106void PacketDecoder::keystrike( ENetPacket* packet, int clientId )
107{
108        keyboard* key = new keyboard;
109        *key = *(keyboard*)packet->data; //see above
110
111        //clean memory
112        enet_packet_destroy( packet );
113
114        printKey( key ); //debug info
115
116}
117
118void PacketDecoder::chatMessage( ENetPacket* packet, int clientId )
119{
120        chat* chatting = new chat;
121        chatting->id = (int)*packet->data; //first copy id into new struct
122        //since the chat message is a char*, allocate the memory needed
123        char* reserve = new char[packet->dataLength-4];
124        //copy the transmitted bytestream into the new generated char*,
125        //note the lenght of the message is represented as "packet->dataLength-sizeof( int )"
126        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) );
127        //put pointer of chatting struct to the begining of the new generated char*
128        chatting->message = reserve;
129
130        //clean memory
131        enet_packet_destroy( packet );
132
133        processChat( chatting, clientId ); //debug info
134
135}
136
137void PacketDecoder::gstate( ENetPacket* packet )
138{
139        GameStateCompressed* currentState = new GameStateCompressed;
140        //since it's not alowed to use void* for pointer arithmetic
141        unsigned char* data = (unsigned char*)packet->data;
142        //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int )
143        memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) );
144        //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th
145        //position of the data stream, data+2*sizeof( int )
146        memcpy( (void*)&(currentState->compsize), (const void*)(data+2*sizeof( int )), sizeof( int) );
147        //size of uncompressed data
148        memcpy( (void*)&(currentState->normsize), (const void*)(data+3*sizeof( int )), sizeof( int ) );
149        //since the packetgenerator was changed, due to a new parameter, change this function too
150        memcpy( (void*)&(currentState->diffed), (const void*)(data+4*sizeof(int)), sizeof(bool));
151        //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
152        currentState->data = (unsigned char*)(malloc( currentState->compsize ));
153        //copy the GameStateCompressed data
154        memcpy( (void*)(currentState->data), (const void*)(data+4*sizeof( int ) + sizeof(bool)), currentState->compsize );
155
156        //clean memory
157        enet_packet_destroy( packet );
158  //run processGameStateCompressed
159  //TODO: not yet implemented!
160  //processGamestate(currentState);
161}
162
163void PacketDecoder::clid( ENetPacket *packet)
164{
165        classid* cid = new classid;
166        cid->length = ((classid*)(packet->data))->length;
167        cid->id = ((classid *)(packet->data))->id;
168        cid->clid = ((classid *)(packet->data))->clid;
169        cid->message = (const char *)malloc(cid->length);
170        enet_packet_destroy( packet );
171        processClassid(cid);
172}
173
174
175// now the data processing functions:
176
177void PacketDecoder::processChat( chat *data, int clientId){
178  printChat(data, clientId);
179}
180
181void PacketDecoder::processClassid( classid *cid){
182  printClassid(cid);
183  return;
184}
185
186
187
188//these are some print functions for test stuff
189
190void PacketDecoder::printAck( ack* data )
191{
192        cout << "data id: " << data->id << endl;
193        cout << "data:    " << data->a << endl;
194}
195
196void PacketDecoder::printMouse( mouse* data )
197{
198        cout << "data id: " << data->id << endl;
199        cout << "data:    " << data->x << " " << data->y << endl;
200}
201
202void PacketDecoder::printKey( keyboard* data )
203{
204        cout << "data id: " << data->id << endl;
205        cout << "data:    " << (char)data->press << endl;
206}
207
208void PacketDecoder::printChat( chat* data, int clientId )
209{
210  if(clientId!=CLIENTID_CLIENT)
211    cout << "client: " << clientId << endl;
212        cout << "data id: " << data->id << endl;
213        cout << "data:    " << data->message << endl;
214}
215
216void PacketDecoder::printGamestate( GameStateCompressed* data )
217{
218        cout << "id of GameStateCompressed:   " << data->id << endl;
219        cout << "size of GameStateCompressed: " << data->compsize << endl;
220}
221
222void PacketDecoder::printClassid( classid *cid)
223{
224        cout << "id of classid:    " << cid->id << endl;
225        cout << "size of classid:  " << cid->length << endl;
226        cout << "ID of classid:    " << cid->clid <<endl;
227        cout << "data of classid:  " << cid->message <<endl;
228}
Note: See TracBrowser for help on using the repository browser.