Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged network branch into new network2 branch (from trunk)

File size: 9.4 KB
RevLine 
[1056]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 */
[514]28
29/*
[777]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*
[223]35*/
36
[1062]37#include "PacketTypes.h"
38#include "PacketManager.h"
39
[199]40#include <iostream>
41
[1055]42#include "core/Debug.h"
[199]43
[777]44namespace network
45{
[199]46
[777]47  PacketDecoder::PacketDecoder(){}
[531]48
[777]49  PacketDecoder::~PacketDecoder(){}
[199]50
[777]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;
[1098]56    COUT(5) << "PacketDecoder: clientId: " << client << std::endl; //control cout, not important, just debugging info
[777]57    int id = (int)*packet->data; //the first 4 bytes are always the enet packet id
[1098]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    }
[777]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  }
[223]93
[777]94  //following are the decode functions for the data of the packets
[514]95
[777]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
[514]100
[199]101
[1098]102    COUT(5) << "PacketDecoder: got ack id: " << a->id << std::endl;
[777]103    processAck( a, clientId ); //debug info
[514]104
[777]105    //clean memory
106    enet_packet_destroy( packet );
107  }
[514]108
[777]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;
[199]114
[777]115    //clean memory
116    enet_packet_destroy( packet );
[514]117
[777]118    printMouse( mouseMove ); //debug info
119  }
[514]120
[777]121  void PacketDecoder::keystrike( ENetPacket* packet, int clientId )
122  {
123    keyboard* key = new keyboard;
124    *key = *(keyboard*)packet->data; //see above
[332]125
[777]126    //clean memory
127    enet_packet_destroy( packet );
[199]128
[777]129    printKey( key ); //debug info
[514]130
[777]131  }
[514]132
[777]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;
[514]144
[777]145    //clean memory
146    enet_packet_destroy( packet );
[223]147
[777]148    processChat( chatting, clientId ); //debug info
149  }
[514]150
[777]151  void PacketDecoder::gstate( ENetPacket* packet )
152  {
[1021]153    GameStateCompressed* currentState = NULL;
154    currentState = new GameStateCompressed;
155    if(currentState == NULL){
[1098]156      COUT(3) << "PacketDecoder: could not generate new GameStateCompressed" << std::endl;
[1021]157      return;
158    }
[777]159    //since it's not alowed to use void* for pointer arithmetic
[1064]160    //FIXME: variable never used
[1021]161    unsigned char* data = (unsigned char *)(packet->data);
[777]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 ) );
[1021]164    //currentState->id = *((int *)packet->data+sizeof(int));
165    memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) );
[1098]166    COUT(5) << "PacketDecoder: received gs id: " << currentState->id << std::endl;
[1021]167//     std::cout << "id: " << currentState->id << std::endl;
[777]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 )
[1021]170    memcpy( (void*)&(currentState->compsize), (const void*)(packet->data+2*sizeof( int )), sizeof( int) );
[777]171    //currentState->compsize = (int)*(data+2*sizeof(int));
[1021]172//     std::cout << "compsize: " << currentState->compsize << std::endl;
[777]173    //size of uncompressed data
[1021]174    memcpy( (void*)&(currentState->normsize), (const void*)(packet->data+3*sizeof( int )), sizeof( int ) );
[1098]175    memcpy( (void*)&(currentState->base_id), (const void*)(packet->data+4*sizeof( int )), sizeof( int ) );
[777]176    //currentState->normsize = (int)*(data+3*sizeof(int));
[1021]177//     std::cout << "normsize. " << currentState->normsize << std::endl;
[777]178    //since the packetgenerator was changed, due to a new parameter, change this function too
[1098]179    memcpy( (void*)&(currentState->diffed), (const void*)(packet->data+5*sizeof(int)), sizeof(bool));
[777]180    //currentState->diffed = (bool)*(data+4*sizeof(int));
[1021]181//     std::cout << "diffed: " << currentState->diffed << std::endl;
[777]182    //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
[1021]183    if(currentState->compsize==0)
[1098]184      COUT(2) << "PacketDecoder: compsize is 0" << std::endl;
[777]185    currentState->data = (unsigned char*)(malloc( currentState->compsize ));
186    if(currentState->data==NULL)
[1098]187      COUT(2) << "PacketDecoder: Gamestatepacket-decoder: memory leak" << std::endl;
[777]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;
[1021]191    memcpy( (void*)(currentState->data), (const void*)(packet->data+4*sizeof( int ) + sizeof(bool)), currentState->compsize );
[332]192
[777]193    //clean memory
194    enet_packet_destroy( packet );
195    processGamestate(currentState);
196  }
[400]197
[777]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);
[1098]207    COUT(4) << "PacketDecoder: classid: " << cid->clid << ", name: " << cid->message << std::endl;
[777]208    enet_packet_destroy( packet );
209    processClassid(cid);
210  }
[400]211
[369]212
[777]213  // now the data processing functions:
[369]214
[777]215  void PacketDecoder::processChat( chat *data, int clientId)
216  {
217    printChat(data, clientId);
218  }
[620]219
[777]220  void PacketDecoder::processGamestate( GameStateCompressed *state )
221  {
[1098]222    COUT(5) << "PacketDecoder: processing Gamestate" << std::endl;
223    //printGamestate( state );
[777]224  }
[400]225
[777]226  void PacketDecoder::processClassid( classid *cid)
227  {
228    printClassid(cid);
229    return;
230  }
[400]231
[777]232  void PacketDecoder::processAck( ack *data, int clientID)
233  {
234    printAck(data);
235    return;
236  }
[400]237
[223]238
[777]239  //these are some print functions for test stuff
[199]240
[777]241  void PacketDecoder::printAck( ack* data )
242  {
[1021]243    COUT(5) << "data id: " << data->id << std::endl;
244    COUT(5) << "data:    " << data->a << std::endl;
[777]245  }
[199]246
[777]247  void PacketDecoder::printMouse( mouse* data )
248  {
[1021]249    COUT(5) << "data id: " << data->id << std::endl;
250    COUT(5) << "data:    " << data->x << " " << data->y << std::endl;
[777]251  }
[199]252
[777]253  void PacketDecoder::printKey( keyboard* data )
254  {
[1021]255    COUT(5) << "data id: " << data->id << std::endl;
256    COUT(5) << "data:    " << (char)data->press << std::endl;
[777]257  }
[332]258
[777]259  void PacketDecoder::printChat( chat* data, int clientId )
260  {
261    if(clientId!=CLIENTID_CLIENT)
[1021]262      COUT(5) << "client: " << clientId << std::endl;
263    COUT(5) << "data id: " << data->id << std::endl;
264    COUT(5) << "data:    " << data->message << std::endl;
[777]265  }
[400]266
[777]267  void PacketDecoder::printGamestate( GameStateCompressed* data )
268  {
[1021]269    COUT(5) << "id of GameStateCompressed:   " << data->id << std::endl;
270    COUT(5) << "size of GameStateCompressed: " << data->compsize << std::endl;
[777]271  }
272
273  void PacketDecoder::printClassid( classid *cid)
274  {
[1021]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;
[777]279  }
280
[400]281}
Note: See TracBrowser for help on using the repository browser.