Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 11, 2010, 8:55:13 AM (14 years ago)
Author:
dafrick
Message:

Merged presentation3 branch into trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/network/synchronisable/Synchronisable.h

    r6417 r7163  
    3737#include <map>
    3838#include <queue>
     39#include <set>
    3940
    4041#include "util/mbool.h"
     
    6465    };
    6566  }
     67 
     68  typedef uint8_t VariableID;
    6669
    6770  /**
     
    7174   * in an emulated bitset.
    7275   * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
    73    * Bit 32 is a bool and defines whether the data is actually stored or is just filled up with 0
     76   * Bit 32 is a bool and defines whether the variables are stored in diff mode
    7477   * Byte 5 to 8: objectID_
    7578   * Byte 9 to 12: classID_
     
    7780   */
    7881  class _NetworkExport SynchronisableHeader{
     82    friend class SynchronisableHeaderLight;
    7983    private:
    80       uint8_t *data_;
     84      uint8_t* data_;
    8185    public:
    8286      SynchronisableHeader(uint8_t* data)
    8387        { data_ = data; }
    8488      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 ); }
    90       inline bool isDataAvailable() const
    91         { return ( (*(uint32_t*)data_) & 0x80000000 ) == 0x80000000; }
    92       inline void setDataAvailable( bool b)
    93         { *(uint32_t*)(data_) = (b << 31) | (*(uint32_t*)(data_) & 0x7FFFFFFF ); }
     89        { return 14; }
     90      inline uint16_t getDataSize() const
     91        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 15 bits
     92      inline void setDataSize(uint16_t size)
     93        { *(uint16_t*)(data_) = (size & 0x7FFF) | (*(uint16_t*)(data_) & 0x8000 ); }
     94      inline bool isDiffed() const
     95        { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
     96      inline void setDiffed( bool b)
     97        { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }
    9498      inline uint32_t getObjectID() const
    95         { return *(uint32_t*)(data_+4); }
     99        { return *(uint32_t*)(data_+2); }
    96100      inline void setObjectID(uint32_t objectID_)
    97         { *(uint32_t*)(data_+4) = objectID_; }
     101        { *(uint32_t*)(data_+2) = objectID_; }
    98102      inline uint32_t getClassID() const
    99         { return *(uint32_t*)(data_+8); }
     103        { return *(uint32_t*)(data_+6); }
    100104      inline void setClassID(uint32_t classID_)
    101         { *(uint32_t*)(data_+8) = classID_; }
     105        { *(uint32_t*)(data_+6) = classID_; }
    102106      inline uint32_t getCreatorID() const
    103         { return *(uint32_t*)(data_+12); }
     107        { return *(uint32_t*)(data_+10); }
    104108      inline void setCreatorID(uint32_t creatorID_)
    105         { *(uint32_t*)(data_+12) = creatorID_; }
     109        { *(uint32_t*)(data_+10) = creatorID_; }
    106110      inline void operator=(SynchronisableHeader& h)
    107111        { memcpy(data_, h.data_, getSize()); }
    108112  };
    109113
     114    /**
     115   * @brief: stores information about a Synchronisable (light version)
     116   *
     117   * This class stores the information about a Synchronisable (objectID_, dataSize)
     118   * in an emulated bitset.
     119   * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
     120   * Bit 32 is a bool and defines whether the variables are stored in diff mode
     121   * Byte 5 to 8: objectID_
     122   */
     123  class _NetworkExport SynchronisableHeaderLight{
     124    private:
     125      uint8_t* data_;
     126    public:
     127      SynchronisableHeaderLight(uint8_t* data)
     128        { data_ = data; }
     129      inline static uint32_t getSize()
     130        { return 6; }
     131      inline uint16_t getDataSize() const
     132        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits
     133      inline void setDataSize(uint16_t size)
     134        { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); }
     135      inline bool isDiffed() const
     136        { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
     137      inline void setDiffed( bool b)
     138        { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }
     139      inline uint32_t getObjectID() const
     140        { return *(uint32_t*)(data_+2); }
     141      inline void setObjectID(uint32_t objectID_)
     142        { *(uint32_t*)(data_+2) = objectID_; }
     143      inline void operator=(SynchronisableHeader& h)
     144        { memcpy(data_, h.data_, getSize()); }
     145  };
    110146
    111147  /**
     
    134170
    135171    void setSyncMode(uint8_t mode);
     172   
     173    inline uint32_t getNrOfVariables(){ return this->syncList_.size(); }
     174    inline uint32_t getVarSize( VariableID ID )
     175    { return this->syncList_[ID]->getSize(state_); }
    136176
    137177  protected:
    138178    Synchronisable(BaseObject* creator);
    139179    template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
     180    template <class T> void registerVariable(std::set<T>& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
     181    template <class T> void unregisterVariable(T& var);
    140182
    141183    void setPriority(unsigned int freq){ objectFrequency_ = freq; }
     
    143185
    144186  private:
    145     uint32_t getData(uint8_t*& men, int32_t id, uint8_t mode=0x0);
     187    uint32_t getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode);
    146188    uint32_t getSize(int32_t id, uint8_t mode=0x0);
    147189    bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false);
    148     bool isMyData(uint8_t* mem);
    149190    bool doSync(int32_t id, uint8_t mode=0x0);
    150191
     
    156197    uint32_t classID_;
    157198
    158     std::vector<SynchronisableVariableBase*> syncList;
    159     std::vector<SynchronisableVariableBase*> stringList;
     199    std::vector<SynchronisableVariableBase*> syncList_;
     200    std::vector<SynchronisableVariableBase*> stringList_;
    160201    uint32_t dataSize_; //size of all variables except strings
    161202    static uint8_t state_; // detemines wheter we are server (default) or client
     
    171212    if (bidirectional)
    172213    {
    173       syncList.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
    174       this->dataSize_ += syncList.back()->getSize(state_);
     214      syncList_.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
     215      this->dataSize_ += syncList_.back()->getSize(state_);
    175216    }
    176217    else
    177218    {
    178       syncList.push_back(new SynchronisableVariable<T>(variable, mode, cb));
     219      syncList_.push_back(new SynchronisableVariable<T>(variable, mode, cb));
    179220      if ( this->state_ == mode )
    180         this->dataSize_ += syncList.back()->getSize(state_);
     221        this->dataSize_ += syncList_.back()->getSize(state_);
    181222    }
    182223  }
     224 
     225  template <class T> void Synchronisable::unregisterVariable(T& variable){
     226    std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin();
     227    while(it!=syncList_.end()){
     228      if( ((*it)->getReference()) == &variable ){
     229        this->dataSize_ -= (*it)->getSize(Synchronisable::state_);
     230        delete (*it);
     231        syncList_.erase(it);
     232        return;
     233      }
     234      else
     235        it++;
     236    }
     237    bool unregistered_nonexistent_variable = false;
     238    assert(unregistered_nonexistent_variable); //if we reach this point something went wrong:
     239    // the variable has not been registered before
     240  }
     241
     242  template <class T> void Synchronisable::registerVariable( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
     243  {
     244    SynchronisableVariableBase* sv;
     245    if (bidirectional)
     246      sv = new SynchronisableVariableBidirectional<std::set<T> >(variable, mode, cb);
     247    else
     248      sv = new SynchronisableVariable<std::set<T> >(variable, mode, cb);
     249    syncList_.push_back(sv);
     250    stringList_.push_back(sv);
     251  }
    183252
    184253  template <> _NetworkExport void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
     254//   template <class T> _NetworkExport void Synchronisable::registerVariable<std::set<T> >( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
     255  template <> _NetworkExport void Synchronisable::unregisterVariable( std::string& variable );
    185256
    186257
Note: See TracChangeset for help on using the changeset viewer.