Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network2/src/libraries/network/synchronisable/Synchronisable.cc @ 6450

Last change on this file since 6450 was 6450, checked in by scheusso, 14 years ago

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
  • Property svn:eol-style set to native
File size: 14.1 KB
RevLine 
[1505]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Dumeni Manatschal, (C) 2007
24 *      Oliver Scheuss, (C) 2007
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30
31#include "Synchronisable.h"
32
[3214]33#include <cstdlib>
[1505]34#include "core/CoreIncludes.h"
[5742]35#include "core/GameMode.h"
[1735]36#include "core/BaseObject.h"
[3214]37#include "network/Host.h"
[1505]38
[2171]39namespace orxonox
[1505]40{
[1639]41
[2309]42  std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_;
43  std::queue<uint32_t> Synchronisable::deletedObjects_;
[1639]44
[2171]45  uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
[1639]46
[1505]47  /**
48  * Constructor:
[5929]49  * Initializes all Variables and sets the right objectID_
[1505]50  */
[5929]51  Synchronisable::Synchronisable(BaseObject* creator ){
[1505]52    RegisterRootObject(Synchronisable);
[1907]53    static uint32_t idCounter=0;
54    objectMode_=0x1; // by default do not send data to server
[5742]55    if ( GameMode::isMaster() || ( Host::running() && Host::isServer() ) )
[2171]56    {
[5929]57      this->setObjectID( idCounter++ );
[2171]58    }
59    else
[5743]60    {
[5929]61      objectID_=OBJECTID_UNKNOWN;
[5743]62    }
[5929]63    classID_ = static_cast<uint32_t>(-1);
[6417]64
[3084]65    // set dataSize to 0
66    this->dataSize_ = 0;
[2415]67    // set standard priority
[3280]68    this->setPriority( Priority::Normal );
[2171]69
[2415]70    // get creator id
[5929]71    if( creator )
72      this->creatorID_ = creator->getSceneID();
73    else
74      this->creatorID_ = OBJECTID_UNKNOWN;
[2087]75
[5929]76    /*searchcreatorID:
[2087]77    if (creator)
78    {
[3325]79        Synchronisable* synchronisable_creator = orxonox_cast<Synchronisable*>(creator);
[2087]80        if (synchronisable_creator && synchronisable_creator->objectMode_)
81        {
[5929]82            this->creatorID = synchronisable_creator->getScene()->getObjectID();
[2087]83        }
84        else if (creator != creator->getCreator())
85        {
86            creator = creator->getCreator();
87            goto searchcreatorID;
88        }
[5929]89    }*/
[1505]90  }
91
[1907]92  /**
[2087]93   * Destructor:
[5929]94   * Delete all callback objects and remove objectID_ from the objectMap_
[1907]95   */
[1505]96  Synchronisable::~Synchronisable(){
[1534]97    // delete callback function objects
[2171]98    if(!Identifier::isCreatingHierarchy()){
[3198]99      // remove object from the static objectMap
100      if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer()))
[5929]101        deletedObjects_.push(objectID_);
[1907]102    }
[6449]103    // delete all Synchronisable Variables from syncList_ ( which are also in stringList_ )
104    for(std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin(); it!=syncList_.end(); it++)
[3304]105      delete (*it);
[6449]106    syncList_.clear();
107    stringList_.clear();
[2309]108    std::map<uint32_t, Synchronisable*>::iterator it;
[5929]109    it = objectMap_.find(objectID_);
[2171]110    if (it != objectMap_.end())
111      objectMap_.erase(it);
[2485]112
[1505]113  }
[1639]114
[2087]115
[1907]116  /**
117   * This function sets the internal mode for synchronisation
118   * @param b true if this object is located on a client or on a server
119   */
[1505]120  void Synchronisable::setClient(bool b){
121    if(b) // client
122      state_=0x2;
123    else  // server
124      state_=0x1;
125  }
[1856]126
[1907]127  /**
128   * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create
129   * After calling this function the mem pointer will be increased by the size of the needed data
130   * @param mem pointer to where the appropriate data is located
131   * @param mode defines the mode, how the data should be loaded
132   * @return pointer to the newly created synchronisable
133   */
[6450]134  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode)
[1735]135  {
[2662]136    SynchronisableHeader header(mem);
[6450]137    assert( !header.isDiffed() );
[1856]138
[2662]139    COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl;
[1856]140
[2662]141    Identifier* id = ClassByID(header.getClassID());
[2485]142    if (!id)
143    {
[3088]144        for(int i = 0; i<160; i++)
[2759]145            COUT(0) << "classid: " << i << " identifier: " << ClassByID(i) << endl;
[2485]146        COUT(0) << "Assertion failed: id" << std::endl;
147        COUT(0) << "Possible reason for this error: Client received a synchronizable object whose class has no factory." << std::endl;
148        abort();
149    }
[1907]150    assert(id);
[2171]151    BaseObject* creator = 0;
[2662]152    if (header.getCreatorID() != OBJECTID_UNKNOWN)
[2087]153    {
[2662]154      Synchronisable* synchronisable_creator = Synchronisable::getSynchronisable(header.getCreatorID());
[2087]155      if (!synchronisable_creator)
156      {
[6449]157        mem += header.getDataSize()+SynchronisableHeader::getSize(); //.TODO: this suckz.... remove size from header
[2662]158        assert(0); // TODO: uncomment this if we have a clean objecthierarchy (with destruction of children of objects) ^^
[2087]159        return 0;
160      }
161      else
[3325]162        creator = orxonox_cast<BaseObject*>(synchronisable_creator);
[2087]163    }
[2662]164    assert(getSynchronisable(header.getObjectID())==0);   //make sure no object with this id exists
[2171]165    BaseObject *bo = id->fabricate(creator);
[2087]166    assert(bo);
[3325]167    Synchronisable *no = orxonox_cast<Synchronisable*>(bo);
[1735]168    assert(no);
[5929]169    assert( Synchronisable::objectMap_.find(header.getObjectID()) == Synchronisable::objectMap_.end() );
170    no->setObjectID(header.getObjectID());
171    //no->creatorID=header.getCreatorID(); //TODO: remove this
172    no->setClassID(header.getClassID());
173    assert(no->creatorID_ == header.getCreatorID());
174    //assert(no->classID_ == header.getClassID());
175    COUT(4) << "fabricate objectID_: " << no->objectID_ << " classID_: " << no->classID_ << std::endl;
[1735]176          // update data and create object/entity...
[2087]177    bool b = no->updateData(mem, mode, true);
[1907]178    assert(b);
[2087]179    if (b)
180    {
[2245]181//        b = no->create();
[2087]182        assert(b);
183    }
[1907]184    return no;
185  }
186
[2087]187
[1907]188  /**
[5929]189   * Finds and deletes the Synchronisable with the appropriate objectID_
190   * @param objectID_ objectID_ of the Synchronisable
[1907]191   * @return true/false
192   */
[5929]193  bool Synchronisable::deleteObject(uint32_t objectID_){
194    if(!getSynchronisable(objectID_))
[1735]195      return false;
[5929]196    assert(getSynchronisable(objectID_)->objectID_==objectID_);
197    Synchronisable *s = getSynchronisable(objectID_);
[1907]198    if(s)
[5929]199      s->destroy(); // or delete?
[1907]200    else
[1735]201      return false;
202    return true;
203  }
[2087]204
[1907]205  /**
[5929]206   * This function looks up the objectID_ in the objectMap_ and returns a pointer to the right Synchronisable
207   * @param objectID_ objectID_ of the Synchronisable
208   * @return pointer to the Synchronisable with the objectID_
[1907]209   */
[5929]210  Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID_){
[2309]211    std::map<uint32_t, Synchronisable*>::iterator it1;
[5929]212    it1 = objectMap_.find(objectID_);
[2171]213    if (it1 != objectMap_.end())
214      return it1->second;
215
[3198]216//     ObjectList<Synchronisable>::iterator it;
217//     for(it = ObjectList<Synchronisable>::begin(); it; ++it){
[5929]218//       if( it->getObjectID()==objectID_ ){
219//         objectMap_[objectID_] = *it;
[3198]220//         return *it;
221//       }
222//     }
223    // if the objects not in the map it should'nt exist at all anymore
[1907]224    return NULL;
225  }
226
[2087]227
[1505]228  /**
[5929]229   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID_ and classID_ to the given memory
[1735]230   * takes a pointer to already allocated memory (must have at least getSize bytes length)
[1751]231   * structure of the bitstream:
[5929]232   * |totalsize,objectID_,classID_,var1,var2,string1_length,string1,var3,...|
[1907]233   * length of varx: size saved int syncvarlist
234   * @param mem pointer to allocated memory with enough size
235   * @param id gamestateid of the gamestate to be saved (important for priorities)
236   * @param mode defines the direction in which the data will be send/received
237   *             0x1: server->client
238   *             0x2: client->server (not recommended)
239   *             0x3: bidirectional
[2087]240   * @return true: if !doSync or if everything was successfully saved
[1735]241   */
[6449]242  uint32_t Synchronisable::getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode){
243    unsigned int test = 0;
[2171]244    if(mode==0x0)
245      mode=state_;
[1907]246    //if this tick is we dont synchronise, then abort now
[2171]247    if(!doSync(id, mode))
[3084]248      return 0;
[2309]249    uint32_t tempsize = 0;
[3304]250#ifndef NDEBUG
[6449]251    uint8_t* oldmem = mem;
[5929]252    if (this->classID_==0)
[1735]253      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
[3304]254#endif
[2087]255
[5929]256    if (this->classID_ == static_cast<uint32_t>(-1))
257        this->classID_ = this->getIdentifier()->getNetworkID();
[2087]258
[5929]259    assert(ClassByID(this->classID_));
260    assert(this->classID_==this->getIdentifier()->getNetworkID());
261    assert(this->objectID_!=OBJECTID_UNKNOWN);
[3084]262    std::vector<SynchronisableVariableBase*>::iterator i;
[1856]263
[1735]264    // start copy header
[2662]265    SynchronisableHeader header(mem);
266    mem += SynchronisableHeader::getSize();
[1735]267    // end copy header
[1856]268
[6449]269    CCOUT(5) << "getting data from objectID_: " << objectID_ << ", classID_: " << classID_ << std::endl;
270//     COUT(4) << "objectid: " << this->objectID_ << ":";
[1735]271    // copy to location
[6449]272    for(i=syncList_.begin(); i!=syncList_.end(); ++i){
273      uint32_t varsize = (*i)->getData( mem, mode );
274//       COUT(4) << " " << varsize;
275      tempsize += varsize;
276      sizes.push_back(varsize);
277      ++test;
[3084]278      //tempsize += (*i)->getSize( mode );
[1735]279    }
[6449]280//     COUT(4) << endl;
[6417]281
[5929]282    header.setObjectID( this->objectID_ );
283    header.setCreatorID( this->creatorID_ );
284    header.setClassID( this->classID_ );
[3084]285    header.setDataSize( tempsize );
[6449]286    assert( tempsize == mem-oldmem-SynchronisableHeader::getSize() );
287    assert( test == this->getNrOfVariables() );
288    header.setDiffed(false);
289    tempsize += SynchronisableHeader::getSize();
[6417]290
[3084]291#ifndef NDEBUG
292    uint32_t size;
293    size=getSize(id, mode);
[1735]294    assert(tempsize==size);
[3084]295#endif
296    return tempsize;
[1735]297  }
[1505]298
[1856]299
[1505]300  /**
[1907]301   * This function takes a bytestream and loads the data into the registered variables
302   * @param mem pointer to the bytestream
303   * @param mode same as in getData
[1735]304   * @return true/false
305   */
[2171]306  bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){
[1735]307    if(mode==0x0)
308      mode=state_;
[6449]309    if(syncList_.empty()){
[2419]310      assert(0);
[6450]311      COUT(2) << "Synchronisable::updateData syncList_ is empty" << std::endl;
[1735]312      return false;
313    }
[1856]314
[2245]315    uint8_t* data=mem;
[1735]316    // start extract header
[6450]317    SynchronisableHeaderLight syncHeaderLight(mem);
318    assert(syncHeaderLight.getObjectID()==this->getObjectID());
[1856]319
[5929]320    //COUT(5) << "Synchronisable: objectID_ " << syncHeader.getObjectID() << ", classID_ " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << std::endl;
[6450]321    if( !syncHeaderLight.isDiffed() )
[2245]322    {
[6450]323      SynchronisableHeader syncHeader2(mem);
324      assert( this->getClassID() == syncHeader2.getClassID() );
325      assert( this->getCreatorID() == syncHeader2.getCreatorID() );
326      mem += SynchronisableHeader::getSize();
[6449]327      std::vector<SynchronisableVariableBase *>::iterator i;
328      for(i=syncList_.begin(); i!=syncList_.end(); i++)
329      {
[6450]330        assert( mem <= data+syncHeader2.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream
[6449]331        (*i)->putData( mem, mode, forceCallback );
332      }
[6450]333      assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeader::getSize() );
[1735]334    }
[6449]335    else
336    {
[6450]337      mem += SynchronisableHeaderLight::getSize();
338//       COUT(0) << "objectID: " << this->objectID_ << endl;
339      while( mem < data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() )
[6449]340      {
[6450]341        VariableID varID = *(VariableID*)mem;
342//         COUT(0) << "varID: " << varID << endl;
[6449]343        assert( varID < syncList_.size() );
[6450]344        mem += sizeof(VariableID);
[6449]345        syncList_[varID]->putData( mem, mode, forceCallback );
346      }
[6450]347      assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() );
[6449]348    }
[1735]349    return true;
350  }
[1505]351
352  /**
353  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
[1907]354  * @param id id of the gamestate
355  * @param mode same as getData
[1505]356  * @return amount of bytes
357  */
[2309]358  uint32_t Synchronisable::getSize(int32_t id, uint8_t mode){
[6449]359    uint32_t tsize=SynchronisableHeader::getSize();
[3084]360    if (mode==0x0)
[1505]361      mode=state_;
[3084]362    if (!doSync(id, mode))
[2171]363      return 0;
[3084]364    assert( mode==state_ );
365    tsize += this->dataSize_;
366    std::vector<SynchronisableVariableBase*>::iterator i;
[6449]367    for(i=stringList_.begin(); i!=stringList_.end(); ++i){
[2245]368      tsize += (*i)->getSize( mode );
[1505]369    }
370    return tsize;
371  }
[1639]372
[1735]373  /**
[1907]374   * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction)
375   * @param id gamestate id
376   * @return true/false
[1735]377   */
[2309]378  bool Synchronisable::doSync(int32_t id, uint8_t mode){
[2171]379    if(mode==0x0)
380      mode=state_;
[6449]381    return ( (this->objectMode_ & mode)!=0 && (!syncList_.empty() ) );
[1735]382  }
[1856]383
[1907]384  /**
385   * This function sets the synchronisation mode of the object
[2171]386   * If set to 0x0 variables will not be synchronised at all
[1907]387   * If set to 0x1 variables will only be synchronised to the client
388   * If set to 0x2 variables will only be synchronised to the server
389   * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar)
390   * @param mode same as in registerVar
391   */
[5929]392  void Synchronisable::setSyncMode(uint8_t mode){
[2087]393    assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3);
[5929]394    this->objectMode_=mode;
[1505]395  }
396
[6417]397  template <> void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
398  {
399    SynchronisableVariableBase* sv;
400    if (bidirectional)
401      sv = new SynchronisableVariableBidirectional<std::string>(variable, mode, cb);
402    else
403      sv = new SynchronisableVariable<std::string>(variable, mode, cb);
[6449]404    syncList_.push_back(sv);
405    stringList_.push_back(sv);
[6417]406  }
[2485]407
[6417]408
[1505]409}
Note: See TracBrowser for help on using the repository browser.