Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network3/src/network/PacketDecoder.cc @ 1245

Last change on this file since 1245 was 1245, checked in by scheusso, 16 years ago

different enhancements in input/network handling and synchronisation

File size: 10.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Dumeni Manatschal, (C) 2007
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/*
30* Class contains functions to determine and decode incomming packages
31* ->don't read this without the class PacketGenerator, since they belong together
32*
33* Autor: Dumeni Manatschal
34*
35*/
36
37#include "PacketTypes.h"
38#include "PacketManager.h"
39
40#include <iostream>
41
42#include "core/Debug.h"
43
44namespace network
45{
46
47  PacketDecoder::PacketDecoder(){}
48
49  PacketDecoder::~PacketDecoder(){}
50
51  //call this function out of an instance of PacketDecoder
52  //it will determine the type id and call the right decode function
53  bool PacketDecoder::elaborate( ENetPacket* packet, int clientId )
54  {
55    COUT(5) << "PacketDecoder: clientId: " << clientId << std::endl; //control cout, not important, just debugging info
56    int id = (int)*packet->data; //the first 4 bytes are always the enet packet id
57    COUT(5) << "PacketDecoder: packet id: " << id << std::endl;
58    //COUT(5) << "packet size inside packetdecoder: " << packet->dataLength << std::endl;
59
60    if ( packet == NULL ) {
61      COUT(4) << "PacketDecoder: no packets->packetbuffer queue is empty" << std::endl;
62      return false;
63    }
64    switch( id ) {
65    case ACK:
66      acknowledgement( packet, clientId );
67      return true;
68    case COMMAND:
69      return command( packet, clientId );
70    case MOUSE:
71      mousem( packet, clientId );
72      return true;
73    case KEYBOARD:
74      keystrike( packet, clientId );
75      return true;
76    case CHAT:
77      chatMessage( packet, clientId );
78      return true;
79    case GAMESTATE:
80      gstate( packet, clientId );
81      return true;
82    case CLASSID:
83      clid(packet);
84      return true;
85    case WELCOME:
86      return decodeWelcome( packet, clientId );
87    case CONNECT:
88      return decodeConnectRequest( packet, clientId );
89    }
90    return false;
91  }
92
93  //following are the decode functions for the data of the packets
94
95  void PacketDecoder::acknowledgement( ENetPacket* packet, int clientId )
96  {
97    ack* a = new ack;
98    *a = *(ack*)(packet->data); //press pattern of ack on new data
99
100
101    COUT(4) << "PacketDecoder: got ack id: " << a->a << std::endl;
102    processAck( a, clientId ); //debug info
103
104    //clean memory
105    enet_packet_destroy( packet );
106  }
107 
108  bool PacketDecoder::command( ENetPacket* packet, int clientId ){
109    int length = *(int*)((unsigned char *)packet->data+sizeof(int));
110    void *data = (void *)new unsigned char[length];
111    memcpy(data, (void *)(packet->data+2*sizeof(int)), length);
112    return true;
113  }
114
115  void PacketDecoder::mousem( ENetPacket* packet, int clientId )
116  {
117    mouse* mouseMove = new mouse;
118    //copy data of packet->data to new struct
119    *mouseMove = *(mouse*)packet->data;
120
121    //clean memory
122    enet_packet_destroy( packet );
123
124    printMouse( mouseMove ); //debug info
125  }
126
127  void PacketDecoder::keystrike( ENetPacket* packet, int clientId )
128  {
129    keyboard* key = new keyboard;
130    *key = *(keyboard*)packet->data; //see above
131
132    //clean memory
133    enet_packet_destroy( packet );
134
135    printKey( key ); //debug info
136
137  }
138
139  void PacketDecoder::chatMessage( ENetPacket* packet, int clientId )
140  {
141    chat* chatting = new chat;
142    chatting->id = (int)*packet->data; //first copy id into new struct
143    //since the chat message is a char*, allocate the memory needed
144    char* reserve = new char[packet->dataLength-4];
145    //copy the transmitted bytestream into the new generated char*,
146    //note the lenght of the message is represented as "packet->dataLength-sizeof( int )"
147    memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) );
148    //put pointer of chatting struct to the begining of the new generated char*
149    chatting->message = reserve;
150
151    //clean memory
152    enet_packet_destroy( packet );
153
154    processChat( chatting, clientId ); //debug info
155  }
156
157  void PacketDecoder::gstate( ENetPacket* packet, int clientID )
158  {
159    GameStateCompressed* currentState = NULL;
160    currentState = new GameStateCompressed;
161    if(currentState == NULL){
162      COUT(3) << "PacketDecoder: could not generate new GameStateCompressed" << std::endl;
163      return;
164    }
165    //since it's not alowed to use void* for pointer arithmetic
166    //FIXME: variable never used
167    unsigned char* data = (unsigned char *)(packet->data);
168    //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int )
169    memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) );
170    COUT(5) << "PacketDecoder: received gs id: " << currentState->id << std::endl;
171    //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th
172    //position of the data stream, data+2*sizeof( int )
173    memcpy( (void*)&(currentState->compsize), (const void*)(packet->data+2*sizeof( int )), sizeof( int) );
174    //size of uncompressed data
175    memcpy( (void*)&(currentState->normsize), (const void*)(packet->data+3*sizeof( int )), sizeof( int ) );
176    memcpy( (void*)&(currentState->base_id), (const void*)(packet->data+4*sizeof( int )), sizeof( int ) );
177    //since the packetgenerator was changed, due to a new parameter, change this function too
178    memcpy( (void*)&(currentState->diffed), (const void*)(packet->data+5*sizeof(int)), sizeof(bool));
179    memcpy( (void*)&(currentState->complete), (const void*)(packet->data+5*sizeof(int)+sizeof(bool)), sizeof(bool));
180    //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
181    if(currentState->compsize==0)
182      COUT(2) << "PacketDecoder: compsize is 0" << std::endl;
183//     currentState->data = (unsigned char*)(malloc( currentState->compsize ));
184    currentState->data = new unsigned char[currentState->compsize];
185    if(currentState->data==NULL)
186      COUT(2) << "PacketDecoder: Gamestatepacket-decoder: memory leak" << std::endl;
187    //copy the GameStateCompressed data
188    memcpy( (void*)(currentState->data), (const void*)(packet->data+5*sizeof( int ) + 2*sizeof(bool)), currentState->compsize );
189
190    //clean memory
191    enet_packet_destroy( packet );
192    processGamestate(currentState, clientID);
193  }
194
195  void PacketDecoder::clid( ENetPacket *packet)
196  {
197    classid* cid = new classid;
198    cid->length = ((classid*)(packet->data))->length;
199    cid->id = ((classid *)(packet->data))->id;
200    cid->clid = ((classid *)(packet->data))->clid;
201//     cid->message = (const char *)malloc(cid->length);
202    cid->message = new char[cid->length];
203    void *data  = (void *)cid->message;
204    memcpy(data, (const void*)(packet->data+3*sizeof(int)), cid->length);
205    COUT(4) << "PacketDecoder: classid: " << cid->clid << ", name: " << cid->message << std::endl;
206    enet_packet_destroy( packet );
207    processClassid(cid);
208  }
209 
210 
211  bool PacketDecoder::decodeWelcome( ENetPacket* packet, int clientID ){
212    welcome *w = new welcome;
213    w->allowed = ((welcome *)(packet->data))->allowed;
214    w->shipID = ((welcome *)(packet->data))->shipID;
215    w->clientID = ((welcome *)(packet->data))->clientID;
216    w->id = ((welcome *)(packet->data))->id;
217    enet_packet_destroy( packet );
218    return processWelcome(w);
219  }
220 
221  bool PacketDecoder::decodeConnectRequest( ENetPacket *packet, int clientID ){
222    connectRequest *con = new connectRequest;
223    con->id = ((connectRequest *)(packet->data))->id;
224    enet_packet_destroy( packet );
225    return processConnectRequest(con, clientID );
226  }
227
228
229  // now the data processing functions:
230
231  void PacketDecoder::processChat( chat *data, int clientId)
232  {
233    printChat(data, clientId);
234  }
235
236  void PacketDecoder::processGamestate( GameStateCompressed *state, int clientID )
237  {
238    COUT(5) << "PacketDecoder: processing Gamestate" << std::endl;
239    //printGamestate( state );
240  }
241
242  void PacketDecoder::processClassid( classid *cid)
243  {
244    printClassid(cid);
245    return;
246  }
247
248  void PacketDecoder::processAck( ack *data, int clientID)
249  {
250    printAck(data);
251    return;
252  }
253 
254  bool PacketDecoder::processWelcome( welcome *w ){
255    return true;
256  }
257 
258  bool PacketDecoder::processConnectRequest( connectRequest *con, int clientID ){
259    COUT(3) << "packetdecoder: processing connectRequest" << std::endl;
260    return true;
261  }
262
263  //these are some print functions for test stuff
264
265  void PacketDecoder::printAck( ack* data )
266  {
267    COUT(5) << "data id: " << data->id << std::endl;
268    COUT(5) << "data:    " << data->a << std::endl;
269  }
270
271  void PacketDecoder::printMouse( mouse* data )
272  {
273    COUT(5) << "data id: " << data->id << std::endl;
274    COUT(5) << "data:    " << data->x << " " << data->y << std::endl;
275  }
276
277  void PacketDecoder::printKey( keyboard* data )
278  {
279    COUT(5) << "data id: " << data->id << std::endl;
280    COUT(5) << "data:    " << (char)data->press << std::endl;
281  }
282
283  void PacketDecoder::printChat( chat* data, int clientId )
284  {
285    if(clientId!=CLIENTID_CLIENT)
286      COUT(5) << "client: " << clientId << std::endl;
287    COUT(5) << "data id: " << data->id << std::endl;
288    COUT(5) << "data:    " << data->message << std::endl;
289  }
290
291  void PacketDecoder::printGamestate( GameStateCompressed* data )
292  {
293    COUT(5) << "id of GameStateCompressed:   " << data->id << std::endl;
294    COUT(5) << "size of GameStateCompressed: " << data->compsize << std::endl;
295  }
296
297  void PacketDecoder::printClassid( classid *cid)
298  {
299    COUT(5) << "id of classid:    " << cid->id << std::endl;
300    COUT(5) << "size of classid:  " << cid->length << std::endl;
301    COUT(5) << "ID of classid:    " << cid->clid << std::endl;
302    COUT(5) << "data of classid:  " << cid->message << std::endl;
303  }
304
305}
Note: See TracBrowser for help on using the repository browser.