Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changed some comments and catched some return values and maybe some stuff we have to unchange

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