Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 28, 2007, 5:20:23 PM (16 years ago)
Author:
nicolasc
Message:

merge network

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/merger/src/network/PacketDecoder.cc

    r278 r332  
    11/*
    22 * Class contains functions to determine and decode incomming packages
     3 * ->don't read this without the class PacketGenerator, since they belong together
    34 *
    45 * Autor: Dumeni Manatschal
    56 *
    67*/
    7 
    88
    99#include "enet/enet.h"
     
    2121{
    2222        int client = clientId;
    23         cout << "clientId: " << client << endl;
    24         int id = (int)*packet->data;
     23        cout << "clientId: " << client << endl; //control cout, not important, just debugging info
     24        int id = (int)*packet->data; //the first 4 bytes are always the enet packet id
    2525        switch( id ) {
    2626        case ACK:
     
    4040                return true;
    4141                break;
     42        case GAMESTATE:
     43                gstate( packet );
     44                return true;
     45                break;
    4246        }
    4347        return false;
     
    4953{
    5054        ack* a = new ack;
    51         *a = *(ack*)packet->data;
    52         printAck( a );
     55        *a = *(ack*)packet->data; //press pattern of ack on new data
     56       
     57        //clean memory
     58        enet_packet_destroy( packet );
     59       
     60        printAck( a ); //debug info
    5361}
    5462
     
    5664{
    5765        mouse* mouseMove = new mouse;
    58         *mouseMove = *(mouse*)packet->data;
    59         printMouse( mouseMove );
     66        //copy data of packet->data to new struct
     67        *mouseMove = *(mouse*)packet->data;
     68       
     69        //clean memory
     70        enet_packet_destroy( packet );
     71       
     72        printMouse( mouseMove ); //debug info
    6073}
    6174
     
    6376{
    6477        keyboard* key = new keyboard;
    65         *key = *(keyboard*)packet->data;
    66         printKey( key );
     78        *key = *(keyboard*)packet->data; //see above
     79       
     80        //clean memory
     81        enet_packet_destroy( packet );
     82       
     83        printKey( key ); //debug info
     84
    6785}
    6886
     
    7088{
    7189        chat* chatting = new chat;
    72         chatting->id = (int)*packet->data;
    73         char* reserve = new char[packet->dataLength-4];
    74         memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-4 );
     90        chatting->id = (int)*packet->data; //first copy id into new struct
     91        //since the chat message is a char*, allocate the memory needed
     92        char* reserve = new char[packet->dataLength-4];
     93        //copy the transmitted bytestream into the new generated char*,
     94        //note the lenght of the message is represented as "packet->dataLength-sizeof( int )"
     95        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) );
     96        //put pointer of chatting struct to the begining of the new generated char*
    7597        chatting->message = reserve;
    76         printChat( chatting );
     98       
     99        //clean memory
     100        enet_packet_destroy( packet );
     101       
     102        printChat( chatting ); //debug info
     103       
     104}
     105
     106void PacketDecoder::gstate( ENetPacket* packet )
     107{
     108        GameState* currentState = new GameState;
     109        //since it's not alowed to use void* for pointer arithmetic
     110        unsigned char* data = (unsigned char*)packet->data;
     111        //copy the gamestate id into the struct, which is located at second place data+sizeof( int )
     112        memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) );
     113        //copy the size of the gamestate data into the new gamestate struct, located at 3th
     114        //position of the data stream, data+2*sizeof( int )
     115        memcpy( (void*)&(currentState->size), (const void*)(data+2*sizeof( int )), sizeof( int) );
     116        //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
     117        currentState->data = (unsigned char*)(malloc( currentState->size ));
     118        //copy the gamestate data
     119        memcpy( (void*)(currentState->data), (const void*)(data+3*sizeof( int )), currentState->size );
     120       
     121        //clean memory
     122        enet_packet_destroy( packet );
    77123}
    78124
     
    100146{
    101147        cout << "data id: " << data->id << endl;
    102         cout << "blablabla" << endl;
    103148        cout << "data:    " << data->message << endl;
    104149}
     150
     151void PacketDecoder::printGamestate( GameState* data )
     152{
     153        cout << "id of gamestate:   " << data->id << endl;
     154        cout << "size of gamestate: " << data->size << endl;
     155}
Note: See TracChangeset for help on using the changeset viewer.