Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 313 for code/branches/network


Ignore:
Timestamp:
Nov 28, 2007, 2:39:12 PM (17 years ago)
Author:
dumenim
Message:

correct gamestate, comment

Location:
code/branches/network/src/network
Files:
3 edited

Legend:

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

    r290 r313  
    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:
     
    5353{
    5454        ack* a = new ack;
    55         *a = *(ack*)packet->data;
    56         printAck( a );
     55        *a = *(ack*)packet->data; //press pattern of ack on new data
     56        printAck( a ); //debug info
    5757}
    5858
     
    6060{
    6161        mouse* mouseMove = new mouse;
    62         *mouseMove = *(mouse*)packet->data;
    63         printMouse( mouseMove );
     62        //copy data of packet->data to new struct
     63        *mouseMove = *(mouse*)packet->data;
     64        printMouse( mouseMove ); //debug info
    6465}
    6566
     
    6768{
    6869        keyboard* key = new keyboard;
    69         *key = *(keyboard*)packet->data;
    70         printKey( key );
     70        *key = *(keyboard*)packet->data; //see above
     71        printKey( key ); //debug info
    7172}
    7273
     
    7475{
    7576        chat* chatting = new chat;
    76         chatting->id = (int)*packet->data;
    77         char* reserve = new char[packet->dataLength-4];
    78         memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-4 );
     77        chatting->id = (int)*packet->data; //first copy id into new struct
     78        //since the chat message is a char*, allocate the memory needed
     79        char* reserve = new char[packet->dataLength-4];
     80        //copy the transmitted bytestream into the new generated char*,
     81        //note the lenght of the message is represented as "packet->dataLength-sizeof( int )"
     82        memcpy( &reserve[0], packet->data+sizeof(int), packet->dataLength-sizeof(int) );
     83        //put pointer of chatting struct to the begining of the new generated char*
    7984        chatting->message = reserve;
    80         printChat( chatting );
     85        printChat( chatting ); //debug info
    8186}
    8287
     
    8489{
    8590        GameState* currentState = new GameState;
     91        //since it's not alowed to use void* for pointer arithmetic
    8692        unsigned char* data = (unsigned char*)packet->data;
    87         memcpy( (void*)&(currentState->size), (const void*)(data+sizeof( int )), sizeof(int) );
    88         memcpy( (void*)(currentState->data), (const void*)(data+2*sizeof( int )), currentState->size );
     93        //copy the gamestate id into the struct, which is located at second place data+sizeof( int )
     94        memcpy( (void*)&(currentState->id), (const void*)(data+sizeof( int )), sizeof( int ) );
     95        //copy the size of the gamestate data into the new gamestate struct, located at 3th
     96        //position of the data stream, data+2*sizeof( int )
     97        memcpy( (void*)&(currentState->size), (const void*)(data+2*sizeof( int )), sizeof( int) );
     98        //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
     99        currentState->data = (unsigned char*)(malloc( currentState->size ));
     100        //copy the gamestate data
     101        memcpy( (void*)(currentState->data), (const void*)(data+3*sizeof( int )), currentState->size );
    89102}
    90103
     
    117130void PacketDecoder::printGamestate( GameState* data )
    118131{
     132        cout << "id of gamestate:   " << data->id << endl;
    119133        cout << "size of gamestate: " << data->size << endl;
    120134}
  • code/branches/network/src/network/PacketGenerator.cc

    r290 r313  
    11/*
    22 *Class generates packets that can be send by enet
     3 * ->don't read this without the class PacketDecoder, since they belong together
    34 *
    45 * Autor: Dumeni Manatschal
     
    7172ENetPacket* PacketGenerator::gstate( GameState* states, int reliable )
    7273{
    73         int* gid; *gid = GAMESTATE;
    74         int totalLen = sizeof( int ) + states->size;
    75         unsigned char* data = (unsigned char*)malloc( totalLen );
    76         memcpy( (void*)(data), (const void*)gid, sizeof( int ) );
    77         memcpy( (void*)(data+sizeof( int )), (const void*)states->data, states->size );
     74        int* gid; *gid = GAMESTATE; //first assign the correct enet id
     75        int totalLen = 3*sizeof( int ) + states->size; //calculate the total size of the datastream memory
     76        unsigned char* data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream
     77        memcpy( (void*)(data), (const void*)gid, sizeof( int ) ); //this is the enet id
     78        memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the gamestate id
     79        //this is the size of the gamestate data, place at 3th position of the enet datastream
     80        memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->size), sizeof(int));
     81        //place the gamestate data at the end of the enet datastream
     82        memcpy( (void*)(data+3*sizeof( int )), (const void*)states->data, states->size );
     83        //create an enet packet with the generated bytestream
    7884        ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
    7985       
  • code/branches/network/src/network/PacketManager.h

    r290 r313  
    1919 * class to generate packets
    2020 *
    21  * Autor: Dumeni Manatschal
     21 * @autor: Dumeni Manatschal
    2222 *
    2323*/
     
    5454 * class used to decode incoming packets
    5555 *
    56  * Autor: Dumeni Manatschal
     56 * @autor: Dumeni Manatschal
    5757 *
    5858*/
Note: See TracChangeset for help on using the changeset viewer.