Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network64/src/network/synchronisable/Synchronisable.cc @ 2307

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