Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network2/src/network/PacketDecoder.cc @ 1162

Last change on this file since 1162 was 1098, checked in by rgrieder, 17 years ago

merged network branch into new network2 branch (from trunk)

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