Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2749 was 2749, checked in by scheusso, 15 years ago

changed the process of classid (or networkid in the factory) synchronisation:
the packet classid synchronises all classid at once 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/buildsystem3/src/network/synchronisable/Synchronisable.cc2662-2708
    /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/physics_merge/src/network/synchronisable/Synchronisable.cc2436-2457
    /code/branches/pickups/src/network/Synchronisable.cc1926-2086
    /code/branches/presentation/src/network/synchronisable/Synchronisable.cc2654-2660
    /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: 13.3 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#include "Synchronisable.h"
32
33#include <cstring>
34#include <string>
35#include <iostream>
36#include <assert.h>
37
38#include "core/CoreIncludes.h"
39#include "core/BaseObject.h"
40// #include "core/Identifier.h"
41
42#include "network/Host.h"
43namespace orxonox
44{
45
46  std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_;
47  std::queue<uint32_t> Synchronisable::deletedObjects_;
48
49  uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
50
51  /**
52  * Constructor:
53  * Initializes all Variables and sets the right objectID
54  */
55  Synchronisable::Synchronisable(BaseObject* creator){
56    RegisterRootObject(Synchronisable);
57    static uint32_t idCounter=0;
58    objectMode_=0x1; // by default do not send data to server
59    if ( !Host::running() || ( Host::running() && Host::isServer() ) )
60    {
61      this->objectID = idCounter++; //this is only needed when running a server
62    //add synchronisable to the objectMap
63      objectMap_[this->objectID] = this;
64    }
65    else
66      objectID=OBJECTID_UNKNOWN;
67    classID = static_cast<uint32_t>(-1);
68
69    // set standard priority
70    this->setPriority( priority::normal );
71
72    // get creator id
73    this->creatorID = OBJECTID_UNKNOWN;
74
75    searchcreatorID:
76    if (creator)
77    {
78        Synchronisable* synchronisable_creator = dynamic_cast<Synchronisable*>(creator);
79        if (synchronisable_creator && synchronisable_creator->objectMode_)
80        {
81            this->creatorID = synchronisable_creator->getObjectID();
82        }
83        else if (creator != creator->getCreator())
84        {
85            creator = creator->getCreator();
86            goto searchcreatorID;
87        }
88    }
89  }
90
91  /**
92   * Destructor:
93   * Delete all callback objects and remove objectID from the objectMap_
94   */
95  Synchronisable::~Synchronisable(){
96    // delete callback function objects
97    if(!Identifier::isCreatingHierarchy()){
98      for(std::list<SynchronisableVariableBase*>::iterator it = syncList.begin(); it!=syncList.end(); it++)
99        delete (*it);
100      if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer()))
101        deletedObjects_.push(objectID);
102//       COUT(3) << "destruct synchronisable +++" << objectID << " | " << classID << std::endl;
103//       COUT(3) << " bump ---" << objectID << " | " << &objectMap_ << std::endl;
104//       assert(objectMap_[objectID]->objectID==objectID);
105//       objectMap_.erase(objectID);
106    }
107    std::map<uint32_t, Synchronisable*>::iterator it;
108    it = objectMap_.find(objectID);
109    if (it != objectMap_.end())
110      objectMap_.erase(it);
111
112    //HACK HACK HACK HACK HACK HACK
113    // this hack ensures that children of this object also get destroyed
114//     ObjectList<Synchronisable>::iterator it2, it3;
115//     // get total size of gamestate
116//     for(it2 = ObjectList<Synchronisable>::begin(); it2; ++it2)
117//     {
118//       if ( it2->getCreatorID() == this->objectID && it2->getCreatorID() != OBJECTID_UNKNOWN )
119//       {
120//         Synchronisable::deleteObject( it2->getObjectID() );
121//       }
122//     }
123    //HACK HACK HACK HACK HACK HACK
124  }
125
126
127  /**
128   * This function sets the internal mode for synchronisation
129   * @param b true if this object is located on a client or on a server
130   */
131  void Synchronisable::setClient(bool b){
132    if(b) // client
133      state_=0x2;
134    else  // server
135      state_=0x1;
136  }
137
138  /**
139   * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create
140   * After calling this function the mem pointer will be increased by the size of the needed data
141   * @param mem pointer to where the appropriate data is located
142   * @param mode defines the mode, how the data should be loaded
143   * @return pointer to the newly created synchronisable
144   */
145  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode)
146  {
147    SynchronisableHeader header(mem);
148
149    if(!header.isDataAvailable())
150    {
151      mem += header.getDataSize();
152      return 0;
153    }
154
155    COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl;
156
157    Identifier* id = ClassByID(header.getClassID());
158    if (!id)
159    {
160        for(int i = 0; i<100; i++)
161            COUT(0) << "classid: " << i << " identifier: " << ClassByID(i) << endl;
162        COUT(0) << "Assertion failed: id" << std::endl;
163        COUT(0) << "Possible reason for this error: Client received a synchronizable object whose class has no factory." << std::endl;
164        abort();
165    }
166    assert(id);
167    BaseObject* creator = 0;
168    if (header.getCreatorID() != OBJECTID_UNKNOWN)
169    {
170      Synchronisable* synchronisable_creator = Synchronisable::getSynchronisable(header.getCreatorID());
171      if (!synchronisable_creator)
172      {
173        mem += header.getDataSize(); //.TODO: this suckz.... remove size from header
174        assert(0); // TODO: uncomment this if we have a clean objecthierarchy (with destruction of children of objects) ^^
175        return 0;
176      }
177      else
178        creator = dynamic_cast<BaseObject*>(synchronisable_creator);
179    }
180    assert(getSynchronisable(header.getObjectID())==0);   //make sure no object with this id exists
181    BaseObject *bo = id->fabricate(creator);
182    assert(bo);
183    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
184    assert(no);
185    no->objectID=header.getObjectID();
186    no->creatorID=header.getCreatorID(); //TODO: remove this
187    no->classID=header.getClassID();
188    COUT(4) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
189          // update data and create object/entity...
190    bool b = no->updateData(mem, mode, true);
191    assert(b);
192    if (b)
193    {
194//        b = no->create();
195        assert(b);
196    }
197    return no;
198  }
199
200
201  /**
202   * Finds and deletes the Synchronisable with the appropriate objectID
203   * @param objectID objectID of the Synchronisable
204   * @return true/false
205   */
206  bool Synchronisable::deleteObject(uint32_t objectID){
207//     assert(getSynchronisable(objectID));
208    if(!getSynchronisable(objectID))
209      return false;
210    assert(getSynchronisable(objectID)->objectID==objectID);
211//     delete objectMap_[objectID];
212    Synchronisable *s = getSynchronisable(objectID);
213    if(s)
214      delete s;
215    else
216      return false;
217    return true;
218  }
219
220  /**
221   * This function looks up the objectID in the objectMap_ and returns a pointer to the right Synchronisable
222   * @param objectID objectID of the Synchronisable
223   * @return pointer to the Synchronisable with the objectID
224   */
225  Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID){
226    std::map<uint32_t, Synchronisable*>::iterator it1;
227    it1 = objectMap_.find(objectID);
228    if (it1 != objectMap_.end())
229      return it1->second;
230
231    ObjectList<Synchronisable>::iterator it;
232    for(it = ObjectList<Synchronisable>::begin(); it; ++it){
233      if( it->getObjectID()==objectID ){
234        objectMap_[objectID] = *it;
235        return *it;
236      }
237    }
238    return NULL;
239  }
240
241
242  /**
243   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory
244   * takes a pointer to already allocated memory (must have at least getSize bytes length)
245   * structure of the bitstream:
246   * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...|
247   * length of varx: size saved int syncvarlist
248   * @param mem pointer to allocated memory with enough size
249   * @param id gamestateid of the gamestate to be saved (important for priorities)
250   * @param mode defines the direction in which the data will be send/received
251   *             0x1: server->client
252   *             0x2: client->server (not recommended)
253   *             0x3: bidirectional
254   * @return true: if !doSync or if everything was successfully saved
255   */
256  bool Synchronisable::getData(uint8_t*& mem, int32_t id, uint8_t mode){
257    if(mode==0x0)
258      mode=state_;
259    //if this tick is we dont synchronise, then abort now
260    if(!doSync(id, mode))
261      return true;
262    //std::cout << "inside getData" << std::endl;
263    uint32_t tempsize = 0;
264    if (this->classID==0)
265      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
266
267    if (this->classID == static_cast<uint32_t>(-1))
268        this->classID = this->getIdentifier()->getNetworkID();
269
270    assert(this->classID==this->getIdentifier()->getNetworkID());
271//     this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this
272    std::list<SynchronisableVariableBase*>::iterator i;
273    uint32_t size;
274    size=getSize(id, mode);
275
276    // start copy header
277    SynchronisableHeader header(mem);
278    header.setDataSize( size );
279    header.setObjectID( this->objectID );
280    header.setCreatorID( this->creatorID );
281    header.setClassID( this->classID );
282    header.setDataAvailable( true );
283    tempsize += SynchronisableHeader::getSize();
284    mem += SynchronisableHeader::getSize();
285    // end copy header
286
287
288    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl;
289    // copy to location
290    for(i=syncList.begin(); i!=syncList.end(); ++i){
291      (*i)->getData( mem, mode );
292      tempsize += (*i)->getSize( mode );
293    }
294    assert(tempsize==size);
295    return true;
296  }
297
298
299  /**
300   * This function takes a bytestream and loads the data into the registered variables
301   * @param mem pointer to the bytestream
302   * @param mode same as in getData
303   * @return true/false
304   */
305  bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){
306    if(mode==0x0)
307      mode=state_;
308    std::list<SynchronisableVariableBase *>::iterator i;
309    //assert(objectMode_!=0x0);
310    //assert( (mode ^ objectMode_) != 0);
311    if(syncList.empty()){
312      assert(0);
313      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
314      return false;
315    }
316
317    uint8_t* data=mem;
318    // start extract header
319    SynchronisableHeader syncHeader(mem);
320    assert(syncHeader.getObjectID()==this->objectID);
321    assert(syncHeader.getCreatorID()==this->creatorID);
322    assert(syncHeader.getClassID()==this->classID);
323    if(syncHeader.isDataAvailable()==false){
324      mem += syncHeader.getDataSize();
325      return true;
326    }
327
328    mem += SynchronisableHeader::getSize();
329    // stop extract header
330
331    //COUT(5) << "Synchronisable: objectID " << syncHeader.getObjectID() << ", classID " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << std::endl;
332    for(i=syncList.begin(); i!=syncList.end(); i++)
333    {
334      assert( mem <= data+syncHeader.getDataSize() ); // always make sure we don't exceed the datasize in our stream
335      (*i)->putData( mem, mode, forceCallback );
336    }
337    assert(mem == data+syncHeader.getDataSize());
338    return true;
339  }
340
341  /**
342  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
343  * @param id id of the gamestate
344  * @param mode same as getData
345  * @return amount of bytes
346  */
347  uint32_t Synchronisable::getSize(int32_t id, uint8_t mode){
348    int tsize=SynchronisableHeader::getSize();
349    if(mode==0x0)
350      mode=state_;
351    if(!doSync(id, mode))
352      return 0;
353    std::list<SynchronisableVariableBase*>::iterator i;
354    for(i=syncList.begin(); i!=syncList.end(); i++){
355      tsize += (*i)->getSize( mode );
356    }
357    return tsize;
358  }
359
360  /**
361   * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction)
362   * @param id gamestate id
363   * @return true/false
364   */
365  bool Synchronisable::doSync(int32_t id, uint8_t mode){
366    if(mode==0x0)
367      mode=state_;
368    return ( (objectMode_&mode)!=0 && (!syncList.empty() ) );
369  }
370
371  /**
372   * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones
373   * @param mem pointer to the bytestream
374   */
375  bool Synchronisable::isMyData(uint8_t* mem)
376  {
377    SynchronisableHeader header(mem);
378    assert(header.getObjectID()==this->objectID);
379    return header.isDataAvailable();
380  }
381
382  /**
383   * This function sets the synchronisation mode of the object
384   * If set to 0x0 variables will not be synchronised at all
385   * If set to 0x1 variables will only be synchronised to the client
386   * If set to 0x2 variables will only be synchronised to the server
387   * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar)
388   * @param mode same as in registerVar
389   */
390  void Synchronisable::setObjectMode(uint8_t mode){
391    assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3);
392    objectMode_=mode;
393  }
394
395
396}
Note: See TracBrowser for help on using the repository browser.