Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2470 was 2464, checked in by rgrieder, 16 years ago

Bugfix in network: When checking for other objectIDs, it was possible that within the constructor of one Synchronisable, another one was created. In the check then, both have objectID == OBJECTID_UNKNOWN because the objectID of the first object was not yet set (done after construction).

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