Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/network/synchronisable/Synchronisable.cc @ 2415

Last change on this file since 2415 was 2415, checked in by scheusso, 15 years ago
  • adjusted some priorities of objects (movableentity, controllableentity, positionableentity, model)
  • dedicated server mode doesn't consume 100%cpu load anymore now
  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/network/src/network/synchronisable/Synchronisable.ccmergedeligible
    /code/branches/ceguilua/src/network/Synchronisable.cc1802-1808
    /code/branches/core3/src/network/Synchronisable.cc1572-1739
    /code/branches/gcc43/src/network/Synchronisable.cc1580
    /code/branches/gui/src/network/Synchronisable.cc1635-1723
    /code/branches/input/src/network/Synchronisable.cc1629-1636
    /code/branches/objecthierarchy/src/network/Synchronisable.cc1911-2085,​2100,​2110-2169
    /code/branches/pickups/src/network/Synchronisable.cc1926-2086
    /code/branches/questsystem/src/network/Synchronisable.cc1894-2088
    /code/branches/script_trigger/src/network/Synchronisable.cc1295-1953,​1955
    /code/branches/weapon/src/network/Synchronisable.cc1925-2094
File size: 14.8 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// C++ Implementation: synchronisable
32//
33// Description:
34//
35//
36// Author:  Dumeni, Oliver Scheuss, (C) 2007
37//
38// Copyright: See COPYING file that comes with this distribution
39//
40
41#include "Synchronisable.h"
42
[1837]43#include <cstring>
[2087]44#include <string>
[1505]45#include <iostream>
[1735]46#include <assert.h>
[1505]47
48#include "core/CoreIncludes.h"
[1735]49#include "core/BaseObject.h"
[1534]50// #include "core/Identifier.h"
[1505]51
[2211]52#include "network/Host.h"
[2171]53namespace orxonox
[1505]54{
[1639]55
[2309]56  std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_;
57  std::queue<uint32_t> Synchronisable::deletedObjects_;
[1639]58
[2171]59  uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
[1639]60
[1505]61  /**
62  * Constructor:
[1907]63  * Initializes all Variables and sets the right objectID
[1505]64  */
[2171]65  Synchronisable::Synchronisable(BaseObject* creator){
[1505]66    RegisterRootObject(Synchronisable);
[1907]67    static uint32_t idCounter=0;
68    objectMode_=0x1; // by default do not send data to server
[2171]69    if ( !Host::running() || ( Host::running() && Host::isServer() ) )
70    {
71      this->objectID = idCounter++; //this is only needed when running a server
72    //add synchronisable to the objectMap
73      objectMap_[this->objectID] = this;
74    }
75    else
76      objectID=OBJECTID_UNKNOWN;
[2309]77    classID = static_cast<uint32_t>(-1);
[2087]78
[2415]79    // set standard priority
80    this->setPriority( priority::normal );
[2171]81
[2415]82   
83    // get creator id
[2171]84#ifndef NDEBUG
85    ObjectList<Synchronisable>::iterator it;
86    for(it = ObjectList<Synchronisable>::begin(); it!=ObjectList<Synchronisable>::end(); ++it){
87      if( it->getObjectID()==this->objectID )
88        assert(*it==this || (it->objectID==OBJECTID_UNKNOWN && it->objectMode_==0x0));
89    }
90#endif
91
[2087]92    this->creatorID = OBJECTID_UNKNOWN;
93
94    searchcreatorID:
95    if (creator)
96    {
97        Synchronisable* synchronisable_creator = dynamic_cast<Synchronisable*>(creator);
98        if (synchronisable_creator && synchronisable_creator->objectMode_)
99        {
100            this->creatorID = synchronisable_creator->getObjectID();
101        }
102        else if (creator != creator->getCreator())
103        {
104            creator = creator->getCreator();
105            goto searchcreatorID;
106        }
107    }
[1505]108  }
109
[1907]110  /**
[2087]111   * Destructor:
[1907]112   * Delete all callback objects and remove objectID from the objectMap_
113   */
[1505]114  Synchronisable::~Synchronisable(){
[1534]115    // delete callback function objects
[2171]116    if(!Identifier::isCreatingHierarchy()){
[2245]117      for(std::list<SynchronisableVariableBase*>::iterator it = syncList.begin(); it!=syncList.end(); it++)
118        delete (*it);
[2171]119      if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer()))
[2087]120        deletedObjects_.push(objectID);
[1907]121//       COUT(3) << "destruct synchronisable +++" << objectID << " | " << classID << std::endl;
122//       COUT(3) << " bump ---" << objectID << " | " << &objectMap_ << std::endl;
123//       assert(objectMap_[objectID]->objectID==objectID);
124//       objectMap_.erase(objectID);
125    }
[2309]126    std::map<uint32_t, Synchronisable*>::iterator it;
[2171]127    it = objectMap_.find(objectID);
128    if (it != objectMap_.end())
129      objectMap_.erase(it);
[1505]130  }
[1639]131
[2087]132
[1907]133  /**
134   * This function sets the internal mode for synchronisation
135   * @param b true if this object is located on a client or on a server
136   */
[1505]137  void Synchronisable::setClient(bool b){
138    if(b) // client
139      state_=0x2;
140    else  // server
141      state_=0x1;
142  }
[1856]143
[1907]144  /**
145   * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create
146   * After calling this function the mem pointer will be increased by the size of the needed data
147   * @param mem pointer to where the appropriate data is located
148   * @param mode defines the mode, how the data should be loaded
149   * @return pointer to the newly created synchronisable
150   */
[2171]151  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode)
[1735]152  {
[1907]153    synchronisableHeader *header = (synchronisableHeader *)mem;
[1856]154
[2087]155    if(!header->dataAvailable)
156    {
157      mem += header->size;
158      return 0;
159    }
[2171]160
[2087]161    COUT(4) << "fabricating object with id: " << header->objectID << std::endl;
[1856]162
[2171]163    Identifier* id = ClassByID(header->classID);
[1907]164    assert(id);
[2171]165    BaseObject* creator = 0;
[2087]166    if (header->creatorID != OBJECTID_UNKNOWN)
167    {
168      Synchronisable* synchronisable_creator = Synchronisable::getSynchronisable(header->creatorID);
169      if (!synchronisable_creator)
170      {
171        mem += header->size; //.TODO: this suckz.... remove size from header
172        return 0;
173      }
174      else
[2171]175        creator = dynamic_cast<BaseObject*>(synchronisable_creator);
[2087]176    }
[2171]177    assert(getSynchronisable(header->objectID)==0);   //make sure no object with this id exists
178    BaseObject *bo = id->fabricate(creator);
[2087]179    assert(bo);
[1735]180    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
181    assert(no);
[1907]182    no->objectID=header->objectID;
[2087]183    no->creatorID=header->creatorID; //TODO: remove this
[1907]184    no->classID=header->classID;
[2087]185    COUT(4) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
[1735]186          // update data and create object/entity...
[2087]187    bool b = no->updateData(mem, mode, true);
[1907]188    assert(b);
[2087]189    if (b)
190    {
[2245]191//        b = no->create();
[2087]192        assert(b);
193    }
[1907]194    return no;
195  }
196
[2087]197
[1907]198  /**
199   * Finds and deletes the Synchronisable with the appropriate objectID
200   * @param objectID objectID of the Synchronisable
201   * @return true/false
202   */
[2309]203  bool Synchronisable::deleteObject(uint32_t objectID){
[1907]204//     assert(getSynchronisable(objectID));
205    if(!getSynchronisable(objectID))
[1735]206      return false;
[1907]207    assert(getSynchronisable(objectID)->objectID==objectID);
208//     delete objectMap_[objectID];
209    Synchronisable *s = getSynchronisable(objectID);
210    if(s)
211      delete s;
212    else
[1735]213      return false;
214    return true;
215  }
[2087]216
[1907]217  /**
218   * This function looks up the objectID in the objectMap_ and returns a pointer to the right Synchronisable
219   * @param objectID objectID of the Synchronisable
220   * @return pointer to the Synchronisable with the objectID
221   */
[2309]222  Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID){
223    std::map<uint32_t, Synchronisable*>::iterator it1;
[2171]224    it1 = objectMap_.find(objectID);
225    if (it1 != objectMap_.end())
226      return it1->second;
227
228    ObjectList<Synchronisable>::iterator it;
229    for(it = ObjectList<Synchronisable>::begin(); it; ++it){
230      if( it->getObjectID()==objectID ){
231        objectMap_[objectID] = *it;
232        return *it;
233      }
[1907]234    }
235    return NULL;
236  }
237
[2087]238
[1505]239  /**
240  * This function is used to register a variable to be synchronized
241  * also counts the total datasize needed to save the variables
242  * @param var pointer to the variable
243  * @param size size of the datatype the variable consists of
[2171]244  * @param t the type of the variable (DATA or STRING
[1907]245  * @param mode same as in getData
246  * @param cb callback object that should get called, if the value of the variable changes
[1505]247  */
[2245]248
249/*  void Synchronisable::registerVariable(void *var, int size, variableType t, uint8_t mode, NetworkCallbackBase *cb){
[2087]250    assert( mode==direction::toclient || mode==direction::toserver || mode==direction::serverMaster || mode==direction::clientMaster);
[1505]251    // create temporary synch.Var struct
252    synchronisableVariable *temp = new synchronisableVariable;
253    temp->size = size;
254    temp->var = var;
[1639]255    temp->mode = mode;
[1505]256    temp->type = t;
[1534]257    temp->callback = cb;
[2087]258    if( ( mode & direction::bidirectional ) )
259    {
[2171]260      if(t!=STRING)
261      {
262        temp->varBuffer = new uint8_t[size];
263        memcpy(temp->varBuffer, temp->var, size); //now fill the buffer for the first time
264      }
265      else
266      {
267        temp->varBuffer=new std::string( *static_cast<std::string*>(var) );
268      }
[2087]269      temp->varReference = 0;
270    }
[1639]271    COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl;
[1505]272    //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl;
273    COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl;
274    syncList->push_back(temp);
[1907]275#ifndef NDEBUG
276    std::list<synchronisableVariable *>::iterator it = syncList->begin();
277    while(it!=syncList->end()){
278      assert(*it!=var);
279      it++;
280    }
281#endif
[2245]282  }*/
283 
[1856]284
[1735]285  /**
[1907]286   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory
[1735]287   * takes a pointer to already allocated memory (must have at least getSize bytes length)
[1751]288   * structure of the bitstream:
[1907]289   * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...|
290   * length of varx: size saved int syncvarlist
291   * @param mem pointer to allocated memory with enough size
292   * @param id gamestateid of the gamestate to be saved (important for priorities)
293   * @param mode defines the direction in which the data will be send/received
294   *             0x1: server->client
295   *             0x2: client->server (not recommended)
296   *             0x3: bidirectional
[2087]297   * @return true: if !doSync or if everything was successfully saved
[1735]298   */
[2309]299  bool Synchronisable::getData(uint8_t*& mem, int32_t id, uint8_t mode){
[2171]300    if(mode==0x0)
301      mode=state_;
[1907]302    //if this tick is we dont synchronise, then abort now
[2171]303    if(!doSync(id, mode))
[1907]304      return true;
[1735]305    //std::cout << "inside getData" << std::endl;
[2309]306    uint32_t tempsize = 0;
[2316]307    if (this->classID==0)
[1735]308      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
[2087]309
[2309]310    if (this->classID == static_cast<uint32_t>(-1))
[2087]311        this->classID = this->getIdentifier()->getNetworkID();
312
[1907]313    assert(this->classID==this->getIdentifier()->getNetworkID());
314//     this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this
[2245]315    std::list<SynchronisableVariableBase*>::iterator i;
[2309]316    uint32_t size;
[1907]317    size=getSize(id, mode);
[1856]318
[1735]319    // start copy header
[1907]320    synchronisableHeader *header = (synchronisableHeader *)mem;
321    header->size = size;
322    header->objectID = this->objectID;
[2087]323    header->creatorID = this->creatorID;
[1907]324    header->classID = this->classID;
325    header->dataAvailable = true;
[2171]326    tempsize += sizeof(synchronisableHeader);
327    mem += sizeof(synchronisableHeader);
[1735]328    // end copy header
[1856]329
330
[1735]331    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl;
332    // copy to location
[2245]333    for(i=syncList.begin(); i!=syncList.end(); ++i){
334      (*i)->getData( mem, mode );
335      tempsize += (*i)->getSize( mode );
[1735]336    }
337    assert(tempsize==size);
338    return true;
339  }
[1505]340
[1856]341
[1505]342  /**
[1907]343   * This function takes a bytestream and loads the data into the registered variables
344   * @param mem pointer to the bytestream
345   * @param mode same as in getData
[1735]346   * @return true/false
347   */
[2171]348  bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){
[1735]349    if(mode==0x0)
350      mode=state_;
[2245]351    std::list<SynchronisableVariableBase *>::iterator i;
[2171]352    //assert(objectMode_!=0x0);
353    //assert( (mode ^ objectMode_) != 0);
[2245]354    if(syncList.empty()){
[1735]355      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
356      return false;
357    }
[1856]358
[2245]359    uint8_t* data=mem;
[1735]360    // start extract header
[1907]361    synchronisableHeader *syncHeader = (synchronisableHeader *)mem;
362    assert(syncHeader->objectID==this->objectID);
[2171]363    assert(syncHeader->creatorID==this->creatorID);
364    assert(this->classID==syncHeader->classID); //TODO: fix this!!! maybe a problem with the identifier ?
[1907]365    if(syncHeader->dataAvailable==false){
[2171]366      mem += syncHeader->size;
[1751]367      return true;
[1907]368    }
[1856]369
[2171]370    mem += sizeof(synchronisableHeader);
[1907]371    // stop extract header
[2087]372
[1907]373    COUT(5) << "Synchronisable: objectID " << syncHeader->objectID << ", classID " << syncHeader->classID << " size: " << syncHeader->size << " synchronising data" << std::endl;
[2245]374    for(i=syncList.begin(); i!=syncList.end() && mem <= data+syncHeader->size; i++)
375    {
376      (*i)->putData( mem, mode, forceCallback );
[1735]377    }
[2171]378    assert(mem == data+syncHeader->size);
[1735]379    return true;
380  }
[1505]381
382  /**
383  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
[1907]384  * @param id id of the gamestate
385  * @param mode same as getData
[1505]386  * @return amount of bytes
387  */
[2309]388  uint32_t Synchronisable::getSize(int32_t id, uint8_t mode){
[1907]389    int tsize=sizeof(synchronisableHeader);
[1505]390    if(mode==0x0)
391      mode=state_;
[2171]392    if(!doSync(id, mode))
393      return 0;
[2245]394    std::list<SynchronisableVariableBase*>::iterator i;
395    for(i=syncList.begin(); i!=syncList.end(); i++){
396      tsize += (*i)->getSize( mode );
[1505]397    }
398    return tsize;
399  }
[1639]400
[1735]401  /**
[1907]402   * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction)
403   * @param id gamestate id
404   * @return true/false
[1735]405   */
[2309]406  bool Synchronisable::doSync(int32_t id, uint8_t mode){
[2171]407    if(mode==0x0)
408      mode=state_;
[2245]409    return ( (objectMode_&mode)!=0 && (!syncList.empty() ) );
[1735]410  }
[1856]411
[2309]412  bool Synchronisable::doSelection(int32_t id){
[2171]413    return true; //TODO: change this
414    //return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0);
[1751]415  }
[1856]416
[1907]417  /**
418   * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones
419   * @param mem pointer to the bytestream
420   */
421  bool Synchronisable::isMyData(uint8_t* mem)
[1735]422  {
[1907]423    synchronisableHeader *header = (synchronisableHeader *)mem;
424    assert(header->objectID==this->objectID);
425    return header->dataAvailable;
[1735]426  }
[1856]427
[1907]428  /**
429   * This function sets the synchronisation mode of the object
[2171]430   * If set to 0x0 variables will not be synchronised at all
[1907]431   * If set to 0x1 variables will only be synchronised to the client
432   * If set to 0x2 variables will only be synchronised to the server
433   * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar)
434   * @param mode same as in registerVar
435   */
[2171]436  void Synchronisable::setObjectMode(uint8_t mode){
[2087]437    assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3);
[1751]438    objectMode_=mode;
[1505]439  }
[2245]440 
[1505]441
442}
Note: See TracBrowser for help on using the repository browser.