Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7754


Ignore:
Timestamp:
Dec 10, 2010, 8:13:57 PM (14 years ago)
Author:
scheusso
Message:

some optimisations (mostly including gamestateheader inside gamestate)

Location:
code/branches/network4/src/libraries/network
Files:
3 edited

Legend:

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

    r7753 r7754  
    7272
    7373Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
    74   Packet(data, clientID)
     74  Packet(data, clientID), header_(data)
    7575{
    7676  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
    77   header_ = new GamestateHeader(data_);
    78 }
    79 
    80 
    81 Gamestate::Gamestate(uint8_t *data)
     77}
     78
     79
     80Gamestate::Gamestate(uint8_t *data):
     81  header_(data)
    8282{
    8383  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
    8484  data_ = data;
    85   header_ = new GamestateHeader(data_);
    8685}
    8786
    8887
    8988Gamestate::Gamestate(const Gamestate& g) :
    90     Packet( *(Packet*)&g ), nrOfVariables_(0)
     89  Packet( *(Packet*)&g ), header_(this->data_), nrOfVariables_(0)
    9190{
    9291  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
    93   header_ = new GamestateHeader(data_);
    9492  sizes_ = g.sizes_;
    9593}
     
    9896Gamestate::~Gamestate()
    9997{
    100   if( header_ )
    101     delete header_;
    10298}
    10399
     
    105101bool Gamestate::collectData(int id, uint8_t mode)
    106102{
    107   assert(this->header_==0); // make sure the header didn't exist before
    108103  uint32_t tempsize=0, currentsize=0;
    109104  assert(data_==0);
     
    120115  }
    121116
    122   // create the header object
    123   assert( header_ == 0 );
    124   header_ = new GamestateHeader(data_);
     117  // tell the gamestate header where to store the data
     118  header_.setData(this->data_);
    125119
    126120  //start collect data synchronisable by synchronisable
     
    161155
    162156  //start write gamestate header
    163   header_->setDataSize( currentsize );
    164   header_->setID( id );
    165   header_->setBaseID( GAMESTATEID_INITIAL );
    166   header_->setDiffed( false );
    167   header_->setComplete( true );
    168   header_->setCompressed( false );
     157  header_.setDataSize( currentsize );
     158  header_.setID( id );
     159  header_.setBaseID( GAMESTATEID_INITIAL );
     160  header_.setDiffed( false );
     161  header_.setComplete( true );
     162  header_.setCompressed( false );
    169163  //stop write gamestate header
    170164
     
    177171bool Gamestate::spreadData(uint8_t mode)
    178172{
    179   COUT(4) << "processing gamestate with id " << header_->getID() << endl;
     173  COUT(4) << "processing gamestate with id " << header_.getID() << endl;
    180174  assert(data_);
    181   assert(!header_->isCompressed());
     175  assert(!header_.isCompressed());
    182176  uint8_t *mem=data_+GamestateHeader::getSize();
    183177  Synchronisable *s;
    184178
    185179  // update the data of the objects we received
    186   while(mem < data_+GamestateHeader::getSize()+header_->getDataSize())
     180  while(mem < data_+GamestateHeader::getSize()+header_.getDataSize())
    187181  {
    188182    SynchronisableHeader objectheader(mem);
     
    249243{
    250244  assert(data_);
    251   if(header_->isCompressed())
    252     return header_->getCompSize()+GamestateHeader::getSize();
     245  if(header_.isCompressed())
     246    return header_.getCompSize()+GamestateHeader::getSize();
    253247  else
    254248  {
    255     return header_->getDataSize()+GamestateHeader::getSize();
     249    return header_.getDataSize()+GamestateHeader::getSize();
    256250  }
    257251}
     
    280274{
    281275  assert(data_);
    282   assert(!header_->isCompressed());
    283   uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1);
     276  assert(!header_.isCompressed());
     277  uLongf buffer = (uLongf)(((header_.getDataSize() + 12)*1.01)+1);
    284278  if(buffer==0)
    285279    return false;
     
    289283  uint8_t *source = data_ + GamestateHeader::getSize();
    290284  int retval;
    291   retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) );
     285  retval = compress( dest, &buffer, source, (uLong)(header_.getDataSize()) );
    292286  switch ( retval )
    293287  {
     
    299293
    300294  //copy and modify header
    301   GamestateHeader *temp = header_;
    302   header_ = new GamestateHeader(ndata, temp);
     295  GamestateHeader *temp = new GamestateHeader(data_);
     296  header_.setData(ndata);
     297  header_ = *temp;
    303298  delete temp;
    304299  //delete old data
     
    306301  //save new data
    307302  data_ = ndata;
    308   header_->setCompSize( buffer );
    309   header_->setCompressed( true );
    310   COUT(0) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;
     303  header_.setCompSize( buffer );
     304  header_.setCompressed( true );
     305  COUT(0) << "gamestate compress datasize: " << header_.getDataSize() << " compsize: " << header_.getCompSize() << std::endl;
    311306  return true;
    312307}
     
    316311{
    317312  assert(data_);
    318   assert(header_->isCompressed());
    319   COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl;
    320   uint32_t datasize = header_->getDataSize();
    321   uint32_t compsize = header_->getCompSize();
     313  assert(header_.isCompressed());
     314  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_.getID() << ", baseid: " << header_.getBaseID() << ", datasize: " << header_.getDataSize() << ", compsize: " << header_.getCompSize() << std::endl;
     315  uint32_t datasize = header_.getDataSize();
     316  uint32_t compsize = header_.getCompSize();
    322317  uint32_t bufsize;
    323318  bufsize = datasize;
     
    338333
    339334  //copy over the header
    340   GamestateHeader *temp = header_;
    341   header_ = new GamestateHeader( data_, header_ );
     335  GamestateHeader* temp = new GamestateHeader( data_ );
     336  header_.setData(ndata);
     337  header_ = *temp;
    342338  delete temp;
    343339
     
    357353  //set new pointers
    358354  data_ = ndata;
    359   header_->setCompressed( false );
    360   assert(header_->getDataSize()==datasize);
    361   assert(header_->getCompSize()==compsize);
     355  header_.setCompressed( false );
     356  assert(header_.getDataSize()==datasize);
     357  assert(header_.getCompSize()==compsize);
    362358  return true;
    363359}
     
    512508{
    513509  assert(this && base); assert(data_ && base->data_);
    514   assert(!header_->isCompressed() && !base->header_->isCompressed());
    515   assert(!header_->isDiffed());
    516   assert( header_->getDataSize() && base->header_->getDataSize() );
     510  assert(!header_.isCompressed() && !base->header_.isCompressed());
     511  assert(!header_.isDiffed());
     512  assert( header_.getDataSize() && base->header_.getDataSize() );
    517513
    518514
     
    521517  uint8_t *baseDataPtr = GAMESTATE_START(base->data_);
    522518  uint8_t *origDataPtr = GAMESTATE_START(this->data_);
    523   uint8_t *origDataEnd = origDataPtr + header_->getDataSize();
    524   uint8_t *baseDataEnd = baseDataPtr + base->header_->getDataSize();
    525 //   uint32_t origLength = header_->getDataSize();
    526 //   uint32_t baseLength = base->header_->getDataSize();
     519  uint8_t *origDataEnd = origDataPtr + header_.getDataSize();
     520  uint8_t *baseDataEnd = baseDataPtr + base->header_.getDataSize();
     521//   uint32_t origLength = header_.getDataSize();
     522//   uint32_t baseLength = base->header_.getDataSize();
    527523
    528524  // Allocate new space for diffed gamestate
    529   uint32_t newDataSize = header_->getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_;
     525  uint32_t newDataSize = header_.getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_;
    530526  uint8_t *newData = new uint8_t[newDataSize]; // this is the maximum size needed in the worst case
    531527  uint8_t *destDataPtr = GAMESTATE_START(newData);
     
    598594
    599595  Gamestate *g = new Gamestate(newData, getClientID());
    600   assert(g->header_);
    601   *(g->header_) = *header_;
    602   g->header_->setBaseID( base->getID() );
    603   g->header_->setDataSize(destDataPtr - newData - GamestateHeader::getSize());
     596  (g->header_) = header_;
     597  g->header_.setBaseID( base->getID() );
     598  g->header_.setDataSize(destDataPtr - newData - GamestateHeader::getSize());
    604599  g->flags_=flags_;
    605600  g->packetDirection_ = packetDirection_;
     
    612607{
    613608  assert(this && base); assert(data_ && base->data_);
    614   assert(!header_->isCompressed() && !base->header_->isCompressed());
    615   assert(!header_->isDiffed());
     609  assert(!header_.isCompressed() && !base->header_.isCompressed());
     610  assert(!header_.isDiffed());
    616611
    617612  uint8_t *basep = GAMESTATE_START(base->data_);
    618613  uint8_t *gs = GAMESTATE_START(this->data_);
    619   uint32_t dest_length = header_->getDataSize();
     614  uint32_t dest_length = header_.getDataSize();
    620615
    621616  if(dest_length==0)
     
    625620  uint8_t *dest = GAMESTATE_START(ndata);
    626621
    627   rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
     622  rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
    628623#ifndef NDEBUG
    629624  uint8_t *dest2 = new uint8_t[dest_length];
    630   rawDiff( dest2, dest, basep, header_->getDataSize(), base->header_->getDataSize() );
     625  rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() );
    631626  assert( memcmp( dest2, gs, dest_length) == 0 );
    632627  delete dest2;
     
    636631  assert(g->header_);
    637632  *(g->header_) = *header_;
    638   g->header_->setDiffed( true );
    639   g->header_->setBaseID( base->getID() );
     633  g->header_.setDiffed( true );
     634  g->header_.setBaseID( base->getID() );
    640635  g->flags_=flags_;
    641636  g->packetDirection_ = packetDirection_;
     
    649644{
    650645  assert(this && base); assert(data_ && base->data_);
    651   assert(!header_->isCompressed() && !base->header_->isCompressed());
    652   assert(header_->isDiffed());
     646  assert(!header_.isCompressed() && !base->header_.isCompressed());
     647  assert(header_.isDiffed());
    653648
    654649  uint8_t *basep = GAMESTATE_START(base->data_);
    655650  uint8_t *gs = GAMESTATE_START(this->data_);
    656   uint32_t dest_length = header_->getDataSize();
     651  uint32_t dest_length = header_.getDataSize();
    657652
    658653  if(dest_length==0)
     
    662657  uint8_t *dest = ndata + GamestateHeader::getSize();
    663658
    664   rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
     659  rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
    665660
    666661  Gamestate *g = new Gamestate(ndata, getClientID());
    667662  assert(g->header_);
    668663  *(g->header_) = *header_;
    669   g->header_->setDiffed( false );
     664  g->header_.setDiffed( false );
    670665  g->flags_=flags_;
    671666  g->packetDirection_ = packetDirection_;
     
    708703
    709704  // allocate memory for new data
    710   uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
     705  uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()];
    711706  // create a gamestate out of it
    712707  Gamestate *gs = new Gamestate(gdata);
     
    724719
    725720  //call TrafficControl
    726   TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), dataVector_ );
     721  TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ );
    727722
    728723  //copy in the zeros
     
    755750#ifndef NDEBUG
    756751  uint32_t origsize = destsize;
    757   while ( origsize < header_->getDataSize() )
     752  while ( origsize < header_.getDataSize() )
    758753  {
    759754    SynchronisableHeader oldobjectheader(origdata);
     
    762757    origsize += objectsize;
    763758  }
    764   assert(origsize==header_->getDataSize());
     759  assert(origsize==header_.getDataSize());
    765760  assert(destsize!=0);
    766761#endif
    767   gs->header_->setDataSize( destsize );
     762  gs->header_.setDataSize( destsize );
    768763  return gs;
    769764}*/
  • code/branches/network4/src/libraries/network/packet/Gamestate.h

    r7753 r7754  
    4848class _NetworkExport GamestateHeader{
    4949  public:
    50     GamestateHeader(uint8_t *data){ assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; }
    51     GamestateHeader(uint8_t *data, GamestateHeader* h)
    52     { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }
     50    GamestateHeader();
     51    GamestateHeader(uint8_t* data)
     52      { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; }
     53    /*GamestateHeader(uint8_t* data, GamestateHeader* h)
     54      { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }*/
     55    void setData(uint8_t* data)
     56      { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; }
    5357    static inline uint32_t getSize()
    54     { return 21; }
     58      { return 21; }
    5559
    5660    inline int32_t getID() const
    57     { assert(data_); return *(int32_t*)(data_+4); }
     61      { assert(data_); return *(int32_t*)(data_+4); }
    5862    inline void setID(int32_t id)
    59     { assert(data_); *(int32_t*)(data_+4) = id; }
     63      { assert(data_); *(int32_t*)(data_+4) = id; }
    6064
    6165    inline int32_t getBaseID() const
    62     { assert(data_); return *(int32_t*)(data_+8); }
     66      { assert(data_); return *(int32_t*)(data_+8); }
    6367    inline void setBaseID(int32_t id)
    64     { assert(data_); *(int32_t*)(data_+8) = id; }
     68      { assert(data_); *(int32_t*)(data_+8) = id; }
    6569
    6670    inline uint32_t getDataSize() const
    67     { assert(data_); return *(uint32_t*)(data_+12); }
     71      { assert(data_); return *(uint32_t*)(data_+12); }
    6872    inline void setDataSize(uint32_t size)
    69     { assert(data_); *(uint32_t*)(data_+12) = size; }
     73      { assert(data_); *(uint32_t*)(data_+12) = size; }
    7074
    7175    inline uint32_t getCompSize() const
     
    7579
    7680    inline bool isDiffed() const
    77     { assert(data_); return *(int8_t*)(data_+20) & 0x1; }
     81      { assert(data_); return *(int8_t*)(data_+20) & 0x1; }
    7882    inline void setDiffed(bool b)
    79     { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }
     83      { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }
    8084
    8185    inline bool isComplete() const
    82     { assert(data_); return *(int8_t*)(data_+20) & 0x2; }
     86      { assert(data_); return *(int8_t*)(data_+20) & 0x2; }
    8387    inline void setComplete(bool b)
    84     { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }
     88      { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }
    8589
    8690    inline bool isCompressed() const
    87     { assert(data_); return *(int8_t*)(data_+20) & 0x4; }
     91      { assert(data_); return *(int8_t*)(data_+20) & 0x4; }
    8892    inline void setCompressed(bool b)
    89     { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }
     93      { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }
    9094
    9195    inline void operator=(GamestateHeader& h)
    92     { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); }
     96      { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); }
    9397  private:
    94     uint8_t *data_;
     98    uint8_t* data_;
    9599
    96100};
     
    110114    bool collectData(int id, uint8_t mode=0x0);
    111115    bool spreadData( uint8_t mode=0x0);
    112     inline int32_t getID() const { return header_->getID(); }
    113     inline bool isDiffed() const { return header_->isDiffed(); }
    114     inline bool isCompressed() const { return header_->isCompressed(); }
    115     inline int32_t getBaseID() const { return header_->getBaseID(); }
    116     inline uint32_t getDataSize() const { return header_->getDataSize(); }
     116    inline int32_t getID() const { return header_.getID(); }
     117    inline bool isDiffed() const { return header_.isDiffed(); }
     118    inline bool isCompressed() const { return header_.isCompressed(); }
     119    inline int32_t getBaseID() const { return header_.getBaseID(); }
     120    inline uint32_t getDataSize() const { return header_.getDataSize(); }
    117121    Gamestate* diffVariables(Gamestate *base);
    118122//     Gamestate* diffData(Gamestate *base);
     
    134138   
    135139    std::list<obj>          dataVector_;
    136     GamestateHeader*        header_;
     140    GamestateHeader         header_;
    137141    std::vector<uint32_t>   sizes_;
    138142    uint32_t                nrOfVariables_;
  • code/branches/network4/src/libraries/network/synchronisable/Synchronisable.cc

    r7497 r7754  
    321321      mem += SynchronisableHeader::getSize();
    322322      std::vector<SynchronisableVariableBase *>::iterator i;
    323       for(i=syncList_.begin(); i!=syncList_.end(); i++)
     323      for(i=syncList_.begin(); i!=syncList_.end(); ++i)
    324324      {
    325325        assert( mem <= data+syncHeader2.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream
Note: See TracChangeset for help on using the changeset viewer.