Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7333


Ignore:
Timestamp:
Sep 3, 2010, 10:49:13 AM (14 years ago)
Author:
scheusso
Message:

rewrote network gamestate diff function in order to fix a bug

Location:
code/branches/network3/src/libraries/network/packet
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network3/src/libraries/network/packet/Gamestate.cc

    r7163 r7333  
    364364
    365365
     366inline void /*Gamestate::*/diffObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
     367{
     368  //       COUT(4) << "dodiff" << endl;
     369  //       if(baseOffset==0)
     370  //       {
     371  //         assert(origOffset==0);
     372  //       }
     373  uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData
     374  // Check whether the whole object stayed the same
     375  if( memcmp( origData+objectOffset, newData+objectOffset, objectHeader.getDataSize()) == 0 )
     376  {
     377    //         COUT(4) << "skip object" << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
     378    origData += objectOffset + objectHeader.getDataSize(); // skip the whole object
     379    baseData += objectOffset + objectHeader.getDataSize();
     380    sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables();
     381  }
     382  else
     383  {
     384    //         if( Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() == "Bot" )
     385    //           COUT(0) << "blub" << endl;
     386    //         COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
     387    //         COUT(4) << "diff " << h.getObjectID() << ":";
     388    // Now start to diff the Object
     389    SynchronisableHeaderLight h2(newData);
     390    h2 = objectHeader; // copy over the objectheader
     391    VariableID variableID = 0;
     392    uint32_t newObjectOffset = SynchronisableHeaderLight::getSize();
     393    // iterate through all variables
     394    while( objectOffset < objectHeader.getDataSize()+SynchronisableHeader::getSize() )
     395    {
     396      // check whether variable changed and write id and copy over variable to the new stream
     397      // otherwise skip variable
     398//       assert(sizes != this->sizes_.end());
     399      uint32_t varSize = *sizes;
     400      assert( varSize == Synchronisable::getSynchronisable(objectHeader.getObjectID())->getVarSize(variableID) );
     401      if ( varSize != 0 )
     402      {
     403        if ( memcmp(origData+objectOffset, baseData+objectOffset, varSize) != 0 )
     404        {
     405          //               COUT(4) << "copy variable" << endl;
     406          *(VariableID*)(newData+newObjectOffset) = variableID; // copy over the variableID
     407          newObjectOffset += sizeof(VariableID);
     408          memcpy( newData+newObjectOffset, origData+objectOffset, varSize );
     409          newObjectOffset += varSize;
     410          objectOffset += varSize;
     411        }
     412        else
     413        {
     414          //               COUT(4) << "skip variable" << endl;
     415          objectOffset += varSize;
     416        }
     417      }
     418//           else
     419//             COUT(4) << "varsize 0" << endl;
     420
     421      ++variableID;
     422      ++sizes;
     423    }
     424           
     425    if( Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() != variableID )
     426      sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() - variableID;
     427    //         COUT(4) << endl;
     428   
     429    h2.setDiffed(true);
     430    h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize());
     431    assert(objectOffset == objectHeader.getDataSize()+SynchronisableHeader::getSize());
     432    origData += objectOffset;
     433    //         baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
     434    //baseOffset += objectOffset;
     435    //         SynchronisableHeader htemp(temp);
     436    //         baseOffset += SynchronisableHeader::getSize() + htemp.getDataSize();
     437    //         {
     438      //           SynchronisableHeader htemp2( baseData+(temp-baseData+objectOffset) );
     439    //           if( baseData+(temp-baseData+objectOffset) < baseData+baseLength )
     440    //           {
     441      //             assert(htemp2.getClassID()<500);
     442    //             assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
     443    //             assert(htemp2.isDiffed()==false);
     444    //           }
     445    //         }
     446    baseData += objectOffset;
     447    newData += newObjectOffset;
     448  }
     449}
     450
     451inline void /*Gamestate::*/copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
     452{
     453  //       COUT(4) << "docopy" << endl;
     454  // Just copy over the whole Object
     455  memcpy( newData, origData, objectHeader.getDataSize()+SynchronisableHeader::getSize() );
     456  newData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
     457  origData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
     458  SynchronisableHeader baseHeader( baseData );
     459  baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize();
     460  //       COUT(4) << "copy " << h.getObjectID() << endl;
     461  //       COUT(4) << "copy " << h.getObjectID() << ":";
     462  sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables();
     463//   for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i )
     464//   {
     465//     //         COUT(4) << " " << *sizes;
     466//     ++sizes;
     467//   }
     468    //       COUT(4) << endl;
     469}
     470
    366471Gamestate* Gamestate::diffVariables(Gamestate *base)
    367472{
     
    375480  uint8_t *baseData = GAMESTATE_START(base->data_);
    376481  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
     482  uint8_t *origDataEnd = origData + header_->getDataSize();
     483  uint8_t *baseDataEnd = baseData + base->header_->getDataSize();
     484//   uint32_t origLength = header_->getDataSize();
     485//   uint32_t baseLength = base->header_->getDataSize();
     486
     487  assert( header_->getDataSize() && base->header_->getDataSize() );
     488 
     489  uint32_t newDataSize = header_->getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_;
     490  uint8_t *newData = new uint8_t[newDataSize]; // this is the maximum size needed in the worst case
     491  uint8_t *dest = GAMESTATE_START(newData);
     492
     493//   uint32_t baseOffset = 0; //offset in the diffed stream
     494//   uint32_t origOffset = 0; //offset in the new stream with removed 0's
    387495  std::vector<uint32_t>::iterator sizes = this->sizes_.begin();
    388496
    389   while( origOffset < origLength )
     497  while( origData < origDataEnd )
    390498  {
    391499    //iterate through all objects
    392500
    393     SynchronisableHeader h(origData+origOffset);
     501    SynchronisableHeader h(origData);
    394502
    395503    // Find (if possible) the current object in the datastream of the old gamestate
    396504    // Start at the current offset position
    397     if(baseOffset >= baseLength)
    398       baseOffset = 0;
    399     uint8_t* temp = baseData + baseOffset;
     505    if(baseData >= baseDataEnd)
     506      baseData = GAMESTATE_START(base->data_);
     507//     uint8_t* temp = baseData + baseOffset;
    400508    uint32_t objectID = h.getObjectID();
    401     assert(temp < baseData+baseLength);
    402     assert(dest < nData + origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_);
     509    assert(baseData < baseDataEnd);
     510    assert(dest < newData + newDataSize);
    403511    assert(sizes != this->sizes_.end());
    404     while ( temp < baseData+baseLength )
    405     {
    406       SynchronisableHeader htemp(temp);
     512    while ( baseData < baseDataEnd )
     513    {
     514      SynchronisableHeader htemp(baseData);
    407515      assert( htemp.getDataSize()!=0 );
    408516      if ( htemp.getObjectID() == objectID )
    409517      {
    410518        assert( h.getClassID() == htemp.getClassID() );
    411         goto DODIFF;
     519//         goto DODIFF;
     520        diffObject(dest, origData, baseData, h, sizes);
     521      }
     522      {
     523        SynchronisableHeader htemp2(baseData+htemp.getDataSize()+SynchronisableHeader::getSize());
     524        if( baseData+htemp.getDataSize()+SynchronisableHeader::getSize() < baseDataEnd )
     525        {
     526          assert(htemp2.getClassID()<500);
     527          assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
     528          assert(htemp2.isDiffed()==false);
     529        }
     530      }
     531//       assert( baseData+htemp.getDataSize()+SynchronisableHeader::getSize() <=baseData+baseLength );
     532//       temp += htemp.getDataSize()+SynchronisableHeader::getSize();
     533       
     534    }
     535    // If not found start looking at the beginning
     536    assert( baseData == baseDataEnd );
     537    baseData = GAMESTATE_START(base->data_);
     538    {
     539      SynchronisableHeader htemp2(baseData);
     540      if( baseData < baseDataEnd )
     541      {
     542        assert(htemp2.getClassID()<500);
     543        assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
     544        assert(htemp2.isDiffed()==false);
     545      }
     546    }
     547    while ( baseData < baseDataEnd )
     548    {
     549      SynchronisableHeader htemp(baseData);
     550      if ( htemp.getObjectID() == objectID )
     551      {
     552        assert( h.getClassID() == htemp.getClassID() );
     553//         goto DODIFF;
     554        diffObject(dest, origData, baseData, h, sizes);
     555        break;
    412556      }
    413557//       {
    414 //         SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize());
    415 //         if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength )
     558//         SynchronisableHeader htemp2(baseData+htemp.getDataSize()+SynchronisableHeader::getSize());
     559//         if( baseData+htemp.getDataSize()+SynchronisableHeader::getSize() < baseDataEnd )
    416560//         {
    417561//           assert(htemp2.getClassID()<500);
     
    420564//         }
    421565//       }
    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();
     566      baseData += htemp.getDataSize()+SynchronisableHeader::getSize();
    455567    }
    456568    // 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());
     569//     goto DOCOPY;
     570    if ( baseData >= baseDataEnd )
     571    {
     572      assert(baseData == baseDataEnd);
     573      copyObject(dest, origData, baseData, h, sizes);
     574      assert(sizes != this->sizes_.end() || origData>=origDataEnd);
     575    }
     576   
     577  }
     578
     579
     580  Gamestate *g = new Gamestate(newData, getClientID());
    570581  assert(g->header_);
    571582  *(g->header_) = *header_;
    572583  g->header_->setBaseID( base->getID() );
    573   g->header_->setDataSize(dest - nData - GamestateHeader::getSize());
     584  g->header_->setDataSize(dest - newData - GamestateHeader::getSize());
    574585  g->flags_=flags_;
    575586  g->packetDirection_ = packetDirection_;
  • code/branches/network3/src/libraries/network/packet/Gamestate.h

    r7163 r7333  
    130130    virtual inline bool process();
    131131    uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0);
     132//     inline void diffObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes );
     133//     inline void copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes );
    132134   
    133135    std::list<obj>          dataVector_;
Note: See TracChangeset for help on using the changeset viewer.