Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/releases/0.1.0/src/network/PacketDecoder.cc @ 2410

Last change on this file since 2410 was 790, checked in by nicolasc, 17 years ago

merged FICN back into trunk
awaiting release.

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