Changeset 1336 for code/branches/merge/src/network
- Timestamp:
- May 20, 2008, 5:58:37 PM (17 years ago)
- Location:
- code/branches/merge/src/network
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/merge/src/network/PacketDecoder.cc
r1299 r1336 88 88 return decodeConnectRequest( packet, clientId ); 89 89 } 90 return false; 91 } 92 93 bool PacketDecoder::testAndRemoveCRC(ENetPacket *packet){ 94 uint32_t submittetcrc; 95 int dataLength = packet->dataLength; 96 // get the submittet crc code 97 memcpy(&submittetcrc, &packet->data[dataLength-sizeof(uint32_t)], sizeof(uint32_t)); 98 unsigned char *data = packet->data; 99 uint32_t crc32=calcCRC(data, packet->dataLength-sizeof(uint32_t)); 100 // now append the crc to the packet data 101 if(crc32==submittetcrc){ 102 dataLength-=sizeof(uint32_t); 103 enet_packet_resize(packet, dataLength); 104 return true; 105 } 106 COUT(3) << "gamestate crc: " << crc32 << std::endl; 107 COUT(3) << "submitted crc: " << submittetcrc << std::endl; 90 108 return false; 91 109 } … … 157 175 void PacketDecoder::gstate( ENetPacket* packet, int clientID ) 158 176 { 177 if(!testAndRemoveCRC(packet)){ 178 COUT(3) << "crc test of gamestate failed - dropping packet" << std::endl; 179 return; 180 } 159 181 GameStateCompressed* currentState = NULL; 160 182 currentState = new GameStateCompressed; -
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 } -
code/branches/merge/src/network/PacketManager.h
r1264 r1336 33 33 34 34 #include <string> 35 #include <inttypes.h> 35 36 #include <enet/enet.h> 36 37 … … 38 39 39 40 #define CLIENTID_CLIENT -1 41 #define NETWORK_CRC32POLY 0x04C11DB7 /* CRC-32 Polynom */ 40 42 41 43 //enum netowk generally used to set the type ID of a packet … … 48 50 * 49 51 */ 52 //void calcCRC(uint32_t &crc32, int bit); 53 uint32_t calcCRC(unsigned char *data, unsigned int dataLength); 54 50 55 class PacketGenerator 51 56 { … … 63 68 ENetPacket* generateConnectRequest( int reliable = ENET_PACKET_FLAG_RELIABLE ); 64 69 private: 70 bool addCRC( ENetPacket *packet); 65 71 }; 66 72 … … 84 90 85 91 private: 86 92 bool testAndRemoveCRC(ENetPacket *packet); 87 93 88 94
Note: See TracChangeset
for help on using the changeset viewer.