Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 20, 2008, 5:58:37 PM (16 years ago)
Author:
scheusso
Message:

added crc checksum testing for submitted gamestates (can also be used for other packet types)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/merge/src/network/PacketGenerator.cc

    r1294 r1336  
    4646namespace network
    4747{
     48  void calcCRCBit(uint32_t &crc32, int bit){
     49    int hbit;
     50 
     51    hbit=(crc32 & 0x80000000) ? 1 : 0;
     52    if (hbit != bit)
     53      crc32=(crc32<<1) ^ NETWORK_CRC32POLY;
     54    else
     55      crc32=crc32<<1;
     56  }
     57 
     58  uint32_t calcCRC(unsigned char *data, unsigned int dataLength){
     59    uint32_t crc32=0;
     60    for(unsigned int i=0; i<dataLength; i++){
     61      calcCRCBit(crc32, (data[i]&0x1)>>0); // 1st bit
     62      calcCRCBit(crc32, (data[i]&0x2)>>1); // 2nd bit
     63      calcCRCBit(crc32, (data[i]&0x3)>>2); // 3rd bit
     64      calcCRCBit(crc32, (data[i]&0x4)>>3); // 4th bit
     65      calcCRCBit(crc32, (data[i]&0x5)>>4); // 5th bit
     66      calcCRCBit(crc32, (data[i]&0x6)>>5); // 6th bit
     67      calcCRCBit(crc32, (data[i]&0x7)>>6); // 7th bit
     68      calcCRCBit(crc32, (data[i]&0x8)>>7); // 8th bit
     69    }
     70    return crc32;
     71  }
     72 
    4873  PacketGenerator::PacketGenerator() { }
    4974
     
    135160    memcpy( (void*)(data+5*sizeof( int ) + sizeof(bool)), (const void*)&(states->complete), sizeof(bool) );
    136161    memcpy( (void*)(data+5*sizeof( int ) + 2*sizeof(bool)), (const void*)states->data, states->compsize );
     162   
    137163    //create an enet packet with the generated bytestream
    138164    COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl;
    139165    ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
    140166    delete[] data;
     167    if(!addCRC(packet))
     168      COUT(3) << "could not add crc to gamestate packet" << std::endl;
    141169    return packet;
    142170  }
     
    174202    return packet;
    175203  }
     204 
     205 
     206  bool PacketGenerator::addCRC( ENetPacket *packet){
     207    unsigned char *data = packet->data;
     208    uint32_t crc32=calcCRC(data, packet->dataLength);
     209    // now append the crc to the packet data
     210    int oldlength = packet->dataLength;
     211    if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){
     212      memcpy(&packet->data[oldlength], &crc32, sizeof(uint32_t));
     213      return true;
     214    }else{
     215      COUT(3) << "could not add crc to gamestate" << std::endl;
     216      return false;
     217    }
     218  }
    176219
    177220}
Note: See TracChangeset for help on using the changeset viewer.