Changeset 1336 for code/branches/merge/src/network/PacketGenerator.cc
- Timestamp:
- May 20, 2008, 5:58:37 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/merge/src/network/PacketGenerator.cc
r1294 r1336 46 46 namespace network 47 47 { 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 48 73 PacketGenerator::PacketGenerator() { } 49 74 … … 135 160 memcpy( (void*)(data+5*sizeof( int ) + sizeof(bool)), (const void*)&(states->complete), sizeof(bool) ); 136 161 memcpy( (void*)(data+5*sizeof( int ) + 2*sizeof(bool)), (const void*)states->data, states->compsize ); 162 137 163 //create an enet packet with the generated bytestream 138 164 COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl; 139 165 ENetPacket *packet = enet_packet_create( data , totalLen, reliable ); 140 166 delete[] data; 167 if(!addCRC(packet)) 168 COUT(3) << "could not add crc to gamestate packet" << std::endl; 141 169 return packet; 142 170 } … … 174 202 return packet; 175 203 } 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 } 176 219 177 220 }
Note: See TracChangeset
for help on using the changeset viewer.