Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6450 for code


Ignore:
Timestamp:
Jan 18, 2010, 12:05:57 AM (15 years ago)
Author:
scheusso
Message:

further traffic reduction:

  • synchronisableheaders are now smaller( by 2 bytes per header )
  • variableID of SynchronisableVariables are now uint8_t → max 256 synchronised variables per Synchronisable
Location:
code/branches/network2/src/libraries/network
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network2/src/libraries/network/GamestateClient.cc

    r6449 r6450  
    161161      assert(b);
    162162    }
    163 //     if(gs->isDiffed()){
     163    if(gs->isDiffed())
     164    {
     165      assert(0);
    164166//       packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
    165 //       if(!base){
    166 //         COUT(3) << "could not find base gamestate id: " << gs->getBaseID() << endl;
     167//       if(!base)
     168//       {
     169//         COUT(0) << "could not find base gamestate id: " << gs->getBaseID() << endl;
     170//         assert(0);
    167171//         delete gs;
    168172//         return 0;
    169173//       }
    170 // //       assert(base); //TODO: fix this
    171174//       packet::Gamestate *undiffed = gs->undiff(base);
    172175//       delete gs;
    173176//       gs=undiffed;
    174177//       COUT(5) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
    175 //     }
     178    }
    176179    if(gs->spreadData(0x2))
    177180      return gs;
    178181    else
    179182    {
    180       COUT(3) << "could not spread gamestate" << endl;
     183      COUT(0) << "could not spread gamestate" << endl;
     184      assert(0);
    181185      return NULL;
    182186    }
  • code/branches/network2/src/libraries/network/GamestateManager.cc

    r6449 r6450  
    190190//       packet::Gamestate* gs = new packet::Gamestate(*gamestate);
    191191//     packet::Gamestate* gs = gamestate;
    192     packet::Gamestate *gs = new packet::Gamestate(*gamestate);
     192    packet::Gamestate *gs = new packet::Gamestate(*gamestate); //TODO: is this neccessary ?
    193193//     packet::Gamestate *gs = new packet::Gamestate();
    194194//     gs->collectData( id_, 0x1 );
     
    201201    if(base)
    202202    {
    203 
    204 //       COUT(3) << "diffing" << std::endl;
    205 //       packet::Gamestate* gs1  = gs;
    206       packet::Gamestate *diffed = gs->diff(base);
    207       if( diffed->getDataSize() == 0 )
     203      packet::Gamestate *diffed1 = gs->diffVariables(base);
     204      if( diffed1->getDataSize() == 0 )
    208205      {
    209         delete diffed;
     206        delete diffed1;
    210207        destgamestate = 0;
    211208        return;
    212209      }
    213       else
    214         gs = diffed;
    215       //packet::Gamestate *gs2 = diffed->undiff(gs);
    216 //       assert(*gs == *gs2);
    217 //       packet::Gamestate* gs2 = gs->undiff(client);
    218 //       gs = new packet::Gamestate(*gs);
    219 //       assert(*gs1==*gs2);
    220     }
    221     else{
     210      gs = diffed1;
     211    }
     212    else
     213    {
    222214      gs = new packet::Gamestate(*gs);
    223215    }
  • code/branches/network2/src/libraries/network/Host.h

    r6073 r6450  
    3636
    3737  const unsigned int CLIENTID_SERVER = 0;
    38   const unsigned int NETWORK_FREQUENCY = 30;
     38  const unsigned int NETWORK_FREQUENCY = 25;
    3939  const float NETWORK_PERIOD = 1.0f/NETWORK_FREQUENCY;
    4040
  • code/branches/network2/src/libraries/network/packet/Gamestate.cc

    r6449 r6450  
    181181  assert(!header_->isCompressed());
    182182  uint8_t *mem=data_+GamestateHeader::getSize();
    183   bool diffed = header_->isDiffed();
    184183  Synchronisable *s;
    185184
     
    194193      if (!GameMode::isMaster())
    195194      {
    196         Synchronisable::fabricate(mem, diffed, mode);
     195        Synchronisable::fabricate(mem, mode);
    197196      }
    198197      else
    199198      {
    200         mem += objectheader.getDataSize()+SynchronisableHeader::getSize();
     199        mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() );
    201200      }
    202201    }
     
    365364
    366365
    367 Gamestate *Gamestate::diff(Gamestate *base)
     366Gamestate* Gamestate::diffVariables(Gamestate *base)
    368367{
    369368  assert(this && base); assert(data_ && base->data_);
     
    381380  assert( origLength && baseLength );
    382381
    383   COUT(0) << "newSize: " << origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_ << endl;
    384382  uint8_t *nData = new uint8_t[origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_]; // this is the maximum size needed in the worst case
    385383  uint8_t *dest = GAMESTATE_START(nData);
     
    448446//         COUT(4) << "diff " << h.getObjectID() << ":";
    449447        // Now start to diff the Object
    450         SynchronisableHeader h2(dest);
     448        SynchronisableHeaderLight h2(dest);
    451449        h2 = h; // copy over the objectheader
    452         uint32_t variableID = 0;
    453         uint32_t newObjectOffset = SynchronisableHeader::getSize();
     450        VariableID variableID = 0;
     451        uint32_t newObjectOffset = SynchronisableHeaderLight::getSize();
    454452        // iterate through all variables
    455453        while( objectOffset < h.getDataSize()+SynchronisableHeader::getSize() )
     
    465463            {
    466464//               COUT(4) << " c" << varSize;
    467               *(uint32_t*)(dest+newObjectOffset) = variableID; // copy over the variableID
    468               newObjectOffset += sizeof(uint32_t);
     465              *(VariableID*)(dest+newObjectOffset) = variableID; // copy over the variableID
     466              newObjectOffset += sizeof(VariableID);
    469467              memcpy( dest+newObjectOffset, origData+origOffset+objectOffset, varSize );
    470468              newObjectOffset += varSize;
     
    485483//         COUT(4) << endl;
    486484        h2.setDiffed(true);
    487         h2.setDataSize(newObjectOffset-SynchronisableHeader::getSize());
     485        h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize());
    488486        assert(objectOffset == h.getDataSize()+SynchronisableHeader::getSize());
    489487        origOffset += objectOffset;
     
    520518  assert(g->header_);
    521519  *(g->header_) = *header_;
     520  g->header_->setBaseID( base->getID() );
     521  g->header_->setDataSize(dest - nData - GamestateHeader::getSize());
     522  g->flags_=flags_;
     523  g->packetDirection_ = packetDirection_;
     524  assert(!g->isCompressed());
     525  return g;
     526}
     527
     528
     529Gamestate* Gamestate::diffData(Gamestate *base)
     530{
     531  assert(this && base); assert(data_ && base->data_);
     532  assert(!header_->isCompressed() && !base->header_->isCompressed());
     533  assert(!header_->isDiffed());
     534
     535  uint8_t *basep = GAMESTATE_START(base->data_);
     536  uint8_t *gs = GAMESTATE_START(this->data_);
     537  uint32_t dest_length = header_->getDataSize();
     538
     539  if(dest_length==0)
     540    return NULL;
     541
     542  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
     543  uint8_t *dest = GAMESTATE_START(ndata);
     544
     545  rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
     546#ifndef NDEBUG
     547  uint8_t *dest2 = new uint8_t[dest_length];
     548  rawDiff( dest2, dest, basep, header_->getDataSize(), base->header_->getDataSize() );
     549  assert( memcmp( dest2, gs, dest_length) == 0 );
     550  delete dest2;
     551#endif
     552
     553  Gamestate *g = new Gamestate(ndata, getClientID());
     554  assert(g->header_);
     555  *(g->header_) = *header_;
    522556  g->header_->setDiffed( true );
    523557  g->header_->setBaseID( base->getID() );
    524   g->header_->setDataSize(dest - nData - GamestateHeader::getSize());
    525558  g->flags_=flags_;
    526559  g->packetDirection_ = packetDirection_;
     
    528561  assert(!g->isCompressed());
    529562  return g;
     563}
     564
     565
     566Gamestate* Gamestate::undiff(Gamestate *base)
     567{
     568  assert(this && base); assert(data_ && base->data_);
     569  assert(!header_->isCompressed() && !base->header_->isCompressed());
     570  assert(header_->isDiffed());
     571
     572  uint8_t *basep = GAMESTATE_START(base->data_);
     573  uint8_t *gs = GAMESTATE_START(this->data_);
     574  uint32_t dest_length = header_->getDataSize();
     575
     576  if(dest_length==0)
     577    return NULL;
     578
     579  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
     580  uint8_t *dest = ndata + GamestateHeader::getSize();
     581
     582  rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
     583
     584  Gamestate *g = new Gamestate(ndata, getClientID());
     585  assert(g->header_);
     586  *(g->header_) = *header_;
     587  g->header_->setDiffed( false );
     588  g->flags_=flags_;
     589  g->packetDirection_ = packetDirection_;
     590  assert(!g->isDiffed());
     591  assert(!g->isCompressed());
     592  return g;
     593}
     594
     595
     596void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength)
     597{
     598  uint64_t* gd = (uint64_t*)data;
     599  uint64_t* bd = (uint64_t*)basedata;
     600  uint64_t* nd = (uint64_t*)newdata;
     601
     602  unsigned int i;
     603  for( i=0; i<datalength/8; i++ )
     604  {
     605    if( i<baselength/8 )
     606      *(nd+i) = *(gd+i) ^ *(bd+i);  // xor the data
     607    else
     608      *(nd+i) = *(gd+i); // just copy over the data
     609  }
     610  unsigned int j;
     611  // now process the rest (when datalength isn't a multiple of 4)
     612  for( j = 8*(datalength/8); j<datalength; j++ )
     613  {
     614    if( j<baselength )
     615      *(newdata+j) = *(data+j) ^ *(basedata+j); // xor
     616    else
     617      *(newdata+j) = *(data+j); // just copy
     618  }
     619  assert(j==datalength);
    530620}
    531621
  • code/branches/network2/src/libraries/network/packet/Gamestate.h

    r6449 r6450  
    115115    inline int32_t getBaseID() const { return header_->getBaseID(); }
    116116    inline uint32_t getDataSize() const { return header_->getDataSize(); }
    117     Gamestate *diff(Gamestate *base);
     117    Gamestate* diffVariables(Gamestate *base);
     118    Gamestate* diffData(Gamestate *base);
     119    Gamestate *undiff(Gamestate *base);
    118120    Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);
    119121    bool compressData();
  • code/branches/network2/src/libraries/network/packet/Packet.cc

    r6417 r6450  
    6969  enetPacket_=0;
    7070  bDataENetAllocated_ = false;
    71 }
    72 
    73 void blub(ENetPacket *packet){
    74   COUT(4) << "blubb" << std::endl;
    7571}
    7672
  • code/branches/network2/src/libraries/network/synchronisable/Synchronisable.cc

    r6449 r6450  
    132132   * @return pointer to the newly created synchronisable
    133133   */
    134   Synchronisable *Synchronisable::fabricate(uint8_t*& mem, bool diffed, uint8_t mode)
     134  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode)
    135135  {
    136136    SynchronisableHeader header(mem);
     137    assert( !header.isDiffed() );
    137138
    138139    COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl;
     
    308309    if(syncList_.empty()){
    309310      assert(0);
    310       COUT(4) << "Synchronisable::updateData syncList_ is empty" << std::endl;
     311      COUT(2) << "Synchronisable::updateData syncList_ is empty" << std::endl;
    311312      return false;
    312313    }
     
    314315    uint8_t* data=mem;
    315316    // start extract header
    316     SynchronisableHeader syncHeader(mem);
    317     assert(syncHeader.getObjectID()==this->objectID_);
    318     assert(syncHeader.getCreatorID()==this->creatorID_);
    319     assert(syncHeader.getClassID()==this->classID_);
    320 
    321     mem += SynchronisableHeader::getSize();
    322     // stop extract header
     317    SynchronisableHeaderLight syncHeaderLight(mem);
     318    assert(syncHeaderLight.getObjectID()==this->getObjectID());
    323319
    324320    //COUT(5) << "Synchronisable: objectID_ " << syncHeader.getObjectID() << ", classID_ " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << std::endl;
    325     if( !syncHeader.isDiffed() )
    326     {
     321    if( !syncHeaderLight.isDiffed() )
     322    {
     323      SynchronisableHeader syncHeader2(mem);
     324      assert( this->getClassID() == syncHeader2.getClassID() );
     325      assert( this->getCreatorID() == syncHeader2.getCreatorID() );
     326      mem += SynchronisableHeader::getSize();
    327327      std::vector<SynchronisableVariableBase *>::iterator i;
    328328      for(i=syncList_.begin(); i!=syncList_.end(); i++)
    329329      {
    330         assert( mem <= data+syncHeader.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream
     330        assert( mem <= data+syncHeader2.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream
    331331        (*i)->putData( mem, mode, forceCallback );
    332332      }
     333      assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeader::getSize() );
    333334    }
    334335    else
    335336    {
    336       COUT(0) << "objectID: " << this->objectID_ << endl;
    337       while( mem < data+syncHeader.getDataSize()+SynchronisableHeader::getSize() )
     337      mem += SynchronisableHeaderLight::getSize();
     338//       COUT(0) << "objectID: " << this->objectID_ << endl;
     339      while( mem < data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() )
    338340      {
    339         uint32_t varID = *(uint32_t*)mem;
    340         COUT(0) << "varID: " << varID << endl;
    341         if( varID == 22 )
    342           COUT(6) << " blub " << endl;
     341        VariableID varID = *(VariableID*)mem;
     342//         COUT(0) << "varID: " << varID << endl;
    343343        assert( varID < syncList_.size() );
    344         mem += sizeof(uint32_t);
     344        mem += sizeof(VariableID);
    345345        syncList_[varID]->putData( mem, mode, forceCallback );
    346346      }
    347     }
    348     assert(mem == data+syncHeader.getDataSize()+SynchronisableHeader::getSize() );
     347      assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() );
     348    }
    349349    return true;
    350350  }
  • code/branches/network2/src/libraries/network/synchronisable/Synchronisable.h

    r6449 r6450  
    6464    };
    6565  }
     66 
     67  typedef uint8_t VariableID;
    6668
    6769  /**
     
    7779   */
    7880  class _NetworkExport SynchronisableHeader{
     81    friend class SynchronisableHeaderLight;
    7982    private:
    80       uint8_t *data_;
     83      uint8_t* data_;
    8184    public:
    8285      SynchronisableHeader(uint8_t* data)
    8386        { data_ = data; }
    8487      inline static uint32_t getSize()
    85         { return 16; }
    86       inline uint32_t getDataSize() const
    87         { return (*(uint32_t*)data_) & 0x7FFFFFFF; } //only use the first 31 bits
    88       inline void setDataSize(uint32_t size)
    89         { *(uint32_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint32_t*)(data_) & 0x80000000 ); }
     88        { return 14; }
     89      inline uint16_t getDataSize() const
     90        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits
     91      inline void setDataSize(uint16_t size)
     92        { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); }
    9093      inline bool isDiffed() const
    91         { return ( (*(uint32_t*)data_) & 0x80000000 ) == 0x80000000; }
     94        { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
    9295      inline void setDiffed( bool b)
    93         { *(uint32_t*)(data_) = (b << 31) | (*(uint32_t*)(data_) & 0x7FFFFFFF ); }
     96        { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }
    9497      inline uint32_t getObjectID() const
    95         { return *(uint32_t*)(data_+4); }
     98        { return *(uint32_t*)(data_+2); }
    9699      inline void setObjectID(uint32_t objectID_)
    97         { *(uint32_t*)(data_+4) = objectID_; }
     100        { *(uint32_t*)(data_+2) = objectID_; }
    98101      inline uint32_t getClassID() const
    99         { return *(uint32_t*)(data_+8); }
     102        { return *(uint32_t*)(data_+6); }
    100103      inline void setClassID(uint32_t classID_)
    101         { *(uint32_t*)(data_+8) = classID_; }
     104        { *(uint32_t*)(data_+6) = classID_; }
    102105      inline uint32_t getCreatorID() const
    103         { return *(uint32_t*)(data_+12); }
     106        { return *(uint32_t*)(data_+10); }
    104107      inline void setCreatorID(uint32_t creatorID_)
    105         { *(uint32_t*)(data_+12) = creatorID_; }
     108        { *(uint32_t*)(data_+10) = creatorID_; }
    106109      inline void operator=(SynchronisableHeader& h)
    107110        { memcpy(data_, h.data_, getSize()); }
     
    119122  class _NetworkExport SynchronisableHeaderLight{
    120123    private:
    121       uint8_t *data_;
     124      uint8_t* data_;
    122125    public:
    123       SynchronisableHeader(uint8_t* data)
     126      SynchronisableHeaderLight(uint8_t* data)
    124127        { data_ = data; }
    125128      inline static uint32_t getSize()
    126         { return 16; }
    127       inline uint32_t getDataSize() const
    128         { return (*(uint32_t*)data_) & 0x7FFFFFFF; } //only use the first 31 bits
    129       inline void setDataSize(uint32_t size)
    130         { *(uint32_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint32_t*)(data_) & 0x80000000 ); }
     129        { return 6; }
     130      inline uint16_t getDataSize() const
     131        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits
     132      inline void setDataSize(uint16_t size)
     133        { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); }
    131134      inline bool isDiffed() const
    132         { return ( (*(uint32_t*)data_) & 0x80000000 ) == 0x80000000; }
     135        { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
    133136      inline void setDiffed( bool b)
    134         { *(uint32_t*)(data_) = (b << 31) | (*(uint32_t*)(data_) & 0x7FFFFFFF ); }
     137        { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }
    135138      inline uint32_t getObjectID() const
    136         { return *(uint32_t*)(data_+4); }
     139        { return *(uint32_t*)(data_+2); }
    137140      inline void setObjectID(uint32_t objectID_)
    138         { *(uint32_t*)(data_+4) = objectID_; }
     141        { *(uint32_t*)(data_+2) = objectID_; }
    139142      inline void operator=(SynchronisableHeader& h)
    140143        { memcpy(data_, h.data_, getSize()); }
     
    153156    static void setClient(bool b);
    154157
    155     static Synchronisable *fabricate(uint8_t*& mem, bool diffed, uint8_t mode=0x0);
     158    static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0);
    156159    static bool deleteObject(uint32_t objectID_);
    157160    static Synchronisable *getSynchronisable(uint32_t objectID_);
     
    168171   
    169172    inline uint32_t getNrOfVariables(){ return this->syncList_.size(); }
    170     inline uint32_t getVarSize( uint32_t ID )
     173    inline uint32_t getVarSize( VariableID ID )
    171174    { return this->syncList_[ID]->getSize(state_); }
    172175
Note: See TracChangeset for help on using the changeset viewer.