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