Changeset 7163 for code/trunk/src/libraries/network/packet
- Timestamp:
- Aug 11, 2010, 8:55:13 AM (14 years ago)
- Location:
- code/trunk
- Files:
-
- 8 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/network/packet/CMakeLists.txt
r5929 r7163 11 11 Welcome.cc 12 12 COMPILATION_END 13 ServerInformation.cc 13 14 ) 14 15 … … 23 24 Gamestate.h 24 25 Packet.h 26 ServerInformation.h 25 27 Welcome.h 26 28 ) -
code/trunk/src/libraries/network/packet/Chat.cc
r6417 r7163 37 37 38 38 #define PACKET_FLAGS_CHAT PacketFlag::Reliable 39 40 /* Some lengths */ 39 41 #define _PACKETID 0 40 42 const int _PLAYERID = _PACKETID + sizeof(Type::Value); … … 45 47 : Packet() 46 48 { 49 /* Add chat flag to packet flags */ 47 50 flags_ = flags_ | PACKET_FLAGS_CHAT; 51 52 /* set message length to length of input string + 1 */ 48 53 messageLength_ = message.length()+1; 54 55 /* allocate memory for the data */ 49 56 data_=new unsigned char[ getSize() ]; 57 50 58 *(Type::Value *)(data_ + _PACKETID ) = Type::Chat; 51 59 *(unsigned int *)(data_ + _PLAYERID ) = playerID; 52 60 *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_; 61 62 /* cast the hell out of the message string, and copy it into the 63 * data buffer. 64 */ 53 65 memcpy( data_+_MESSAGE, static_cast<void*>(const_cast<char*>(message.c_str())), messageLength_ ); 54 66 } -
code/trunk/src/libraries/network/packet/Chat.h
r6073 r7163 41 41 { 42 42 public: 43 /* constructors */ 43 44 Chat( const std::string& message, unsigned int playerID ); 44 45 Chat( uint8_t* data, unsigned int clientID ); 46 47 /* destructor */ 45 48 ~Chat(); 46 49 50 /* get size of packet */ 47 51 inline unsigned int getSize() const; 52 53 /* process chat message packet and remove it afterwards */ 48 54 bool process(); 49 55 56 /* Get the length of the message (not the full size of the packet) */ 50 57 unsigned int getMessageLength(){ return messageLength_; }; 58 59 /* return message content */ 51 60 unsigned char *getMessage(); 61 52 62 private: 63 64 /* Message length */ 53 65 uint32_t messageLength_; 66 67 /* Client ID (an integral value for identification) */ 54 68 unsigned int clientID_; 55 69 }; -
code/trunk/src/libraries/network/packet/ClassID.cc
r6417 r7163 65 65 tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) ); 66 66 ++nrOfClasses; 67 packetSize += (classname.size()+1)+sizeof( uint32_t)+sizeof(uint32_t);67 packetSize += (classname.size()+1)+sizeof(network_id)+sizeof(uint32_t); 68 68 } 69 69 … … 80 80 // now save all classids and classnames 81 81 std::pair<uint32_t, std::string> tempPair; 82 uint32_t tempsize = 2*sizeof(uint32_t); // packetid and nrOfClasses 82 83 while( !tempQueue.empty() ){ 83 84 tempPair = tempQueue.front(); … … 87 88 memcpy(temp+2*sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size()+1); 88 89 temp+=2*sizeof(uint32_t)+tempPair.second.size()+1; 90 tempsize+=2*sizeof(uint32_t)+tempPair.second.size()+1; 89 91 } 92 assert(tempsize==packetSize); 90 93 91 94 COUT(5) << "classid packetSize is " << packetSize << endl; … … 111 114 for(unsigned int i=0; i<nrOfClasses; i++){ 112 115 totalsize += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); 116 temp += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); 113 117 } 114 118 return totalsize; … … 141 145 COUT(3) << "processing classid: " << networkID << " name: " << classname << " id: " << id << std::endl; 142 146 if(id==NULL){ 143 COUT(0) << "Rec ieved a bad classname" << endl;147 COUT(0) << "Received a bad classname" << endl; 144 148 abort(); 145 149 } -
code/trunk/src/libraries/network/packet/Gamestate.cc
r6417 r7163 45 45 #define PACKET_FLAG_GAMESTATE PacketFlag::Reliable 46 46 47 48 Gamestate::Gamestate() 47 inline bool memzero( uint8_t* data, uint32_t datalength) 48 { 49 uint64_t* d = (uint64_t*)data; 50 51 for( unsigned int i=0; i<datalength/8; i++ ) 52 { 53 if( *(d+i) != 0 ) 54 return false; 55 } 56 // now process the rest (when datalength isn't a multiple of 4) 57 for( unsigned int j = 8*(datalength/8); j<datalength; j++ ) 58 { 59 if( *(data+j) != 0 ) 60 return false; 61 } 62 return true; 63 } 64 65 66 Gamestate::Gamestate(): 67 header_(0) 49 68 { 50 69 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 51 header_ = 0; 52 } 70 } 71 53 72 54 73 Gamestate::Gamestate(uint8_t *data, unsigned int clientID): 55 74 Packet(data, clientID) 56 75 { 57 76 flags_ = flags_ | PACKET_FLAG_GAMESTATE; … … 59 78 } 60 79 80 61 81 Gamestate::Gamestate(uint8_t *data) 62 82 { 63 83 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 64 data_ =data;84 data_ = data; 65 85 header_ = new GamestateHeader(data_); 66 86 } 67 87 88 68 89 Gamestate::Gamestate(const Gamestate& g) : 69 Packet( *(Packet*)&g ) 90 Packet( *(Packet*)&g ), nrOfVariables_(0) 70 91 { 71 92 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 72 93 header_ = new GamestateHeader(data_); 94 sizes_ = g.sizes_; 73 95 } 74 96 … … 79 101 delete header_; 80 102 } 103 81 104 82 105 bool Gamestate::collectData(int id, uint8_t mode) … … 91 114 return false; 92 115 data_ = new uint8_t[size + GamestateHeader::getSize()]; 93 if(!data_){ 116 if(!data_) 117 { 94 118 COUT(2) << "GameStateManager: could not allocate memory" << std::endl; 95 119 return false; … … 101 125 102 126 //start collect data synchronisable by synchronisable 103 uint8_t *mem =data_;127 uint8_t *mem = data_; // in this stream store all data of the variables and the headers of the synchronisable 104 128 mem += GamestateHeader::getSize(); 105 129 ObjectList<Synchronisable>::iterator it; 106 for(it = ObjectList<Synchronisable>::begin(); it; ++it){ 130 for(it = ObjectList<Synchronisable>::begin(); it; ++it) 131 { 107 132 108 133 // tempsize=it->getSize(id, mode); 109 134 110 tempsize = it->getData(mem, id, mode);135 tempsize = it->getData(mem, this->sizes_, id, mode); 111 136 if ( tempsize != 0 ) 112 137 dataVector_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) ); 113 138 114 139 #ifndef NDEBUG 115 if(currentsize+tempsize > size){ 140 if(currentsize+tempsize > size) 141 { 116 142 assert(0); // if we don't use multithreading this part shouldn't be neccessary 117 143 // start allocate additional memory … … 148 174 } 149 175 176 150 177 bool Gamestate::spreadData(uint8_t mode) 151 178 { … … 153 180 assert(data_); 154 181 assert(!header_->isCompressed()); 155 assert(!header_->isDiffed());156 182 uint8_t *mem=data_+GamestateHeader::getSize(); 157 183 Synchronisable *s; 158 184 159 185 // update the data of the objects we received 160 while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()){ 186 while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()) 187 { 161 188 SynchronisableHeader objectheader(mem); 162 189 … … 170 197 else 171 198 { 172 mem += objectheader.getDataSize() ;199 mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() ); 173 200 } 174 201 } … … 181 208 // In debug mode, check first, whether there are no duplicate objectIDs 182 209 #ifndef NDEBUG 183 if(this->getID()%1000==0){ 210 if(this->getID()%1000==1) 211 { 184 212 std::list<uint32_t> v1; 185 213 ObjectList<Synchronisable>::iterator it; 186 for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) { 187 if (it->getObjectID() == OBJECTID_UNKNOWN) { 188 if (it->objectMode_ != 0x0) { 214 for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) 215 { 216 if (it->getObjectID() == OBJECTID_UNKNOWN) 217 { 218 if (it->objectMode_ != 0x0) 219 { 189 220 COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl; 190 221 COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl; … … 193 224 } 194 225 } 195 else { 226 else 227 { 196 228 std::list<uint32_t>::iterator it2; 197 for (it2 = v1.begin(); it2 != v1.end(); ++it2) { 198 if (it->getObjectID() == *it2) { 229 for (it2 = v1.begin(); it2 != v1.end(); ++it2) 230 { 231 if (it->getObjectID() == *it2) 232 { 199 233 COUT(0) << "Found duplicate objectIDs on the client!" << std::endl 200 234 << "Are you sure you don't create a Sychnronisable objcect with 'new' \ … … 211 245 } 212 246 247 213 248 uint32_t Gamestate::getSize() const 214 249 { … … 222 257 } 223 258 224 bool Gamestate::operator==(packet::Gamestate gs){ 259 260 bool Gamestate::operator==(packet::Gamestate gs) 261 { 225 262 uint8_t *d1 = data_+GamestateHeader::getSize(); 226 263 uint8_t *d2 = gs.data_+GamestateHeader::getSize(); … … 233 270 } 234 271 272 235 273 bool Gamestate::process() 236 274 { 237 275 return GamestateHandler::addGamestate(this, getClientID()); 238 276 } 239 240 277 241 278 … … 253 290 int retval; 254 291 retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) ); 255 switch ( retval ) { 292 switch ( retval ) 293 { 256 294 case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; 257 295 case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false; … … 270 308 header_->setCompSize( buffer ); 271 309 header_->setCompressed( true ); 272 COUT( 5) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;310 COUT(0) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl; 273 311 return true; 274 312 } 313 314 275 315 bool Gamestate::decompressData() 276 316 { … … 289 329 uLongf length=bufsize; 290 330 retval = uncompress( dest, &length, source, (uLong)compsize ); 291 switch ( retval ) { 331 switch ( retval ) 332 { 292 333 case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; 293 334 case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false; … … 301 342 delete temp; 302 343 303 if (this->bDataENetAllocated_){ 344 if (this->bDataENetAllocated_) 345 { 304 346 // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will 305 347 // deallocated it anyway. So data and packet stay together. 306 348 this->bDataENetAllocated_ = false; 307 349 } 308 else{ 350 else 351 { 309 352 // We allocated the memory in the first place (unlikely). So we destroy the old data 310 353 // and overwrite it with the new decompressed data. … … 320 363 } 321 364 322 /*Gamestate *Gamestate::diff(Gamestate *base) 323 { 324 assert(data_); 325 assert(!header_->isCompressed()); 365 366 Gamestate* Gamestate::diffVariables(Gamestate *base) 367 { 368 assert(this && base); assert(data_ && base->data_); 369 assert(!header_->isCompressed() && !base->header_->isCompressed()); 326 370 assert(!header_->isDiffed()); 327 GamestateHeader diffHeader(base->data_); 328 uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); 329 uint32_t of=0; // pointers offset 330 uint32_t dest_length=0; 331 dest_length=header_->getDataSize(); 332 if(dest_length==0) 333 return NULL; 334 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; 335 uint8_t *dest = ndata + GamestateHeader::getSize(); 336 while(of < diffHeader.getDataSize() && of < header_->getDataSize()){ 337 *(dest+of)=*(basep+of)^*(gs+of); // do the xor 338 ++of; 339 } 340 if(diffHeader.getDataSize()!=header_->getDataSize()){ 341 uint8_t n=0; 342 if(diffHeader.getDataSize() < header_->getDataSize()){ 343 while(of<dest_length){ 344 *(dest+of)=n^*(gs+of); 345 of++; 346 } 347 } 348 } 349 350 Gamestate *g = new Gamestate(ndata, getClientID()); 371 372 373 // *** first do a raw diff of the two gamestates 374 375 uint8_t *baseData = GAMESTATE_START(base->data_); 376 uint8_t *origData = GAMESTATE_START(this->data_); 377 uint32_t origLength = header_->getDataSize(); 378 uint32_t baseLength = base->header_->getDataSize(); 379 380 assert( origLength && baseLength ); 381 382 uint8_t *nData = new uint8_t[origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_]; // this is the maximum size needed in the worst case 383 uint8_t *dest = GAMESTATE_START(nData); 384 385 uint32_t baseOffset = 0; //offset in the diffed stream 386 uint32_t origOffset = 0; //offset in the new stream with removed 0's 387 std::vector<uint32_t>::iterator sizes = this->sizes_.begin(); 388 389 while( origOffset < origLength ) 390 { 391 //iterate through all objects 392 393 SynchronisableHeader h(origData+origOffset); 394 395 // Find (if possible) the current object in the datastream of the old gamestate 396 // Start at the current offset position 397 if(baseOffset >= baseLength) 398 baseOffset = 0; 399 uint8_t* temp = baseData + baseOffset; 400 uint32_t objectID = h.getObjectID(); 401 assert(temp < baseData+baseLength); 402 assert(dest < nData + origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_); 403 assert(sizes != this->sizes_.end()); 404 while ( temp < baseData+baseLength ) 405 { 406 SynchronisableHeader htemp(temp); 407 assert( htemp.getDataSize()!=0 ); 408 if ( htemp.getObjectID() == objectID ) 409 { 410 assert( h.getClassID() == htemp.getClassID() ); 411 goto DODIFF; 412 } 413 // { 414 // SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize()); 415 // if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength ) 416 // { 417 // assert(htemp2.getClassID()<500); 418 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 419 // assert(htemp2.isDiffed()==false); 420 // } 421 // } 422 temp += htemp.getDataSize()+SynchronisableHeader::getSize(); 423 424 } 425 // If not found start looking at the beginning 426 assert( temp==baseData+baseLength ); 427 temp = baseData; 428 // { 429 // SynchronisableHeader htemp2(temp); 430 // if( temp < baseData+baseLength ) 431 // { 432 // assert(htemp2.getClassID()<500); 433 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 434 // assert(htemp2.isDiffed()==false); 435 // } 436 // } 437 while ( temp < baseData+baseOffset ) 438 { 439 SynchronisableHeader htemp(temp); 440 if ( htemp.getObjectID() == objectID ) 441 { 442 assert( h.getClassID() == htemp.getClassID() ); 443 goto DODIFF; 444 } 445 // { 446 // SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize()); 447 // if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength ) 448 // { 449 // assert(htemp2.getClassID()<500); 450 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 451 // assert(htemp2.isDiffed()==false); 452 // } 453 // } 454 temp += htemp.getDataSize()+SynchronisableHeader::getSize(); 455 } 456 // Object is new, thus never transmitted -> just copy over 457 goto DOCOPY; 458 459 460 DODIFF: 461 { 462 // COUT(4) << "dodiff" << endl; 463 // if(baseOffset==0) 464 // { 465 // assert(origOffset==0); 466 // } 467 uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData 468 // Check whether the whole object stayed the same 469 if( memcmp( origData+origOffset+objectOffset, temp+objectOffset, h.getDataSize()) == 0 ) 470 { 471 // COUT(4) << "skip object" << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl; 472 origOffset += objectOffset+ h.getDataSize(); // skip the whole object 473 baseOffset = temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData; 474 sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); 475 } 476 else 477 { 478 // if( Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() == "Bot" ) 479 // COUT(0) << "blub" << endl; 480 // COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl; 481 // COUT(4) << "diff " << h.getObjectID() << ":"; 482 // Now start to diff the Object 483 SynchronisableHeaderLight h2(dest); 484 h2 = h; // copy over the objectheader 485 VariableID variableID = 0; 486 uint32_t newObjectOffset = SynchronisableHeaderLight::getSize(); 487 // iterate through all variables 488 while( objectOffset < h.getDataSize()+SynchronisableHeader::getSize() ) 489 { 490 // check whether variable changed and write id and copy over variable to the new stream 491 // otherwise skip variable 492 assert(sizes != this->sizes_.end()); 493 uint32_t varSize = *sizes; 494 assert( varSize == Synchronisable::getSynchronisable(h.getObjectID())->getVarSize(variableID) ); 495 if ( varSize != 0 ) 496 { 497 if ( memcmp(origData+origOffset+objectOffset, temp+objectOffset, varSize) != 0 ) 498 { 499 // COUT(4) << "copy variable" << endl; 500 *(VariableID*)(dest+newObjectOffset) = variableID; // copy over the variableID 501 newObjectOffset += sizeof(VariableID); 502 memcpy( dest+newObjectOffset, origData+origOffset+objectOffset, varSize ); 503 newObjectOffset += varSize; 504 objectOffset += varSize; 505 } 506 else 507 { 508 // COUT(4) << "skip variable" << endl; 509 objectOffset += varSize; 510 } 511 } 512 // else 513 // COUT(4) << "varsize 0" << endl; 514 515 ++variableID; 516 ++sizes; 517 } 518 519 if( Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() != variableID ) 520 sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() - variableID; 521 // COUT(4) << endl; 522 h2.setDiffed(true); 523 h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize()); 524 assert(objectOffset == h.getDataSize()+SynchronisableHeader::getSize()); 525 origOffset += objectOffset; 526 // baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData; 527 //baseOffset += objectOffset; 528 // SynchronisableHeader htemp(temp); 529 // baseOffset += SynchronisableHeader::getSize() + htemp.getDataSize(); 530 // { 531 // SynchronisableHeader htemp2( baseData+(temp-baseData+objectOffset) ); 532 // if( baseData+(temp-baseData+objectOffset) < baseData+baseLength ) 533 // { 534 // assert(htemp2.getClassID()<500); 535 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 536 // assert(htemp2.isDiffed()==false); 537 // } 538 // } 539 baseOffset = temp-baseData + objectOffset; 540 dest += newObjectOffset; 541 } 542 543 continue; 544 } 545 546 DOCOPY: 547 { 548 // COUT(4) << "docopy" << endl; 549 // Just copy over the whole Object 550 memcpy( dest, origData+origOffset, h.getDataSize()+SynchronisableHeader::getSize() ); 551 dest += h.getDataSize()+SynchronisableHeader::getSize(); 552 origOffset += h.getDataSize()+SynchronisableHeader::getSize(); 553 assert( Synchronisable::getSynchronisable(h.getObjectID()) ); 554 // COUT(4) << "copy " << h.getObjectID() << endl; 555 // COUT(4) << "copy " << h.getObjectID() << ":"; 556 //sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); 557 for( unsigned int i = 0; i < Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); ++i ) 558 { 559 // COUT(4) << " " << *sizes; 560 ++sizes; 561 } 562 // COUT(4) << endl; 563 assert(sizes != this->sizes_.end() || origOffset>=origLength); 564 continue; 565 } 566 } 567 568 569 Gamestate *g = new Gamestate(nData, getClientID()); 570 assert(g->header_); 351 571 *(g->header_) = *header_; 352 g->header_->setDiffed( true );353 572 g->header_->setBaseID( base->getID() ); 573 g->header_->setDataSize(dest - nData - GamestateHeader::getSize()); 354 574 g->flags_=flags_; 355 575 g->packetDirection_ = packetDirection_; 576 assert(!g->isCompressed()); 356 577 return g; 357 }*/ 358 359 Gamestate *Gamestate::diff(Gamestate *base) 578 } 579 580 581 Gamestate* Gamestate::diffData(Gamestate *base) 360 582 { 361 583 assert(this && base); assert(data_ && base->data_); … … 393 615 } 394 616 395 Gamestate *Gamestate::undiff(Gamestate *base) 617 618 Gamestate* Gamestate::undiff(Gamestate *base) 396 619 { 397 620 assert(this && base); assert(data_ && base->data_); … … 423 646 424 647 425 // Gamestate *Gamestate::diff(Gamestate *base)426 // {427 // assert(data_);428 // assert(!header_->isCompressed());429 // assert(!header_->isDiffed());430 // GamestateHeader diffHeader(base->data_);431 // uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);432 // uint32_t of=0; // pointers offset433 // uint32_t dest_length=0;434 // dest_length=header_->getDataSize();435 // if(dest_length==0)436 // return NULL;437 // uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];438 // uint8_t *dest = ndata + GamestateHeader::getSize();439 //440 //441 // // LOOP-UNROLLED DIFFING442 // uint32_t *dest32 = (uint32_t*)dest, *base32 = (uint32_t*)basep, *gs32 = (uint32_t*)gs;443 // // diff in 4-byte steps444 // while( of < (uint32_t)(header_->getDataSize())/4 ){445 // if( of < (uint32_t)(diffHeader.getDataSize())/4 )446 // {447 // *(dest32+of)=*(base32+of) ^ *(gs32+of); // do the xor448 // ++of;449 // }450 // else451 // {452 // *(dest32+of)=*(gs32+of); // same as 0 ^ *(gs32+of)453 // ++of;454 // }455 // }456 // for( unsigned int of2 = 0; of2 < header_->getDataSize()%4; ++of2 )457 // {458 // if( of*4+of2 < diffHeader.getDataSize() )459 // {460 // *(dest+4*of+of2)=*(basep+4*of+of2) ^ *(gs+4*of+of2); // do the xor461 // }462 // else463 // {464 // *(dest+4*of+of2)=*(gs+4*of+of2); // same as 0 ^ *(gs32+of)465 // }466 // }467 //468 // Gamestate *g = new Gamestate(ndata, getClientID());469 // *(g->header_) = *header_;470 // g->header_->setDiffed( true );471 // g->header_->setBaseID( base->getID() );472 // g->flags_=flags_;473 // g->packetDirection_ = packetDirection_;474 // return g;475 // }476 477 478 648 void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength) 479 649 { … … 501 671 assert(j==datalength); 502 672 } 673 503 674 504 675 Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ … … 529 700 // COUT(0) << "myvector contains:"; 530 701 // for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ ) 531 // COUT(0) << ' '<< (*itt).objID;702 // COUT(0) << " " << (*itt).objID; 532 703 // COUT(0) << endl; 533 704 for(it=dataVector_.begin(); it!=dataVector_.end();){ 534 705 SynchronisableHeader oldobjectheader(origdata); 535 706 SynchronisableHeader newobjectheader(newdata); 536 if ( it->objSize == 0 )707 if ( (*it).objSize == 0 ) 537 708 { 538 709 ++it; 539 710 continue; 540 711 } 541 objectsize = oldobjectheader.getDataSize() ;712 objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize(); 542 713 objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader 543 if ( it->objID == oldobjectheader.getObjectID() ){714 if ( (*it).objID == oldobjectheader.getObjectID() ){ 544 715 memcpy(newdata, origdata, objectsize); 545 assert(newobjectheader.isDataAvailable()==true);546 716 ++it; 547 717 }else{ 548 718 newobjectheader = oldobjectheader; 549 newobjectheader.setDataAvailable(false);550 719 memset(newdata+objectOffset, 0, objectsize-objectOffset); 551 720 } … … 559 728 { 560 729 SynchronisableHeader oldobjectheader(origdata); 561 objectsize = oldobjectheader.getDataSize() ;730 objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize(); 562 731 origdata += objectsize; 563 732 origsize += objectsize; … … 571 740 572 741 573 /*Gamestate *Gamestate::undiff(Gamestate *base)574 {575 assert(this && base);assert(data_);576 assert(header_->isDiffed());577 assert(!header_->isCompressed() && !base->header_->isCompressed());578 uint8_t *basep = GAMESTATE_START(base->data_);579 uint8_t *gs = GAMESTATE_START(this->data_);580 uint32_t of=0; // pointers offset581 uint32_t dest_length=0;582 dest_length=header_->getDataSize();583 if(dest_length==0)584 return NULL;585 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];586 uint8_t *dest = ndata + GamestateHeader::getSize();587 while(of < base->header_->getDataSize() && of < header_->getDataSize()){588 *(dest+of)=*(basep+of)^*(gs+of); // do the xor589 ++of;590 }591 if(base->header_->getDataSize()!=header_->getDataSize()){592 uint8_t n=0;593 if(base->header_->getDataSize() < header_->getDataSize()){594 while(of < dest_length){595 *(dest+of)=n^*(gs+of);596 of++;597 }598 }599 }600 Gamestate *g = new Gamestate(ndata, getClientID());601 assert(g->header_);602 *(g->header_) = *header_;603 g->header_->setDiffed( false );604 g->flags_=flags_;605 g->packetDirection_ = packetDirection_;606 assert(!g->isDiffed());607 assert(!g->isCompressed());608 return g;609 }*/610 611 742 uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode) 612 743 { 613 uint32_t size=0; 744 uint32_t size = 0; 745 uint32_t nrOfVariables = 0; 614 746 // get the start of the Synchronisable list 615 747 ObjectList<Synchronisable>::iterator it; 616 748 // get total size of gamestate 617 for(it = ObjectList<Synchronisable>::begin(); it; ++it) 749 for(it = ObjectList<Synchronisable>::begin(); it; ++it){ 618 750 size+=it->getSize(id, mode); // size of the actual data of the synchronisable 751 nrOfVariables += it->getNrOfVariables(); 752 } 753 // COUT(0) << "allocating " << nrOfVariables << " ints" << endl; 754 this->sizes_.reserve(nrOfVariables); 619 755 return size; 620 756 } 757 621 758 622 759 } //namespace packet -
code/trunk/src/libraries/network/packet/Gamestate.h
r6073 r7163 36 36 #include <cstring> 37 37 #include <list> 38 #include <vector> 38 39 39 40 #include "util/CRC32.h" … … 113 114 inline bool isCompressed() const { return header_->isCompressed(); } 114 115 inline int32_t getBaseID() const { return header_->getBaseID(); } 115 Gamestate *diff(Gamestate *base); 116 inline uint32_t getDataSize() const { return header_->getDataSize(); } 117 Gamestate* diffVariables(Gamestate *base); 118 Gamestate* diffData(Gamestate *base); 116 119 Gamestate *undiff(Gamestate *base); 117 120 Gamestate* doSelection(unsigned int clientID, unsigned int targetSize); … … 123 126 private: 124 127 void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength); 128 inline uint32_t findObject( const SynchronisableHeader& header, uint8_t* mem, uint32_t dataLength, uint32_t startPosition = 0 ); 125 129 virtual uint32_t getSize() const; 126 130 virtual inline bool process(); 127 128 private:129 131 uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0); 130 std::list<obj> dataVector_; 131 GamestateHeader* header_; 132 133 std::list<obj> dataVector_; 134 GamestateHeader* header_; 135 std::vector<uint32_t> sizes_; 136 uint32_t nrOfVariables_; 132 137 }; 133 138 -
code/trunk/src/libraries/network/packet/Packet.cc
r6417 r7163 62 62 std::map<size_t, Packet *> Packet::packetMap_; 63 63 64 Packet::Packet(){ 64 Packet::Packet() 65 { 65 66 flags_ = PACKET_FLAG_DEFAULT; 66 67 packetDirection_ = Direction::Outgoing; … … 71 72 } 72 73 73 void blub(ENetPacket *packet){ 74 COUT(4) << "blubb" << std::endl; 75 } 76 77 Packet::Packet(uint8_t *data, unsigned int clientID){ 74 Packet::Packet(uint8_t *data, unsigned int clientID) 75 { 78 76 flags_ = PACKET_FLAG_DEFAULT; 79 77 packetDirection_ = Direction::Incoming;
Note: See TracChangeset
for help on using the changeset viewer.