[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 | |
---|
[3214] | 33 | #include <cstdlib> |
---|
[1505] | 34 | #include "core/CoreIncludes.h" |
---|
[5742] | 35 | #include "core/GameMode.h" |
---|
[1735] | 36 | #include "core/BaseObject.h" |
---|
[3214] | 37 | #include "network/Host.h" |
---|
[1505] | 38 | |
---|
[2171] | 39 | namespace orxonox |
---|
[1505] | 40 | { |
---|
[1639] | 41 | |
---|
[2309] | 42 | std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_; |
---|
| 43 | std::queue<uint32_t> Synchronisable::deletedObjects_; |
---|
[1639] | 44 | |
---|
[2171] | 45 | uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client |
---|
[1639] | 46 | |
---|
[1505] | 47 | /** |
---|
| 48 | * Constructor: |
---|
[5929] | 49 | * Initializes all Variables and sets the right objectID_ |
---|
[1505] | 50 | */ |
---|
[9629] | 51 | Synchronisable::Synchronisable(Context* context) |
---|
[7163] | 52 | { |
---|
[1505] | 53 | RegisterRootObject(Synchronisable); |
---|
[1907] | 54 | static uint32_t idCounter=0; |
---|
| 55 | objectMode_=0x1; // by default do not send data to server |
---|
[7163] | 56 | if ( GameMode::isMaster()/* || ( Host::running() && Host::isServer() )*/ ) |
---|
[2171] | 57 | { |
---|
[5929] | 58 | this->setObjectID( idCounter++ ); |
---|
[2171] | 59 | } |
---|
| 60 | else |
---|
[5743] | 61 | { |
---|
[5929] | 62 | objectID_=OBJECTID_UNKNOWN; |
---|
[5743] | 63 | } |
---|
[5929] | 64 | classID_ = static_cast<uint32_t>(-1); |
---|
[6417] | 65 | |
---|
[3084] | 66 | // set dataSize to 0 |
---|
| 67 | this->dataSize_ = 0; |
---|
[2415] | 68 | // set standard priority |
---|
[3280] | 69 | this->setPriority( Priority::Normal ); |
---|
[2171] | 70 | |
---|
[9629] | 71 | // get context id |
---|
| 72 | this->contextID_ = this->findContextID(context); |
---|
[1505] | 73 | } |
---|
| 74 | |
---|
[1907] | 75 | /** |
---|
[2087] | 76 | * Destructor: |
---|
[5929] | 77 | * Delete all callback objects and remove objectID_ from the objectMap_ |
---|
[1907] | 78 | */ |
---|
[7163] | 79 | Synchronisable::~Synchronisable() |
---|
| 80 | { |
---|
[1534] | 81 | // delete callback function objects |
---|
[9564] | 82 | if(!IdentifierManager::isCreatingHierarchy()){ |
---|
[3198] | 83 | // remove object from the static objectMap |
---|
| 84 | if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer())) |
---|
[5929] | 85 | deletedObjects_.push(objectID_); |
---|
[1907] | 86 | } |
---|
[7163] | 87 | // delete all Synchronisable Variables from syncList_ ( which are also in stringList_ ) |
---|
| 88 | for(std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin(); it!=syncList_.end(); it++) |
---|
[3304] | 89 | delete (*it); |
---|
[7163] | 90 | syncList_.clear(); |
---|
| 91 | stringList_.clear(); |
---|
[7183] | 92 | std::map<uint32_t, Synchronisable*>::iterator it2; |
---|
| 93 | it2 = objectMap_.find(objectID_); |
---|
| 94 | if (it2 != objectMap_.end()) |
---|
| 95 | objectMap_.erase(it2); |
---|
[2485] | 96 | |
---|
[1505] | 97 | } |
---|
[1639] | 98 | |
---|
[9629] | 99 | /** |
---|
| 100 | * @brief Returns the id of the context. |
---|
| 101 | * If the context is not Synchronisable, it moves on to its parent, recursively. |
---|
| 102 | */ |
---|
| 103 | uint32_t Synchronisable::findContextID(Context* context) |
---|
| 104 | { |
---|
| 105 | if (context == NULL) |
---|
| 106 | return OBJECTID_UNKNOWN; |
---|
[2087] | 107 | |
---|
[9629] | 108 | Synchronisable* synchronisableContext = orxonox_cast<Synchronisable*>(context); |
---|
| 109 | if (synchronisableContext != NULL) |
---|
| 110 | return synchronisableContext->getObjectID(); |
---|
| 111 | else |
---|
| 112 | return this->findContextID(context->getParentContext()); |
---|
| 113 | } |
---|
| 114 | |
---|
[1907] | 115 | /** |
---|
| 116 | * This function sets the internal mode for synchronisation |
---|
| 117 | * @param b true if this object is located on a client or on a server |
---|
| 118 | */ |
---|
[7163] | 119 | void Synchronisable::setClient(bool b) |
---|
| 120 | { |
---|
[1505] | 121 | if(b) // client |
---|
| 122 | state_=0x2; |
---|
| 123 | else // server |
---|
| 124 | state_=0x1; |
---|
| 125 | } |
---|
[1856] | 126 | |
---|
[1907] | 127 | /** |
---|
| 128 | * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create |
---|
| 129 | * After calling this function the mem pointer will be increased by the size of the needed data |
---|
| 130 | * @param mem pointer to where the appropriate data is located |
---|
| 131 | * @param mode defines the mode, how the data should be loaded |
---|
| 132 | * @return pointer to the newly created synchronisable |
---|
| 133 | */ |
---|
[2171] | 134 | Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode) |
---|
[1735] | 135 | { |
---|
[2662] | 136 | SynchronisableHeader header(mem); |
---|
[7801] | 137 | if( header.isDiffed() ) |
---|
| 138 | { |
---|
| 139 | mem += header.getDataSize() + header.getSize(); |
---|
| 140 | return 0; |
---|
| 141 | } |
---|
| 142 | // assert( !header.isDiffed() ); |
---|
[1856] | 143 | |
---|
[8858] | 144 | orxout(verbose, context::network) << "fabricating object with id: " << header.getObjectID() << endl; |
---|
[1856] | 145 | |
---|
[2662] | 146 | Identifier* id = ClassByID(header.getClassID()); |
---|
[2485] | 147 | if (!id) |
---|
| 148 | { |
---|
[3088] | 149 | for(int i = 0; i<160; i++) |
---|
[8858] | 150 | orxout(user_error, context::network) << "classid: " << i << " identifier: " << ClassByID(i) << endl; |
---|
| 151 | orxout(user_error, context::network) << "Assertion failed: id" << endl; |
---|
| 152 | orxout(user_error, context::network) << "Possible reason for this error: Client received a synchronizable object whose class has no factory." << endl; |
---|
[2485] | 153 | abort(); |
---|
| 154 | } |
---|
[1907] | 155 | assert(id); |
---|
[9629] | 156 | Context* context = 0; |
---|
| 157 | if (header.getContextID() != OBJECTID_UNKNOWN) |
---|
[2087] | 158 | { |
---|
[9629] | 159 | Synchronisable* synchronisable_context = Synchronisable::getSynchronisable(header.getContextID()); |
---|
| 160 | if (!synchronisable_context) |
---|
[2087] | 161 | { |
---|
[7163] | 162 | mem += header.getDataSize()+SynchronisableHeader::getSize(); //.TODO: this suckz.... remove size from header |
---|
[2662] | 163 | assert(0); // TODO: uncomment this if we have a clean objecthierarchy (with destruction of children of objects) ^^ |
---|
[2087] | 164 | return 0; |
---|
| 165 | } |
---|
| 166 | else |
---|
[9629] | 167 | context = orxonox_cast<Context*>(synchronisable_context); |
---|
[2087] | 168 | } |
---|
[9631] | 169 | else |
---|
| 170 | context = Context::getRootContext(); |
---|
| 171 | |
---|
[2662] | 172 | assert(getSynchronisable(header.getObjectID())==0); //make sure no object with this id exists |
---|
[9629] | 173 | BaseObject *bo = orxonox_cast<BaseObject*>(id->fabricate(context)); |
---|
[2087] | 174 | assert(bo); |
---|
[3325] | 175 | Synchronisable *no = orxonox_cast<Synchronisable*>(bo); |
---|
[1735] | 176 | assert(no); |
---|
[5929] | 177 | assert( Synchronisable::objectMap_.find(header.getObjectID()) == Synchronisable::objectMap_.end() ); |
---|
| 178 | no->setObjectID(header.getObjectID()); |
---|
[9629] | 179 | //no->contextID=header.getContextID(); //TODO: remove this |
---|
[5929] | 180 | no->setClassID(header.getClassID()); |
---|
[9629] | 181 | assert(no->contextID_ == header.getContextID()); |
---|
| 182 | if( context ) |
---|
| 183 | { |
---|
| 184 | BaseObject* boContext = orxonox_cast<BaseObject*>(context); |
---|
| 185 | if (boContext) |
---|
| 186 | bo->setLevel(boContext->getLevel()); // Note: this ensures that the level is known on the client for child objects of the scene (and the scene itself) |
---|
| 187 | } |
---|
[5929] | 188 | //assert(no->classID_ == header.getClassID()); |
---|
[8858] | 189 | orxout(verbose, context::network) << "fabricate objectID_: " << no->objectID_ << " classID_: " << no->classID_ << endl; |
---|
[1735] | 190 | // update data and create object/entity... |
---|
[2087] | 191 | bool b = no->updateData(mem, mode, true); |
---|
[1907] | 192 | assert(b); |
---|
[2087] | 193 | if (b) |
---|
| 194 | { |
---|
[2245] | 195 | // b = no->create(); |
---|
[2087] | 196 | assert(b); |
---|
| 197 | } |
---|
[1907] | 198 | return no; |
---|
| 199 | } |
---|
| 200 | |
---|
[2087] | 201 | |
---|
[1907] | 202 | /** |
---|
[5929] | 203 | * Finds and deletes the Synchronisable with the appropriate objectID_ |
---|
| 204 | * @param objectID_ objectID_ of the Synchronisable |
---|
[1907] | 205 | * @return true/false |
---|
| 206 | */ |
---|
[7163] | 207 | bool Synchronisable::deleteObject(uint32_t objectID_) |
---|
| 208 | { |
---|
[5929] | 209 | if(!getSynchronisable(objectID_)) |
---|
[1735] | 210 | return false; |
---|
[5929] | 211 | assert(getSynchronisable(objectID_)->objectID_==objectID_); |
---|
| 212 | Synchronisable *s = getSynchronisable(objectID_); |
---|
[1907] | 213 | if(s) |
---|
[5929] | 214 | s->destroy(); // or delete? |
---|
[1907] | 215 | else |
---|
[1735] | 216 | return false; |
---|
| 217 | return true; |
---|
| 218 | } |
---|
[2087] | 219 | |
---|
[1907] | 220 | /** |
---|
[5929] | 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_ |
---|
[1907] | 224 | */ |
---|
[7163] | 225 | Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID_) |
---|
| 226 | { |
---|
[2309] | 227 | std::map<uint32_t, Synchronisable*>::iterator it1; |
---|
[5929] | 228 | it1 = objectMap_.find(objectID_); |
---|
[2171] | 229 | if (it1 != objectMap_.end()) |
---|
| 230 | return it1->second; |
---|
[3198] | 231 | // if the objects not in the map it should'nt exist at all anymore |
---|
[1907] | 232 | return NULL; |
---|
| 233 | } |
---|
| 234 | |
---|
[2087] | 235 | |
---|
[1505] | 236 | /** |
---|
[5929] | 237 | * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID_ and classID_ to the given memory |
---|
[1735] | 238 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
[1751] | 239 | * structure of the bitstream: |
---|
[5929] | 240 | * |totalsize,objectID_,classID_,var1,var2,string1_length,string1,var3,...| |
---|
[1907] | 241 | * length of varx: size saved int syncvarlist |
---|
| 242 | * @param mem pointer to allocated memory with enough size |
---|
[7801] | 243 | * @param sizes vector containing sizes of all objects in gamestate (to be appended) |
---|
[1907] | 244 | * @param id gamestateid of the gamestate to be saved (important for priorities) |
---|
| 245 | * @param mode defines the direction in which the data will be send/received |
---|
| 246 | * 0x1: server->client |
---|
[7801] | 247 | * 0x2: client->server |
---|
[1907] | 248 | * 0x3: bidirectional |
---|
[2087] | 249 | * @return true: if !doSync or if everything was successfully saved |
---|
[1735] | 250 | */ |
---|
[7163] | 251 | uint32_t Synchronisable::getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode) |
---|
| 252 | { |
---|
| 253 | unsigned int test = 0; |
---|
[2171] | 254 | if(mode==0x0) |
---|
| 255 | mode=state_; |
---|
[1907] | 256 | //if this tick is we dont synchronise, then abort now |
---|
[8329] | 257 | if(!doSync(/*id,*/ mode)) |
---|
[3084] | 258 | return 0; |
---|
[2309] | 259 | uint32_t tempsize = 0; |
---|
[3304] | 260 | #ifndef NDEBUG |
---|
[7163] | 261 | uint8_t* oldmem = mem; |
---|
[5929] | 262 | if (this->classID_==0) |
---|
[8858] | 263 | orxout(internal_info, context::network) << "classid 0 " << this->getIdentifier()->getName() << endl; |
---|
[3304] | 264 | #endif |
---|
[2087] | 265 | |
---|
[5929] | 266 | if (this->classID_ == static_cast<uint32_t>(-1)) |
---|
| 267 | this->classID_ = this->getIdentifier()->getNetworkID(); |
---|
[2087] | 268 | |
---|
[5929] | 269 | assert(ClassByID(this->classID_)); |
---|
| 270 | assert(this->classID_==this->getIdentifier()->getNetworkID()); |
---|
| 271 | assert(this->objectID_!=OBJECTID_UNKNOWN); |
---|
[3084] | 272 | std::vector<SynchronisableVariableBase*>::iterator i; |
---|
[1856] | 273 | |
---|
[1735] | 274 | // start copy header |
---|
[2662] | 275 | SynchronisableHeader header(mem); |
---|
| 276 | mem += SynchronisableHeader::getSize(); |
---|
[1735] | 277 | // end copy header |
---|
[1856] | 278 | |
---|
[8858] | 279 | orxout(verbose_more, context::network) << "getting data from objectID_: " << objectID_ << ", classID_: " << classID_ << endl; |
---|
| 280 | // orxout(verbose, context::network) << "objectid: " << this->objectID_ << ":"; |
---|
[1735] | 281 | // copy to location |
---|
[7163] | 282 | for(i=syncList_.begin(); i!=syncList_.end(); ++i) |
---|
| 283 | { |
---|
| 284 | uint32_t varsize = (*i)->getData( mem, mode ); |
---|
[8858] | 285 | // orxout(verbose, context::network) << " " << varsize; |
---|
[7163] | 286 | tempsize += varsize; |
---|
| 287 | sizes.push_back(varsize); |
---|
| 288 | ++test; |
---|
[3084] | 289 | //tempsize += (*i)->getSize( mode ); |
---|
[1735] | 290 | } |
---|
[7801] | 291 | assert(tempsize!=0); // if this happens an empty object (with no variables) would be transmitted |
---|
[8858] | 292 | // orxout(verbose, context::network) << endl; |
---|
[6417] | 293 | |
---|
[5929] | 294 | header.setObjectID( this->objectID_ ); |
---|
[9629] | 295 | header.setContextID( this->contextID_ ); |
---|
[5929] | 296 | header.setClassID( this->classID_ ); |
---|
[3084] | 297 | header.setDataSize( tempsize ); |
---|
[7163] | 298 | assert( tempsize == mem-oldmem-SynchronisableHeader::getSize() ); |
---|
| 299 | assert( test == this->getNrOfVariables() ); |
---|
| 300 | header.setDiffed(false); |
---|
| 301 | tempsize += SynchronisableHeader::getSize(); |
---|
[6417] | 302 | |
---|
[3084] | 303 | #ifndef NDEBUG |
---|
| 304 | uint32_t size; |
---|
| 305 | size=getSize(id, mode); |
---|
[1735] | 306 | assert(tempsize==size); |
---|
[3084] | 307 | #endif |
---|
| 308 | return tempsize; |
---|
[1735] | 309 | } |
---|
[1505] | 310 | |
---|
[1856] | 311 | |
---|
[1505] | 312 | /** |
---|
[1907] | 313 | * This function takes a bytestream and loads the data into the registered variables |
---|
| 314 | * @param mem pointer to the bytestream |
---|
| 315 | * @param mode same as in getData |
---|
[8327] | 316 | * @param forceCallback this makes updateData call each callback |
---|
[1735] | 317 | * @return true/false |
---|
| 318 | */ |
---|
[7163] | 319 | bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback) |
---|
| 320 | { |
---|
[1735] | 321 | if(mode==0x0) |
---|
| 322 | mode=state_; |
---|
[8329] | 323 | |
---|
[7163] | 324 | if(syncList_.empty()) |
---|
| 325 | { |
---|
[8858] | 326 | orxout(internal_warning, context::network) << "Synchronisable::updateData syncList_ is empty" << endl; |
---|
[2419] | 327 | assert(0); |
---|
[1735] | 328 | return false; |
---|
| 329 | } |
---|
[1856] | 330 | |
---|
[2245] | 331 | uint8_t* data=mem; |
---|
[1735] | 332 | // start extract header |
---|
[7163] | 333 | SynchronisableHeaderLight syncHeaderLight(mem); |
---|
| 334 | assert(syncHeaderLight.getObjectID()==this->getObjectID()); |
---|
[8329] | 335 | |
---|
| 336 | if( !this->doReceive(mode) ) |
---|
| 337 | { |
---|
| 338 | uint32_t headerSize; |
---|
| 339 | if( syncHeaderLight.isDiffed() ) |
---|
| 340 | headerSize = SynchronisableHeaderLight::getSize(); |
---|
| 341 | else |
---|
| 342 | headerSize = SynchronisableHeader::getSize(); |
---|
| 343 | mem += syncHeaderLight.getDataSize() + headerSize; |
---|
| 344 | return true; |
---|
| 345 | } |
---|
[1856] | 346 | |
---|
[8858] | 347 | //orxout(verbose_more, context::network) << "Synchronisable: objectID_ " << syncHeader.getObjectID() << ", classID_ " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << endl; |
---|
[7163] | 348 | if( !syncHeaderLight.isDiffed() ) |
---|
[2245] | 349 | { |
---|
[7163] | 350 | SynchronisableHeader syncHeader2(mem); |
---|
| 351 | assert( this->getClassID() == syncHeader2.getClassID() ); |
---|
[9629] | 352 | assert( this->getContextID() == syncHeader2.getContextID() ); |
---|
[7163] | 353 | mem += SynchronisableHeader::getSize(); |
---|
| 354 | std::vector<SynchronisableVariableBase *>::iterator i; |
---|
[7801] | 355 | for(i=syncList_.begin(); i!=syncList_.end(); ++i) |
---|
[7163] | 356 | { |
---|
| 357 | assert( mem <= data+syncHeader2.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream |
---|
| 358 | (*i)->putData( mem, mode, forceCallback ); |
---|
| 359 | } |
---|
| 360 | assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeader::getSize() ); |
---|
[1735] | 361 | } |
---|
[7163] | 362 | else |
---|
| 363 | { |
---|
| 364 | mem += SynchronisableHeaderLight::getSize(); |
---|
[8858] | 365 | // orxout(debug_output, context::network) << "objectID: " << this->objectID_ << endl; |
---|
[7163] | 366 | while( mem < data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() ) |
---|
| 367 | { |
---|
| 368 | VariableID varID = *(VariableID*)mem; |
---|
[8858] | 369 | // orxout(debug_output, context::network) << "varID: " << varID << endl; |
---|
[7163] | 370 | assert( varID < syncList_.size() ); |
---|
| 371 | mem += sizeof(VariableID); |
---|
| 372 | syncList_[varID]->putData( mem, mode, forceCallback ); |
---|
| 373 | } |
---|
| 374 | assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() ); |
---|
| 375 | } |
---|
[1735] | 376 | return true; |
---|
| 377 | } |
---|
[1505] | 378 | |
---|
| 379 | /** |
---|
| 380 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
[1907] | 381 | * @param id id of the gamestate |
---|
| 382 | * @param mode same as getData |
---|
[1505] | 383 | * @return amount of bytes |
---|
| 384 | */ |
---|
[7163] | 385 | uint32_t Synchronisable::getSize(int32_t id, uint8_t mode) |
---|
| 386 | { |
---|
| 387 | uint32_t tsize=SynchronisableHeader::getSize(); |
---|
[3084] | 388 | if (mode==0x0) |
---|
[1505] | 389 | mode=state_; |
---|
[8329] | 390 | if (!doSync(/*id, */mode)) |
---|
[2171] | 391 | return 0; |
---|
[3084] | 392 | assert( mode==state_ ); |
---|
| 393 | tsize += this->dataSize_; |
---|
| 394 | std::vector<SynchronisableVariableBase*>::iterator i; |
---|
[7163] | 395 | for(i=stringList_.begin(); i!=stringList_.end(); ++i) |
---|
| 396 | { |
---|
[2245] | 397 | tsize += (*i)->getSize( mode ); |
---|
[1505] | 398 | } |
---|
| 399 | return tsize; |
---|
| 400 | } |
---|
[1639] | 401 | |
---|
[1735] | 402 | /** |
---|
[1907] | 403 | * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction) |
---|
[8327] | 404 | * @param mode Synchronisation mode (toclient, toserver or bidirectional) |
---|
[1907] | 405 | * @return true/false |
---|
[1735] | 406 | */ |
---|
[8329] | 407 | bool Synchronisable::doSync(/*int32_t id,*/ uint8_t mode) |
---|
[7163] | 408 | { |
---|
[8327] | 409 | // if(mode==0x0) |
---|
| 410 | // mode=state_; |
---|
| 411 | assert(mode!=0x0); |
---|
[7163] | 412 | return ( (this->objectMode_ & mode)!=0 && (!syncList_.empty() ) ); |
---|
[1735] | 413 | } |
---|
[8329] | 414 | |
---|
| 415 | /** |
---|
| 416 | * This function determines, wheter the object should accept data from the bytestream (according to its syncmode/direction) |
---|
| 417 | * @param mode Synchronisation mode (toclient, toserver or bidirectional) |
---|
| 418 | * @return true/false |
---|
| 419 | */ |
---|
| 420 | bool Synchronisable::doReceive( uint8_t mode) |
---|
| 421 | { |
---|
| 422 | //return mode != this->objectMode_ || this->objectMode_ == ObjectDirection::Bidirectional; |
---|
| 423 | return true; |
---|
| 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 | */ |
---|
[7163] | 434 | void Synchronisable::setSyncMode(uint8_t mode) |
---|
| 435 | { |
---|
[2087] | 436 | assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3); |
---|
[5929] | 437 | this->objectMode_=mode; |
---|
[1505] | 438 | } |
---|
| 439 | |
---|
[6417] | 440 | template <> void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional) |
---|
| 441 | { |
---|
| 442 | SynchronisableVariableBase* sv; |
---|
| 443 | if (bidirectional) |
---|
| 444 | sv = new SynchronisableVariableBidirectional<std::string>(variable, mode, cb); |
---|
| 445 | else |
---|
| 446 | sv = new SynchronisableVariable<std::string>(variable, mode, cb); |
---|
[7163] | 447 | syncList_.push_back(sv); |
---|
| 448 | stringList_.push_back(sv); |
---|
[6417] | 449 | } |
---|
[2485] | 450 | |
---|
[7163] | 451 | template <> void Synchronisable::unregisterVariable( std::string& variable ) |
---|
| 452 | { |
---|
| 453 | bool unregistered_nonexistent_variable = true; |
---|
| 454 | std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin(); |
---|
| 455 | while(it!=syncList_.end()) |
---|
| 456 | { |
---|
| 457 | if( ((*it)->getReference()) == &variable ) |
---|
| 458 | { |
---|
| 459 | delete (*it); |
---|
| 460 | syncList_.erase(it); |
---|
| 461 | unregistered_nonexistent_variable = false; |
---|
| 462 | break; |
---|
| 463 | } |
---|
| 464 | else |
---|
| 465 | ++it; |
---|
| 466 | } |
---|
| 467 | assert(unregistered_nonexistent_variable == false); |
---|
| 468 | |
---|
| 469 | it = stringList_.begin(); |
---|
| 470 | while(it!=stringList_.end()) |
---|
| 471 | { |
---|
| 472 | if( ((*it)->getReference()) == &variable ) |
---|
| 473 | { |
---|
| 474 | delete (*it); |
---|
| 475 | stringList_.erase(it); |
---|
| 476 | return; |
---|
| 477 | } |
---|
| 478 | else |
---|
| 479 | ++it; |
---|
| 480 | } |
---|
| 481 | unregistered_nonexistent_variable = true; |
---|
| 482 | assert(unregistered_nonexistent_variable == false); //if we reach this point something went wrong: |
---|
| 483 | // the variable has not been registered before |
---|
| 484 | } |
---|
[6417] | 485 | |
---|
[7163] | 486 | |
---|
[1505] | 487 | } |
---|