Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 10, 2010, 5:07:02 PM (14 years ago)
Author:
scheusso
Message:

-(hopefully) fixed that nasty bug
-some cleaning up
-readding hackaddbots and renaming it to dedicatedAddBots

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

Legend:

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

    r7497 r7753  
    364364
    365365
    366 inline void /*Gamestate::*/diffObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
     366inline void /*Gamestate::*/diffObject( uint8_t*& newDataPtr, uint8_t*& origDataPtr, uint8_t*& baseDataPtr, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
    367367{
    368368  //       COUT(4) << "dodiff" << endl;
     
    371371  //         assert(origOffset==0);
    372372  //       }
     373  assert( objectHeader.getDataSize() == SynchronisableHeader(baseDataPtr).getDataSize() );
     374 
    373375  uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData
    374376  // Check whether the whole object stayed the same
    375   if( memcmp( origData+objectOffset, baseData+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();
     377  if( memcmp( origDataPtr+objectOffset, baseDataPtr+objectOffset, objectHeader.getDataSize()) == 0 )
     378  {
     379//     COUT(4) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl;
     380    origDataPtr += objectOffset + objectHeader.getDataSize(); // skip the whole object
     381    baseDataPtr += objectOffset + objectHeader.getDataSize();
    380382    sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables();
    381383  }
    382384  else
    383385  {
    384     //         if( Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() == "Bot" )
     386    //         if( Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() == "Bot" )
    385387    //           COUT(0) << "blub" << endl;
    386388    //         COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
    387389    //         COUT(4) << "diff " << h.getObjectID() << ":";
    388390    // Now start to diff the Object
    389     SynchronisableHeaderLight h2(newData);
    390     h2 = objectHeader; // copy over the objectheader
     391    SynchronisableHeaderLight newObjectHeader(newDataPtr);
     392    newObjectHeader = objectHeader; // copy over the objectheader
    391393    VariableID variableID = 0;
    392     uint32_t newObjectOffset = SynchronisableHeaderLight::getSize();
     394    uint32_t diffedObjectOffset = SynchronisableHeaderLight::getSize();
    393395    // iterate through all variables
    394396    while( objectOffset < objectHeader.getDataSize()+SynchronisableHeader::getSize() )
     
    401403      if ( varSize != 0 )
    402404      {
    403         if ( memcmp(origData+objectOffset, baseData+objectOffset, varSize) != 0 )
     405        if ( memcmp(origDataPtr+objectOffset, baseDataPtr+objectOffset, varSize) != 0 )
    404406        {
    405407          //               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;
     408          *(VariableID*)(newDataPtr+diffedObjectOffset) = variableID; // copy over the variableID
     409          diffedObjectOffset += sizeof(VariableID);
     410          memcpy( newDataPtr+diffedObjectOffset, origDataPtr+objectOffset, varSize );
     411          diffedObjectOffset += varSize;
    410412          objectOffset += varSize;
    411413        }
     
    427429    //         COUT(4) << endl;
    428430   
    429     h2.setDiffed(true);
    430     h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize());
     431    newObjectHeader.setDiffed(true);
     432    newObjectHeader.setDataSize(diffedObjectOffset-SynchronisableHeaderLight::getSize());
    431433    assert(objectOffset == objectHeader.getDataSize()+SynchronisableHeader::getSize());
    432     assert(h2.getDataSize()>0);
    433     origData += objectOffset;
     434    assert(newObjectHeader.getDataSize()>0);
     435    origDataPtr += objectOffset;
    434436    //         baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
    435437    //baseOffset += objectOffset;
     
    445447    //           }
    446448    //         }
    447     baseData += objectOffset;
    448     newData += newObjectOffset;
     449    baseDataPtr += objectOffset;
     450    newDataPtr += diffedObjectOffset;
    449451  }
    450452}
     
    457459  newData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
    458460  origData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
    459   SynchronisableHeader baseHeader( baseData );
    460   baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize();
     461//   SynchronisableHeader baseHeader( baseData );
     462//   baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize();
    461463  //       COUT(4) << "copy " << h.getObjectID() << endl;
    462464  //       COUT(4) << "copy " << h.getObjectID() << ":";
     
    470472}
    471473
     474inline bool findObject(uint8_t*& dataPtr, uint8_t* endPtr, SynchronisableHeader& objectHeader)
     475{
     476  // Some assertions to make sure the dataPtr is valid (pointing to a SynchronisableHeader)
     477  {
     478    SynchronisableHeader htemp2(dataPtr);
     479    assert(htemp2.getClassID()<500);
     480    assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
     481    assert(htemp2.isDiffed()==false);
     482  }
     483  uint32_t objectID = objectHeader.getObjectID();
     484  while ( dataPtr < endPtr )
     485  {
     486    SynchronisableHeader htemp(dataPtr);
     487    assert( htemp.getDataSize()!=0 );
     488    if ( htemp.getObjectID() == objectID )
     489    {
     490      assert( objectHeader.getClassID() == htemp.getClassID() );
     491      assert( objectHeader.getCreatorID() == htemp.getCreatorID() );
     492      return true;
     493    }
     494    {
     495      if( dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize() < endPtr )
     496      {
     497        SynchronisableHeader htemp2(dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize());
     498        assert(htemp2.getClassID()<500);
     499        assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
     500        assert(htemp2.isDiffed()==false);
     501      }
     502    }
     503    dataPtr += htemp.getDataSize()+SynchronisableHeader::getSize();
     504   
     505  }
     506  assert(dataPtr == endPtr);
     507 
     508  return false;
     509}
     510
    472511Gamestate* Gamestate::diffVariables(Gamestate *base)
    473512{
     
    475514  assert(!header_->isCompressed() && !base->header_->isCompressed());
    476515  assert(!header_->isDiffed());
     516  assert( header_->getDataSize() && base->header_->getDataSize() );
    477517
    478518
    479519  // *** first do a raw diff of the two gamestates
    480520
    481   uint8_t *baseData = GAMESTATE_START(base->data_);
    482   uint8_t *origData = GAMESTATE_START(this->data_);
    483   uint8_t *origDataEnd = origData + header_->getDataSize();
    484   uint8_t *baseDataEnd = baseData + base->header_->getDataSize();
     521  uint8_t *baseDataPtr = GAMESTATE_START(base->data_);
     522  uint8_t *origDataPtr = GAMESTATE_START(this->data_);
     523  uint8_t *origDataEnd = origDataPtr + header_->getDataSize();
     524  uint8_t *baseDataEnd = baseDataPtr + base->header_->getDataSize();
    485525//   uint32_t origLength = header_->getDataSize();
    486526//   uint32_t baseLength = base->header_->getDataSize();
    487527
    488   assert( header_->getDataSize() && base->header_->getDataSize() );
    489  
     528  // Allocate new space for diffed gamestate
    490529  uint32_t newDataSize = header_->getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_;
    491530  uint8_t *newData = new uint8_t[newDataSize]; // this is the maximum size needed in the worst case
    492   uint8_t *dest = GAMESTATE_START(newData);
    493 
    494 //   uint32_t baseOffset = 0; //offset in the diffed stream
    495 //   uint32_t origOffset = 0; //offset in the new stream with removed 0's
    496   std::vector<uint32_t>::iterator sizes = this->sizes_.begin();
    497 
    498   while( origData < origDataEnd )
     531  uint8_t *destDataPtr = GAMESTATE_START(newData);
     532
     533  std::vector<uint32_t>::iterator sizesIt = this->sizes_.begin();
     534
     535  while( origDataPtr < origDataEnd )
    499536  {
    500537    //iterate through all objects
    501538
    502     SynchronisableHeader h(origData);
     539    SynchronisableHeader origHeader(origDataPtr);
    503540
    504541    // Find (if possible) the current object in the datastream of the old gamestate
    505542    // Start at the current offset position
    506     if(baseData >= baseDataEnd)
    507       baseData = GAMESTATE_START(base->data_);
    508 //     uint8_t* temp = baseData + baseOffset;
    509     uint32_t objectID = h.getObjectID();
    510     assert(baseData < baseDataEnd);
    511     assert(dest < newData + newDataSize);
    512     assert(sizes != this->sizes_.end());
     543    if(baseDataPtr == baseDataEnd)
     544      baseDataPtr = GAMESTATE_START(base->data_);
     545    uint8_t* oldBaseDataPtr = baseDataPtr;
     546   
     547    assert(baseDataPtr < baseDataEnd);
     548    assert(destDataPtr < newData + newDataSize);
     549    assert(sizesIt != this->sizes_.end());
     550   
    513551    bool diffedObject = false;
    514     while ( baseData < baseDataEnd )
    515     {
    516       SynchronisableHeader htemp(baseData);
    517       assert( htemp.getDataSize()!=0 );
    518       if ( htemp.getObjectID() == objectID )
    519       {
    520         assert( h.getClassID() == htemp.getClassID() );
    521 //         goto DODIFF;
    522         diffObject(dest, origData, baseData, h, sizes);
     552    if( findObject(baseDataPtr, baseDataEnd, origHeader) )
     553    {
     554      if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() )
     555      {
     556//         COUT(4) << "diffing object in order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     557        diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
    523558        diffedObject = true;
    524         break;
    525       }
    526       {
    527         SynchronisableHeader htemp2(baseData+htemp.getDataSize()+SynchronisableHeader::getSize());
    528         if( baseData+htemp.getDataSize()+SynchronisableHeader::getSize() < baseDataEnd )
     559      }
     560      else
     561      {
     562//         COUT(4) << "copy object because of different data sizes (1): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     563        copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
     564        assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
     565      }
     566       
     567    }
     568    else
     569    {
     570      assert( baseDataPtr == baseDataEnd );
     571      baseDataPtr = GAMESTATE_START(base->data_);
     572      if( findObject(baseDataPtr, oldBaseDataPtr, origHeader) )
     573      {
     574        if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() )
    529575        {
    530           assert(htemp2.getClassID()<500);
    531           assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
    532           assert(htemp2.isDiffed()==false);
     576//           COUT(4) << "diffing object out of order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     577          diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
     578          diffedObject = true;
    533579        }
    534       }
    535       baseData += htemp.getDataSize()+SynchronisableHeader::getSize();
    536 //       assert( baseData+htemp.getDataSize()+SynchronisableHeader::getSize() <=baseData+baseLength );
    537 //       temp += htemp.getDataSize()+SynchronisableHeader::getSize();
    538      
    539     }
    540     // If not found start looking at the beginning
    541     assert( diffedObject || baseData == baseDataEnd );
    542     baseData = GAMESTATE_START(base->data_);
    543     {
    544       SynchronisableHeader htemp2(baseData);
    545       if( baseData < baseDataEnd )
    546       {
    547         assert(htemp2.getClassID()<500);
    548         assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
    549         assert(htemp2.isDiffed()==false);
    550       }
    551     }
    552     while ( !diffedObject && baseData < baseDataEnd )
    553     {
    554       SynchronisableHeader htemp(baseData);
    555       if ( htemp.getObjectID() == objectID )
    556       {
    557         assert( h.getClassID() == htemp.getClassID() );
    558 //         goto DODIFF;
    559         diffObject(dest, origData, baseData, h, sizes);
    560         break;
    561       }
    562 //       {
    563 //         SynchronisableHeader htemp2(baseData+htemp.getDataSize()+SynchronisableHeader::getSize());
    564 //         if( baseData+htemp.getDataSize()+SynchronisableHeader::getSize() < baseDataEnd )
    565 //         {
    566 //           assert(htemp2.getClassID()<500);
    567 //           assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
    568 //           assert(htemp2.isDiffed()==false);
    569 //         }
    570 //       }
    571       baseData += htemp.getDataSize()+SynchronisableHeader::getSize();
    572     }
    573     // Object is new, thus never transmitted -> just copy over
    574 //     goto DOCOPY;
    575     if ( baseData >= baseDataEnd )
    576     {
    577       assert(baseData == baseDataEnd);
    578       copyObject(dest, origData, baseData, h, sizes);
    579       assert(sizes != this->sizes_.end() || origData>=origDataEnd);
    580     }
    581   }
    582   assert(sizes==this->sizes_.end());
     580        else
     581        {
     582//           COUT(4) << "copy object because of different data sizes (2): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     583          copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
     584          assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
     585        }
     586      }
     587      else
     588      {
     589//         COUT(4) << "copy object: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     590        assert(baseDataPtr == oldBaseDataPtr);
     591        copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
     592        assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
     593      }
     594    }
     595  }
     596  assert(sizesIt==this->sizes_.end());
    583597
    584598
     
    587601  *(g->header_) = *header_;
    588602  g->header_->setBaseID( base->getID() );
    589   g->header_->setDataSize(dest - newData - GamestateHeader::getSize());
     603  g->header_->setDataSize(destDataPtr - newData - GamestateHeader::getSize());
    590604  g->flags_=flags_;
    591605  g->packetDirection_ = packetDirection_;
     
    595609
    596610
    597 Gamestate* Gamestate::diffData(Gamestate *base)
     611/*Gamestate* Gamestate::diffData(Gamestate *base)
    598612{
    599613  assert(this && base); assert(data_ && base->data_);
     
    686700  }
    687701  assert(j==datalength);
    688 }
    689 
    690 
    691 Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
     702}*/
     703
     704
     705/*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
    692706  assert(data_);
    693707  std::list<obj>::iterator it;
     
    753767  gs->header_->setDataSize( destsize );
    754768  return gs;
    755 }
     769}*/
    756770
    757771
  • code/branches/network4/src/libraries/network/packet/Gamestate.h

    r7497 r7753  
    116116    inline uint32_t getDataSize() const { return header_->getDataSize(); }
    117117    Gamestate* diffVariables(Gamestate *base);
    118     Gamestate* diffData(Gamestate *base);
    119     Gamestate *undiff(Gamestate *base);
    120     Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);
     118//     Gamestate* diffData(Gamestate *base);
     119//     Gamestate *undiff(Gamestate *base);
     120//     Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);
    121121    bool compressData();
    122122    bool decompressData();
     
    125125    // Packet functions
    126126  private:
    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 );
     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 );
    129129    virtual uint32_t getSize() const;
    130130    virtual inline bool process();
Note: See TracChangeset for help on using the changeset viewer.