Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 620 was 620, checked in by scheusso, 17 years ago

gamestatehandling, error correction

File size: 7.4 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
45PacketDecoder::~PacketDecoder(){}
46
47//call this function out of an instance of PacketDecoder
48//it will determine the type id and call the right decode function
49bool PacketDecoder::elaborate( ENetPacket* packet, int clientId )
50{
51        int client = clientId;
52        cout << "clientId: " << client << endl; //control cout, not important, just debugging info
53        int id = (int)*packet->data; //the first 4 bytes are always the enet packet id
54        switch( id ) {
55        case ACK:
56                acknowledgement( packet, clientId );
57                return true;
58                break;
59        case MOUSE:
60                mousem( packet, clientId );
61                return true;
62                break;
63        case KEYBOARD:
64                keystrike( packet, clientId );
65                return true;
66                break;
67        case CHAT:
68                chatMessage( packet, clientId );
69                return true;
70                break;
71        case GAMESTATE:
72                gstate( packet );
73                return true;
74                break;
75    case CLASSID:
76        clid(packet);
77        return true;
78        break;
79        }
80        return false;
81}
82
83//following are the decode functions for the data of the packets
84
85void PacketDecoder::acknowledgement( ENetPacket* packet, int clientId )
86{
87        ack* a = new ack;
88        *a = *(ack*)packet->data; //press pattern of ack on new data
89
90
91  processAck( a, clientId ); //debug info
92 
93  //clean memory
94  enet_packet_destroy( packet );
95}
96
97void PacketDecoder::mousem( ENetPacket* packet, int clientId )
98{
99        mouse* mouseMove = new mouse;
100        //copy data of packet->data to new struct
101        *mouseMove = *(mouse*)packet->data;
102
103        //clean memory
104        enet_packet_destroy( packet );
105
106        printMouse( mouseMove ); //debug info
107}
108
109void PacketDecoder::keystrike( ENetPacket* packet, int clientId )
110{
111        keyboard* key = new keyboard;
112        *key = *(keyboard*)packet->data; //see above
113
114        //clean memory
115        enet_packet_destroy( packet );
116
117        printKey( key ); //debug info
118
119}
120
121void PacketDecoder::chatMessage( ENetPacket* packet, int clientId )
122{
123        chat* chatting = new chat;
124        chatting->id = (int)*packet->data; //first copy id into new struct
125        //since the chat message is a char*, allocate the memory needed
126        char* reserve = new char[packet->dataLength-4];
127        //copy the transmitted bytestream into the new generated char*,
128        //note the lenght of the message is represented as "packet->dataLength-sizeof( int )"
129        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) );
130        //put pointer of chatting struct to the begining of the new generated char*
131        chatting->message = reserve;
132
133        //clean memory
134        enet_packet_destroy( packet );
135
136        processChat( chatting, clientId ); //debug info
137
138}
139
140void PacketDecoder::gstate( ENetPacket* packet )
141{
142        GameStateCompressed* currentState = new GameStateCompressed;
143        //since it's not alowed to use void* for pointer arithmetic
144        unsigned char* data = (unsigned char*)packet->data;
145        //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int )
146  //memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) );
147  currentState->id = (int)*(data+sizeof(int));
148        //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th
149        //position of the data stream, data+2*sizeof( int )
150//      memcpy( (void*)&(currentState->compsize), (const void*)(data+2*sizeof( int )), sizeof( int) );
151  currentState->compsize = (int)*(data+2*sizeof(int));
152        //size of uncompressed data
153//      memcpy( (void*)&(currentState->normsize), (const void*)(data+3*sizeof( int )), sizeof( int ) );
154  currentState->normsize = (int)*(data+3*sizeof(int));
155        //since the packetgenerator was changed, due to a new parameter, change this function too
156//      memcpy( (void*)&(currentState->diffed), (const void*)(data+4*sizeof(int)), sizeof(bool));
157  currentState->diffed = (bool)*(data+4*sizeof(int));
158        //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
159        currentState->data = (unsigned char*)(malloc( currentState->compsize ));
160  if(currentState->data==NULL)
161    std::cout << "memory leak" << std::endl;
162        //copy the GameStateCompressed data
163  //std::cout << "packet size (enet): " << packet->dataLength << std::endl;
164  //std::cout << "totallen: " << 4*sizeof(int)+sizeof(bool)+currentState->compsize << std::endl;
165        memcpy( (void*)(currentState->data), (const void*)(data+4*sizeof( int ) + sizeof(bool)), currentState->compsize );
166
167        //clean memory
168        enet_packet_destroy( packet );
169  //run processGameStateCompressed
170  //TODO: not yet implemented!
171  processGamestate(currentState);
172}
173
174void PacketDecoder::clid( ENetPacket *packet)
175{
176        classid* cid = new classid;
177        cid->length = ((classid*)(packet->data))->length;
178        cid->id = ((classid *)(packet->data))->id;
179        cid->clid = ((classid *)(packet->data))->clid;
180        cid->message = (const char *)malloc(cid->length);
181        enet_packet_destroy( packet );
182        processClassid(cid);
183}
184
185
186// now the data processing functions:
187
188void PacketDecoder::processChat( chat *data, int clientId){
189  printChat(data, clientId);
190}
191
192void PacketDecoder::processGamestate( GameStateCompressed *state ){
193 
194}
195
196void PacketDecoder::processClassid( classid *cid){
197  printClassid(cid);
198  return;
199}
200
201void PacketDecoder::processAck( ack *data, int clientID){
202  printAck(data);
203  return;
204}
205
206
207//these are some print functions for test stuff
208
209void PacketDecoder::printAck( ack* data )
210{
211        cout << "data id: " << data->id << endl;
212        cout << "data:    " << data->a << endl;
213}
214
215void PacketDecoder::printMouse( mouse* data )
216{
217        cout << "data id: " << data->id << endl;
218        cout << "data:    " << data->x << " " << data->y << endl;
219}
220
221void PacketDecoder::printKey( keyboard* data )
222{
223        cout << "data id: " << data->id << endl;
224        cout << "data:    " << (char)data->press << endl;
225}
226
227void PacketDecoder::printChat( chat* data, int clientId )
228{
229  if(clientId!=CLIENTID_CLIENT)
230    cout << "client: " << clientId << endl;
231        cout << "data id: " << data->id << endl;
232        cout << "data:    " << data->message << endl;
233}
234
235void PacketDecoder::printGamestate( GameStateCompressed* data )
236{
237        cout << "id of GameStateCompressed:   " << data->id << endl;
238        cout << "size of GameStateCompressed: " << data->compsize << endl;
239}
240
241void PacketDecoder::printClassid( classid *cid)
242{
243        cout << "id of classid:    " << cid->id << endl;
244        cout << "size of classid:  " << cid->length << endl;
245        cout << "ID of classid:    " << cid->clid <<endl;
246        cout << "data of classid:  " << cid->message <<endl;
247}
Note: See TracBrowser for help on using the repository browser.