Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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