Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 6, 2009, 9:39:41 PM (16 years ago)
Author:
scheusso
Message:

-performance/memory optimisations in gamestate and synchronisable
-enhancement of portability (gcc↔msvc)

Location:
code/branches/presentation/src/network
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation/src/network/GamestateManager.h

    r2382 r2575  
    8282    void removeClient(ClientInformation *client);
    8383    private:
    84 //     void cleanup(); // "garbage handler"
    8584    bool processGamestate(packet::Gamestate *gs);
    8685
  • code/branches/presentation/src/network/Server.cc

    r2415 r2575  
    216216  */
    217217  void Server::updateGamestate() {
     218//     if( ClientInformation::getBegin()==NULL )
     219      //no client connected
     220//       return;
    218221    gamestates_->update();
    219222    COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
     
    274277  bool Server::sendObjectDeletes(){
    275278    ClientInformation *temp = ClientInformation::getBegin();
     279    if( temp == NULL )
     280      //no client connected
     281      return true;
    276282    packet::DeleteObjects *del = new packet::DeleteObjects();
    277283    if(!del->fetchIDs())
  • code/branches/presentation/src/network/packet/DeleteObjects.cc

    r2506 r2575  
    6161  if(number==0)
    6262    return false;
    63   COUT(3) << "sending DeleteObjects: ";
     63  COUT(4) << "sending DeleteObjects: ";
    6464  unsigned int size = sizeof(ENUM::Type) + sizeof(uint32_t)*(number+1);
    6565  data_ = new uint8_t[size];
     
    7373//     assert(temp<10000); //ugly hack
    7474    *reinterpret_cast<uint32_t*>(tdata) = temp;
    75     COUT(3) << temp << " ";
     75    COUT(4) << temp << " ";
    7676    tdata += sizeof(uint32_t);
    7777  }
    78   COUT(3) << std::endl;
     78  COUT(4) << std::endl;
    7979  return true;
    8080}
     
    8787bool DeleteObjects::process(){
    8888  for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++){
    89     COUT(3) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << std::endl;
     89    COUT(4) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << std::endl;
    9090    Synchronisable::deleteObject( *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) );
    9191  }
  • code/branches/presentation/src/network/packet/Gamestate.cc

    r2501 r2575  
    3636
    3737#include <zlib.h>
    38 #include <assert.h>
     38#include <cassert>
    3939
    4040
     
    4343
    4444namespace packet {
    45 
    46 #define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
    47 #define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
    48 #define HEADER GAMESTATE_HEADER(data_)
    4945
    5046
     
    9389  ObjectList<Synchronisable>::iterator it;
    9490  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
     91   
     92#ifndef NDEBUG
    9593    tempsize=it->getSize(id, mode);
    96 
    9794    if(currentsize+tempsize > size){
    9895      assert(0); // if we don't use multithreading this part shouldn't be neccessary
     
    108105      size = currentsize+addsize;
    109106    }// stop allocate additional memory
    110 
     107#endif
    111108
    112109    //if(it->doSelection(id))
    113     if(tempsize!=0)
     110    if ( it->doSync( id, mode ) )
    114111      dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) );
    115112//     dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
     
    196193}
    197194
    198 
    199 
    200 int Gamestate::getID(){
    201   return HEADER->id;
    202 }
    203 
    204195uint32_t Gamestate::getSize() const
    205196{
     
    255246    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
    256247  }
    257 #ifndef NDEBUG
    258   //decompress and compare the start and the decompressed data
    259   uint8_t *rdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
    260   uint8_t *d2 = GAMESTATE_START(rdata);
    261   uLongf length2 = HEADER->datasize;
    262   uncompress(d2, &length2, dest, buffer);
    263   for(unsigned int i=0; i<HEADER->datasize; i++){
    264     assert(*(source+i)==*(d2+i));
    265   }
    266   delete[] rdata;
    267 #endif
    268248
    269249  //copy and modify header
    270 #ifndef NDEBUG
    271   HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->datasize);
    272 #endif
    273250  *GAMESTATE_HEADER(ndata) = *HEADER;
    274251  //delete old data
     
    305282    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
    306283  }
    307 #ifndef NDEBUG
    308   assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->datasize));
    309 #endif
    310284
    311285  //copy over the header
     
    420394    origdata += objectsize;
    421395    destsize += objectsize;
    422 //     origdata += objectsize;
    423396  }
    424397#ifndef NDEBUG
     
    438411}
    439412
    440 
    441 Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){
    442   // asserts
    443   assert(data_);
    444   assert(base->data_);
    445   assert(!GAMESTATE_HEADER(base->data_)->diffed);
    446   assert(!GAMESTATE_HEADER(base->data_)->compressed);
    447   assert(!HEADER->compressed);
    448   assert(!HEADER->diffed);
    449 
    450   //preparations
    451   std::list<obj>::iterator it;
    452   uint8_t *origdata, *basedata, *destdata, *ndata;
    453   uint32_t objectOffset, streamOffset=0;    //data offset
    454   uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
    455   synchronisableHeader *origheader;
    456   synchronisableHeader *destheader;
    457   Synchronisable *object;
    458 
    459   origdata = GAMESTATE_START(this->data_);
    460   basedata = GAMESTATE_START(base->data_);
    461   ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
    462   destdata = ndata + sizeof(GamestateHeader);
    463 
    464   // do the diff
    465   for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
    466     assert(streamOffset<HEADER->datasize);
    467     origheader = (synchronisableHeader *)(origdata+streamOffset);
    468     destheader = (synchronisableHeader *)(destdata+streamOffset);
    469     object = Synchronisable::getSynchronisable(origheader->objectID);
    470     bool sendData = object->doSelection(HEADER->id);
    471 
    472     //copy and partially diff the object header
    473     assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool));
    474     *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff)
    475     *(bool*)(destdata+sizeof(uint32_t)) = sendData;
    476     if(sendData){
    477       *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it)
    478       *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it)
    479     }else{
    480       *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
    481       *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
    482     }
    483     objectOffset=sizeof(synchronisableHeader);
    484     streamOffset+=sizeof(synchronisableHeader);
    485 
    486     //now handle the object data or fill with zeros
    487     while(objectOffset<origheader->size ){
    488 
    489       if(sendData && streamOffset<minsize)
    490         *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
    491       else if(sendData)
    492         *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
    493       else
    494         *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
    495 
    496       objectOffset++;
    497       streamOffset++;
    498     }
    499     destdata+=objectOffset;
    500     origdata+=objectOffset;
    501     basedata+=objectOffset;
    502   }
    503 
    504   //copy over the gamestate header and set the diffed flag
    505   *(GamestateHeader *)ndata = *HEADER; //copy over the header
    506   Gamestate *gs = new Gamestate(ndata);
    507   GAMESTATE_HEADER(ndata)->diffed=true;
    508   return gs;
    509 }
    510 
    511 Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){
    512   // asserts
    513   assert(data_);
    514   assert(base->data_);
    515   assert(!GAMESTATE_HEADER(base->data_)->diffed);
    516   assert(!GAMESTATE_HEADER(base->data_)->compressed);
    517   assert(!HEADER->compressed);
    518   assert(HEADER->diffed);
    519 
    520   //preparations
    521   std::list<obj>::iterator it;
    522   uint8_t *origdata, *basedata, *destdata, *ndata;
    523   uint32_t objectOffset, streamOffset=0;    //data offset
    524   uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
    525   synchronisableHeader *origheader;
    526   synchronisableHeader *destheader;
    527   Synchronisable *object;
    528 
    529   origdata = GAMESTATE_START(this->data_);
    530   basedata = GAMESTATE_START(base->data_);
    531   ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
    532   destdata = ndata + sizeof(GamestateHeader);
    533 
    534   // do the undiff
    535   for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
    536     assert(streamOffset<HEADER->datasize);
    537     origheader = (synchronisableHeader *)(origdata+streamOffset);
    538     destheader = (synchronisableHeader *)(destdata+streamOffset);
    539     object = Synchronisable::getSynchronisable( origheader->objectID );
    540     bool sendData;
    541 
    542     //copy and partially diff the object header
    543     assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool));
    544     *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff)
    545     *(bool*)(destdata+sizeof(uint32_t)) = *(bool*)(origdata+sizeof(uint32_t));
    546     sendData = *(bool*)(origdata+sizeof(uint32_t));
    547     if(sendData){
    548       *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it)
    549       *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it)
    550     }else{
    551       *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
    552       *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
    553     }
    554     objectOffset=sizeof(synchronisableHeader);
    555     streamOffset+=sizeof(synchronisableHeader);
    556 
    557     //now handle the object data or fill with zeros
    558     while(objectOffset<origheader->size ){
    559 
    560       if(sendData && streamOffset<minsize)
    561         *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
    562       else if(sendData)
    563         *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
    564       else
    565         *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
    566 
    567       objectOffset++;
    568       streamOffset++;
    569     }
    570     destdata+=objectOffset;
    571     origdata+=objectOffset;
    572     basedata+=objectOffset;
    573   }
    574 
    575   //copy over the gamestate header and set the diffed flag
    576   *(GamestateHeader *)ndata = *HEADER; //copy over the header
    577   Gamestate *gs = new Gamestate(ndata);
    578   GAMESTATE_HEADER(ndata)->diffed=false;
    579   return gs;
    580 }
    581413
    582414Gamestate *Gamestate::undiff(Gamestate *base)
     
    627459  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
    628460    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
    629 //  size+=sizeof(GamestateHeader);
    630461  return size;
    631462}
    632463
    633 /**
    634  * This function removes a Synchronisable out of the universe
    635  * @param it iterator of the list pointing to the object
    636  * @return iterator pointing to the next object in the list
    637  */
    638   void Gamestate::removeObject(ObjectList<Synchronisable>::iterator &it) {
    639     ObjectList<Synchronisable>::iterator temp=it;
    640     ++it;
    641     delete  *temp;
    642   }
    643 
    644   bool Gamestate::isDiffed(){
    645     return HEADER->diffed;
    646   }
    647 
    648   bool Gamestate::isCompressed(){
    649     return HEADER->compressed;
    650   }
    651 
    652   int Gamestate::getBaseID(){
    653     return HEADER->base_id;
    654   }
    655 }
    656 
    657 }
     464} //namespace packet
     465
     466} //namespace orxonox
  • code/branches/presentation/src/network/packet/Gamestate.h

    r2382 r2575  
    4646namespace packet {
    4747
     48#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
     49#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
     50#define HEADER GAMESTATE_HEADER(data_)
     51
    4852struct _NetworkExport GamestateHeader{
    4953  ENUM::Type packetType;
     
    5559  bool complete:1; // wheter it is a complete gamestate or only partial
    5660  bool compressed:1;
    57 #ifndef NDEBUG
    58   uint32_t crc32;
    59 #endif
    6061};
    6162
     
    7374    bool collectData(int id, uint8_t mode=0x0);
    7475    bool spreadData( uint8_t mode=0x0);
    75     int getID();
    76     bool isDiffed();
    77     bool isCompressed();
    78     int getBaseID();
     76    inline int32_t getID() const { return HEADER->id; }
     77    inline bool isDiffed() const { return HEADER->diffed; }
     78    inline bool isCompressed() const { return HEADER->compressed; }
     79    inline int32_t getBaseID() const { return HEADER->base_id; }
    7980    Gamestate *diff(Gamestate *base);
    80     Gamestate* intelligentDiff(Gamestate *base, unsigned int clientID);
    8181    Gamestate *undiff(Gamestate *base);
    82     Gamestate* intelligentUnDiff(Gamestate *base);
    8382    Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);
    8483    bool compressData();
     
    8887  private:
    8988    virtual uint32_t getSize() const;
    90     virtual bool process();
     89    virtual inline bool process();
    9190
    9291    bool operator ==(packet::Gamestate gs);
    9392  private:
    9493    uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0);
    95     void removeObject(ObjectListIterator<Synchronisable> &it);
    9694    std::list<obj> dataMap_;
    97 //     static TrafficControl *trafficControl_;
    9895};
    9996
  • code/branches/presentation/src/network/synchronisable/Synchronisable.cc

    r2540 r2575  
    422422  }
    423423
    424   bool Synchronisable::doSelection(int32_t id){
    425     return true; //TODO: change this
    426     //return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0);
    427   }
    428 
    429424  /**
    430425   * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones
  • code/branches/presentation/src/network/synchronisable/Synchronisable.h

    r2485 r2575  
    118118    uint32_t getSize(int32_t id, uint8_t mode=0x0);
    119119    bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false);
    120     bool isMyData(uint8_t* mem);
    121     bool doSelection(int32_t id);
    122     bool doSync(int32_t id, uint8_t mode=0x0);
     120    inline bool isMyData(uint8_t* mem);
     121    inline bool doSync(int32_t id, uint8_t mode=0x0);
    123122
    124123    uint32_t objectID;
  • code/branches/presentation/src/network/synchronisable/SynchronisableVariable.h

    r2371 r2575  
    7575
    7676      virtual inline uint8_t getMode(){ return mode_; }
    77       virtual void getData(uint8_t*& mem, uint8_t mode);
     77      virtual inline void getData(uint8_t*& mem, uint8_t mode);
    7878      virtual inline void putData(uint8_t*& mem, uint8_t mode, bool forceCallback = false);
    79       virtual uint32_t getSize(uint8_t mode);
     79      virtual inline uint32_t getSize(uint8_t mode);
    8080      virtual inline void* getReference(){ return (void *)&this->variable_; }
    8181    protected:
Note: See TracChangeset for help on using the changeset viewer.