[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 | |
---|
[2171] | 52 | #include "Host.h" |
---|
| 53 | namespace 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; |
---|
[1505] | 80 | syncList = new std::list<synchronisableVariable *>; |
---|
[2087] | 81 | |
---|
[2171] | 82 | |
---|
| 83 | #ifndef NDEBUG |
---|
| 84 | ObjectList<Synchronisable>::iterator it; |
---|
| 85 | for(it = ObjectList<Synchronisable>::begin(); it!=ObjectList<Synchronisable>::end(); ++it){ |
---|
| 86 | if( it->getObjectID()==this->objectID ) |
---|
| 87 | assert(*it==this || (it->objectID==OBJECTID_UNKNOWN && it->objectMode_==0x0)); |
---|
| 88 | } |
---|
| 89 | #endif |
---|
| 90 | |
---|
[2087] | 91 | this->creatorID = OBJECTID_UNKNOWN; |
---|
| 92 | |
---|
| 93 | searchcreatorID: |
---|
| 94 | if (creator) |
---|
| 95 | { |
---|
| 96 | Synchronisable* synchronisable_creator = dynamic_cast<Synchronisable*>(creator); |
---|
| 97 | if (synchronisable_creator && synchronisable_creator->objectMode_) |
---|
| 98 | { |
---|
| 99 | this->creatorID = synchronisable_creator->getObjectID(); |
---|
| 100 | } |
---|
| 101 | else if (creator != creator->getCreator()) |
---|
| 102 | { |
---|
| 103 | creator = creator->getCreator(); |
---|
| 104 | goto searchcreatorID; |
---|
| 105 | } |
---|
| 106 | } |
---|
[1505] | 107 | } |
---|
| 108 | |
---|
[1907] | 109 | /** |
---|
[2087] | 110 | * Destructor: |
---|
[1907] | 111 | * Delete all callback objects and remove objectID from the objectMap_ |
---|
| 112 | */ |
---|
[1505] | 113 | Synchronisable::~Synchronisable(){ |
---|
[1534] | 114 | // delete callback function objects |
---|
[2171] | 115 | if(!Identifier::isCreatingHierarchy()){ |
---|
[1534] | 116 | for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++) |
---|
| 117 | delete (*it)->callback; |
---|
[2171] | 118 | if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer())) |
---|
[2087] | 119 | deletedObjects_.push(objectID); |
---|
[1907] | 120 | // COUT(3) << "destruct synchronisable +++" << objectID << " | " << classID << std::endl; |
---|
| 121 | // COUT(3) << " bump ---" << objectID << " | " << &objectMap_ << std::endl; |
---|
| 122 | // assert(objectMap_[objectID]->objectID==objectID); |
---|
| 123 | // objectMap_.erase(objectID); |
---|
| 124 | } |
---|
[2171] | 125 | std::map<unsigned int, Synchronisable*>::iterator it; |
---|
| 126 | it = objectMap_.find(objectID); |
---|
| 127 | if (it != objectMap_.end()) |
---|
| 128 | objectMap_.erase(it); |
---|
[1505] | 129 | } |
---|
[1639] | 130 | |
---|
[1907] | 131 | /** |
---|
| 132 | * This function gets called after all neccessary data has been passed to the object |
---|
| 133 | * Overload this function and recall the create function of the parent class |
---|
| 134 | * @return true/false |
---|
| 135 | */ |
---|
[1505] | 136 | bool Synchronisable::create(){ |
---|
| 137 | this->classID = this->getIdentifier()->getNetworkID(); |
---|
[1907] | 138 | // COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl; |
---|
[2087] | 139 | |
---|
[1907] | 140 | // COUT(3) << "construct synchronisable +++" << objectID << " | " << classID << std::endl; |
---|
| 141 | // objectMap_[objectID]=this; |
---|
| 142 | // assert(objectMap_[objectID]==this); |
---|
| 143 | // assert(objectMap_[objectID]->objectID==objectID); |
---|
[1505] | 144 | return true; |
---|
| 145 | } |
---|
[1639] | 146 | |
---|
[1856] | 147 | |
---|
[1907] | 148 | /** |
---|
| 149 | * This function sets the internal mode for synchronisation |
---|
| 150 | * @param b true if this object is located on a client or on a server |
---|
| 151 | */ |
---|
[1505] | 152 | void Synchronisable::setClient(bool b){ |
---|
| 153 | if(b) // client |
---|
| 154 | state_=0x2; |
---|
| 155 | else // server |
---|
| 156 | state_=0x1; |
---|
| 157 | } |
---|
[1856] | 158 | |
---|
[1907] | 159 | /** |
---|
| 160 | * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create |
---|
| 161 | * After calling this function the mem pointer will be increased by the size of the needed data |
---|
| 162 | * @param mem pointer to where the appropriate data is located |
---|
| 163 | * @param mode defines the mode, how the data should be loaded |
---|
| 164 | * @return pointer to the newly created synchronisable |
---|
| 165 | */ |
---|
[2171] | 166 | Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode) |
---|
[1735] | 167 | { |
---|
[1907] | 168 | synchronisableHeader *header = (synchronisableHeader *)mem; |
---|
[1856] | 169 | |
---|
[2087] | 170 | if(!header->dataAvailable) |
---|
| 171 | { |
---|
| 172 | mem += header->size; |
---|
| 173 | return 0; |
---|
| 174 | } |
---|
[2171] | 175 | |
---|
[2087] | 176 | COUT(4) << "fabricating object with id: " << header->objectID << std::endl; |
---|
[1856] | 177 | |
---|
[2171] | 178 | Identifier* id = ClassByID(header->classID); |
---|
[1907] | 179 | assert(id); |
---|
[2171] | 180 | BaseObject* creator = 0; |
---|
[2087] | 181 | if (header->creatorID != OBJECTID_UNKNOWN) |
---|
| 182 | { |
---|
| 183 | Synchronisable* synchronisable_creator = Synchronisable::getSynchronisable(header->creatorID); |
---|
| 184 | if (!synchronisable_creator) |
---|
| 185 | { |
---|
| 186 | mem += header->size; //.TODO: this suckz.... remove size from header |
---|
| 187 | return 0; |
---|
| 188 | } |
---|
| 189 | else |
---|
[2171] | 190 | creator = dynamic_cast<BaseObject*>(synchronisable_creator); |
---|
[2087] | 191 | } |
---|
[2171] | 192 | assert(getSynchronisable(header->objectID)==0); //make sure no object with this id exists |
---|
| 193 | BaseObject *bo = id->fabricate(creator); |
---|
[2087] | 194 | assert(bo); |
---|
[1735] | 195 | Synchronisable *no = dynamic_cast<Synchronisable *>(bo); |
---|
| 196 | assert(no); |
---|
[1907] | 197 | no->objectID=header->objectID; |
---|
[2087] | 198 | no->creatorID=header->creatorID; //TODO: remove this |
---|
[1907] | 199 | no->classID=header->classID; |
---|
[2087] | 200 | COUT(4) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl; |
---|
[1735] | 201 | // update data and create object/entity... |
---|
[2087] | 202 | bool b = no->updateData(mem, mode, true); |
---|
[1907] | 203 | assert(b); |
---|
[2087] | 204 | if (b) |
---|
| 205 | { |
---|
| 206 | b = no->create(); |
---|
| 207 | assert(b); |
---|
| 208 | } |
---|
[1907] | 209 | return no; |
---|
| 210 | } |
---|
| 211 | |
---|
[2087] | 212 | |
---|
[1907] | 213 | /** |
---|
| 214 | * Finds and deletes the Synchronisable with the appropriate objectID |
---|
| 215 | * @param objectID objectID of the Synchronisable |
---|
| 216 | * @return true/false |
---|
| 217 | */ |
---|
| 218 | bool Synchronisable::deleteObject(unsigned int objectID){ |
---|
| 219 | // assert(getSynchronisable(objectID)); |
---|
| 220 | if(!getSynchronisable(objectID)) |
---|
[1735] | 221 | return false; |
---|
[1907] | 222 | assert(getSynchronisable(objectID)->objectID==objectID); |
---|
| 223 | // delete objectMap_[objectID]; |
---|
| 224 | Synchronisable *s = getSynchronisable(objectID); |
---|
| 225 | if(s) |
---|
| 226 | delete s; |
---|
| 227 | else |
---|
[1735] | 228 | return false; |
---|
| 229 | return true; |
---|
| 230 | } |
---|
[2087] | 231 | |
---|
[1907] | 232 | /** |
---|
| 233 | * This function looks up the objectID in the objectMap_ and returns a pointer to the right Synchronisable |
---|
| 234 | * @param objectID objectID of the Synchronisable |
---|
| 235 | * @return pointer to the Synchronisable with the objectID |
---|
| 236 | */ |
---|
| 237 | Synchronisable* Synchronisable::getSynchronisable(unsigned int objectID){ |
---|
[2171] | 238 | std::map<unsigned int, Synchronisable*>::iterator it1; |
---|
| 239 | it1 = objectMap_.find(objectID); |
---|
| 240 | if (it1 != objectMap_.end()) |
---|
| 241 | return it1->second; |
---|
| 242 | |
---|
| 243 | ObjectList<Synchronisable>::iterator it; |
---|
| 244 | for(it = ObjectList<Synchronisable>::begin(); it; ++it){ |
---|
| 245 | if( it->getObjectID()==objectID ){ |
---|
| 246 | objectMap_[objectID] = *it; |
---|
| 247 | return *it; |
---|
| 248 | } |
---|
[1907] | 249 | } |
---|
| 250 | return NULL; |
---|
| 251 | } |
---|
| 252 | |
---|
[2087] | 253 | |
---|
[1505] | 254 | /** |
---|
| 255 | * This function is used to register a variable to be synchronized |
---|
| 256 | * also counts the total datasize needed to save the variables |
---|
| 257 | * @param var pointer to the variable |
---|
| 258 | * @param size size of the datatype the variable consists of |
---|
[2171] | 259 | * @param t the type of the variable (DATA or STRING |
---|
[1907] | 260 | * @param mode same as in getData |
---|
| 261 | * @param cb callback object that should get called, if the value of the variable changes |
---|
[1505] | 262 | */ |
---|
[2171] | 263 | void Synchronisable::registerVariable(void *var, int size, variableType t, uint8_t mode, NetworkCallbackBase *cb){ |
---|
[2087] | 264 | assert( mode==direction::toclient || mode==direction::toserver || mode==direction::serverMaster || mode==direction::clientMaster); |
---|
[1505] | 265 | // create temporary synch.Var struct |
---|
| 266 | synchronisableVariable *temp = new synchronisableVariable; |
---|
| 267 | temp->size = size; |
---|
| 268 | temp->var = var; |
---|
[1639] | 269 | temp->mode = mode; |
---|
[1505] | 270 | temp->type = t; |
---|
[1534] | 271 | temp->callback = cb; |
---|
[2087] | 272 | if( ( mode & direction::bidirectional ) ) |
---|
| 273 | { |
---|
[2171] | 274 | if(t!=STRING) |
---|
| 275 | { |
---|
| 276 | temp->varBuffer = new uint8_t[size]; |
---|
| 277 | memcpy(temp->varBuffer, temp->var, size); //now fill the buffer for the first time |
---|
| 278 | } |
---|
| 279 | else |
---|
| 280 | { |
---|
| 281 | temp->varBuffer=new std::string( *static_cast<std::string*>(var) ); |
---|
| 282 | } |
---|
[2087] | 283 | temp->varReference = 0; |
---|
| 284 | } |
---|
[1639] | 285 | COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; |
---|
[1505] | 286 | //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; |
---|
| 287 | COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl; |
---|
| 288 | syncList->push_back(temp); |
---|
[1907] | 289 | #ifndef NDEBUG |
---|
| 290 | std::list<synchronisableVariable *>::iterator it = syncList->begin(); |
---|
| 291 | while(it!=syncList->end()){ |
---|
| 292 | assert(*it!=var); |
---|
| 293 | it++; |
---|
| 294 | } |
---|
| 295 | #endif |
---|
[1505] | 296 | } |
---|
[1856] | 297 | |
---|
[2171] | 298 | void Synchronisable::unregisterVariable(void *var){ |
---|
| 299 | std::list<synchronisableVariable *>::iterator it = syncList->begin(); |
---|
| 300 | while(it!=syncList->end()){ |
---|
| 301 | if( (*it)->var == var ){ |
---|
| 302 | delete *it; |
---|
| 303 | syncList->erase(it); |
---|
| 304 | return; |
---|
| 305 | } |
---|
| 306 | else |
---|
| 307 | it++; |
---|
| 308 | } |
---|
| 309 | bool unregistered_nonexistent_variable = false; |
---|
| 310 | assert(unregistered_nonexistent_variable); //if we reach this point something went wrong: |
---|
| 311 | // the variable has not been registered before |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | |
---|
[1735] | 315 | /** |
---|
[1907] | 316 | * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory |
---|
[1735] | 317 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
[1751] | 318 | * structure of the bitstream: |
---|
[1907] | 319 | * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...| |
---|
| 320 | * length of varx: size saved int syncvarlist |
---|
| 321 | * @param mem pointer to allocated memory with enough size |
---|
| 322 | * @param id gamestateid of the gamestate to be saved (important for priorities) |
---|
| 323 | * @param mode defines the direction in which the data will be send/received |
---|
| 324 | * 0x1: server->client |
---|
| 325 | * 0x2: client->server (not recommended) |
---|
| 326 | * 0x3: bidirectional |
---|
[2087] | 327 | * @return true: if !doSync or if everything was successfully saved |
---|
[1735] | 328 | */ |
---|
[2171] | 329 | bool Synchronisable::getData(uint8_t*& mem, unsigned int id, uint8_t mode){ |
---|
| 330 | if(mode==0x0) |
---|
| 331 | mode=state_; |
---|
[1907] | 332 | //if this tick is we dont synchronise, then abort now |
---|
[2171] | 333 | if(!doSync(id, mode)) |
---|
[1907] | 334 | return true; |
---|
[1735] | 335 | //std::cout << "inside getData" << std::endl; |
---|
| 336 | unsigned int tempsize = 0; |
---|
| 337 | if(classID==0) |
---|
| 338 | COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl; |
---|
[2087] | 339 | |
---|
| 340 | if (this->classID == (unsigned int)-1) |
---|
| 341 | this->classID = this->getIdentifier()->getNetworkID(); |
---|
| 342 | |
---|
[1907] | 343 | assert(this->classID==this->getIdentifier()->getNetworkID()); |
---|
| 344 | // this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this |
---|
[1735] | 345 | std::list<synchronisableVariable *>::iterator i; |
---|
| 346 | unsigned int size; |
---|
[1907] | 347 | size=getSize(id, mode); |
---|
[1856] | 348 | |
---|
[1735] | 349 | // start copy header |
---|
[1907] | 350 | synchronisableHeader *header = (synchronisableHeader *)mem; |
---|
| 351 | header->size = size; |
---|
| 352 | header->objectID = this->objectID; |
---|
[2087] | 353 | header->creatorID = this->creatorID; |
---|
[1907] | 354 | header->classID = this->classID; |
---|
| 355 | header->dataAvailable = true; |
---|
[2171] | 356 | tempsize += sizeof(synchronisableHeader); |
---|
| 357 | mem += sizeof(synchronisableHeader); |
---|
[1735] | 358 | // end copy header |
---|
[1856] | 359 | |
---|
| 360 | |
---|
[1735] | 361 | COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl; |
---|
| 362 | // copy to location |
---|
| 363 | for(i=syncList->begin(); i!=syncList->end(); ++i){ |
---|
| 364 | if( ((*i)->mode & mode) == 0 ){ |
---|
| 365 | COUT(5) << "not getting data: " << std::endl; |
---|
| 366 | continue; // this variable should only be received |
---|
| 367 | } |
---|
[2171] | 368 | |
---|
| 369 | // =========== start bidirectional stuff ============= |
---|
[2087] | 370 | // if the variable gets synchronised bidirectional, then add the reference to the bytestream |
---|
| 371 | if( ( (*i)->mode & direction::bidirectional ) == direction::bidirectional ) |
---|
| 372 | { |
---|
[2171] | 373 | if( ( ((*i)->mode == direction::serverMaster) && (mode == 0x1) ) || \ |
---|
| 374 | ( ((*i)->mode == direction::clientMaster) && (mode == 0x2) ) ) |
---|
| 375 | { |
---|
| 376 | // MASTER |
---|
| 377 | if((*i)->type==DATA){ |
---|
| 378 | if( memcmp((*i)->var,(*i)->varBuffer,(*i)->size) != 0 ) //check whether the variable changed during the last tick |
---|
| 379 | { |
---|
| 380 | ((*i)->varReference)++; //the variable changed so increase the refnr |
---|
| 381 | memcpy((*i)->varBuffer, (*i)->var, (*i)->size); //set the buffer to the new value |
---|
| 382 | } |
---|
| 383 | } |
---|
| 384 | else //STRING |
---|
| 385 | { |
---|
| 386 | if( *static_cast<std::string*>((*i)->var) != *static_cast<std::string*>((*i)->varBuffer) ) //the string changed |
---|
| 387 | { |
---|
| 388 | ((*i)->varReference)++; //the variable changed |
---|
| 389 | *static_cast<std::string*>((*i)->varBuffer) = *static_cast<std::string*>((*i)->var); //now set the buffer to the new value |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | // copy the reference number to the stream |
---|
[2087] | 394 | *(uint8_t*)mem = (*i)->varReference; |
---|
| 395 | mem += sizeof( (*i)->varReference ); |
---|
| 396 | tempsize += sizeof( (*i)->varReference ); |
---|
| 397 | } |
---|
[2171] | 398 | // ================== end bidirectional stuff |
---|
| 399 | |
---|
[1735] | 400 | switch((*i)->type){ |
---|
| 401 | case DATA: |
---|
| 402 | memcpy( (void *)(mem), (void*)((*i)->var), (*i)->size); |
---|
[2171] | 403 | mem += (*i)->size; |
---|
| 404 | tempsize += (*i)->size; |
---|
[1735] | 405 | break; |
---|
| 406 | case STRING: |
---|
[2087] | 407 | memcpy( (void *)(mem), (void *)&((*i)->size), sizeof(size_t) ); |
---|
[2171] | 408 | mem += sizeof(size_t); |
---|
[1735] | 409 | const char *data = ( ( *(std::string *) (*i)->var).c_str()); |
---|
| 410 | memcpy( mem, (void*)data, (*i)->size); |
---|
| 411 | COUT(5) << "synchronisable: char: " << (const char *)(mem) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl; |
---|
[2171] | 412 | mem += (*i)->size; |
---|
| 413 | tempsize += (*i)->size + sizeof(size_t); |
---|
[1735] | 414 | break; |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | assert(tempsize==size); |
---|
| 418 | return true; |
---|
| 419 | } |
---|
[1505] | 420 | |
---|
[1856] | 421 | |
---|
[1505] | 422 | /** |
---|
[1907] | 423 | * This function takes a bytestream and loads the data into the registered variables |
---|
| 424 | * @param mem pointer to the bytestream |
---|
| 425 | * @param mode same as in getData |
---|
[1735] | 426 | * @return true/false |
---|
| 427 | */ |
---|
[2171] | 428 | bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){ |
---|
[1735] | 429 | if(mode==0x0) |
---|
| 430 | mode=state_; |
---|
| 431 | std::list<synchronisableVariable *>::iterator i; |
---|
[2171] | 432 | //assert(objectMode_!=0x0); |
---|
| 433 | //assert( (mode ^ objectMode_) != 0); |
---|
[1735] | 434 | if(syncList->empty()){ |
---|
| 435 | COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; |
---|
| 436 | return false; |
---|
| 437 | } |
---|
[1856] | 438 | |
---|
[1907] | 439 | uint8_t *data=mem; |
---|
[1735] | 440 | // start extract header |
---|
[1907] | 441 | synchronisableHeader *syncHeader = (synchronisableHeader *)mem; |
---|
| 442 | assert(syncHeader->objectID==this->objectID); |
---|
[2171] | 443 | assert(syncHeader->creatorID==this->creatorID); |
---|
| 444 | assert(this->classID==syncHeader->classID); //TODO: fix this!!! maybe a problem with the identifier ? |
---|
[1907] | 445 | if(syncHeader->dataAvailable==false){ |
---|
[2171] | 446 | mem += syncHeader->size; |
---|
[1751] | 447 | return true; |
---|
[1907] | 448 | } |
---|
[1856] | 449 | |
---|
[2171] | 450 | mem += sizeof(synchronisableHeader); |
---|
[1907] | 451 | // stop extract header |
---|
[2087] | 452 | |
---|
[1907] | 453 | COUT(5) << "Synchronisable: objectID " << syncHeader->objectID << ", classID " << syncHeader->classID << " size: " << syncHeader->size << " synchronising data" << std::endl; |
---|
| 454 | for(i=syncList->begin(); i!=syncList->end() && mem <= data+syncHeader->size; i++){ |
---|
[1735] | 455 | if( ((*i)->mode ^ mode) == 0 ){ |
---|
| 456 | COUT(5) << "synchronisable: not updating variable " << std::endl; |
---|
[2171] | 457 | // if we have a forcecallback then do the callback |
---|
[1735] | 458 | continue; // this variable should only be set |
---|
| 459 | } |
---|
| 460 | COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl; |
---|
| 461 | bool callback=false; |
---|
[2171] | 462 | bool master=false; |
---|
| 463 | |
---|
| 464 | if( ( (*i)->mode & direction::bidirectional ) == direction::bidirectional ) |
---|
| 465 | { |
---|
| 466 | uint8_t refNr = *(uint8_t *)mem; |
---|
| 467 | if( ( ((*i)->mode == direction::serverMaster) && (mode == 0x1) ) || \ |
---|
| 468 | ( ((*i)->mode == direction::clientMaster) && (mode == 0x2) ) ) |
---|
| 469 | { // MASTER |
---|
| 470 | master=true; |
---|
| 471 | if( refNr != (*i)->varReference || ( memcmp((*i)->var, (*i)->varBuffer, (*i)->size) != 0 ) ) |
---|
| 472 | { // DISCARD data |
---|
| 473 | if( (*i)->type == DATA ) |
---|
[2087] | 474 | { |
---|
[2171] | 475 | mem += sizeof((*i)->varReference) + (*i)->size; |
---|
[2087] | 476 | } |
---|
[2171] | 477 | else //STRING |
---|
[2087] | 478 | { |
---|
[2171] | 479 | mem += sizeof(size_t) + *(size_t *)mem; |
---|
[2087] | 480 | } |
---|
[2171] | 481 | if( forceCallback && (*i)->callback) |
---|
| 482 | (*i)->callback->call(); |
---|
| 483 | continue; |
---|
| 484 | }//otherwise everything is ok and we update the value |
---|
| 485 | } |
---|
| 486 | else // SLAVE |
---|
| 487 | { |
---|
| 488 | if( (*i)->varReference == refNr ){ |
---|
| 489 | //discard data because it's outdated or not different to what we've got |
---|
| 490 | if( (*i)->type == DATA ) |
---|
| 491 | { |
---|
| 492 | mem += sizeof((*i)->varReference) + (*i)->size; |
---|
| 493 | } |
---|
| 494 | else //STRING |
---|
| 495 | { |
---|
| 496 | mem += sizeof(size_t) + *(size_t *)mem; |
---|
| 497 | } |
---|
| 498 | if( forceCallback && (*i)->callback) |
---|
| 499 | (*i)->callback->call(); |
---|
| 500 | continue; |
---|
[2087] | 501 | } |
---|
[2171] | 502 | else |
---|
| 503 | (*i)->varReference = refNr; //copy the reference value for this variable |
---|
| 504 | } |
---|
| 505 | mem += sizeof((*i)->varReference); |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | switch((*i)->type){ |
---|
| 509 | case DATA: |
---|
[1735] | 510 | if((*i)->callback) // check whether this variable changed (but only if callback was set) |
---|
[2171] | 511 | { |
---|
| 512 | if(memcmp((*i)->var, mem, (*i)->size) != 0) |
---|
[1735] | 513 | callback=true; |
---|
[2171] | 514 | } |
---|
| 515 | if( master ) |
---|
| 516 | { |
---|
| 517 | if( callback || memcmp((*i)->var, mem, (*i)->size) != 0 ) |
---|
| 518 | //value changed, so set the buffer to the new value |
---|
| 519 | memcpy((*i)->varBuffer, mem, (*i)->size); |
---|
| 520 | } |
---|
| 521 | memcpy((*i)->var, mem, (*i)->size); |
---|
| 522 | mem += (*i)->size; |
---|
[1735] | 523 | break; |
---|
| 524 | case STRING: |
---|
[2087] | 525 | (*i)->size = *(size_t *)mem; |
---|
| 526 | mem += sizeof(size_t); |
---|
[2171] | 527 | |
---|
| 528 | if( (*i)->callback) // check whether this string changed |
---|
| 529 | if( *static_cast<std::string*>((*i)->var) != std::string((char *)mem) ) |
---|
[1735] | 530 | callback=true; |
---|
[2171] | 531 | if( master ) |
---|
| 532 | { |
---|
| 533 | if( callback || *static_cast<std::string*>((*i)->var) != std::string((char *)mem) ) |
---|
| 534 | //string changed. set the buffer to the new one |
---|
| 535 | *static_cast<std::string*>((*i)->varBuffer)=*static_cast<std::string*>( (void*)(mem+sizeof(size_t)) ); |
---|
| 536 | } |
---|
| 537 | |
---|
[1735] | 538 | *((std::string *)((*i)->var)) = std::string((const char*)mem); |
---|
| 539 | COUT(5) << "synchronisable: char: " << (const char*)mem << " string: " << std::string((const char*)mem) << std::endl; |
---|
| 540 | mem += (*i)->size; |
---|
| 541 | break; |
---|
| 542 | } |
---|
| 543 | // call the callback function, if defined |
---|
[2087] | 544 | if((callback || forceCallback) && (*i)->callback) |
---|
[1735] | 545 | (*i)->callback->call(); |
---|
| 546 | } |
---|
[2171] | 547 | assert(mem == data+syncHeader->size); |
---|
[1735] | 548 | return true; |
---|
| 549 | } |
---|
[1505] | 550 | |
---|
| 551 | /** |
---|
| 552 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
[1907] | 553 | * @param id id of the gamestate |
---|
| 554 | * @param mode same as getData |
---|
[1505] | 555 | * @return amount of bytes |
---|
| 556 | */ |
---|
[2171] | 557 | uint32_t Synchronisable::getSize(unsigned int id, uint8_t mode){ |
---|
[1907] | 558 | int tsize=sizeof(synchronisableHeader); |
---|
[1505] | 559 | if(mode==0x0) |
---|
| 560 | mode=state_; |
---|
[2171] | 561 | if(!doSync(id, mode)) |
---|
| 562 | return 0; |
---|
[1505] | 563 | std::list<synchronisableVariable *>::iterator i; |
---|
| 564 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
| 565 | if( ((*i)->mode & mode) == 0 ) |
---|
| 566 | continue; // this variable should only be received, so dont add its size to the send-size |
---|
| 567 | switch((*i)->type){ |
---|
| 568 | case DATA: |
---|
| 569 | tsize+=(*i)->size; |
---|
| 570 | break; |
---|
| 571 | case STRING: |
---|
| 572 | tsize+=sizeof(int); |
---|
| 573 | (*i)->size=((std::string *)(*i)->var)->length()+1; |
---|
| 574 | COUT(5) << "String size: " << (*i)->size << std::endl; |
---|
| 575 | tsize+=(*i)->size; |
---|
| 576 | break; |
---|
| 577 | } |
---|
[2087] | 578 | if( ( (*i)->mode & direction::bidirectional ) == direction::bidirectional ) |
---|
| 579 | { |
---|
| 580 | tsize+=sizeof( (*i)->varReference ); |
---|
| 581 | } |
---|
[1505] | 582 | } |
---|
| 583 | return tsize; |
---|
| 584 | } |
---|
[1639] | 585 | |
---|
[1735] | 586 | /** |
---|
[1907] | 587 | * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction) |
---|
| 588 | * @param id gamestate id |
---|
| 589 | * @return true/false |
---|
[1735] | 590 | */ |
---|
[2171] | 591 | bool Synchronisable::doSync(unsigned int id, uint8_t mode){ |
---|
| 592 | if(mode==0x0) |
---|
| 593 | mode=state_; |
---|
| 594 | return ( (objectMode_&mode)!=0 && (!syncList->empty() ) ); |
---|
[1735] | 595 | } |
---|
[1856] | 596 | |
---|
[1907] | 597 | bool Synchronisable::doSelection(unsigned int id){ |
---|
[2171] | 598 | return true; //TODO: change this |
---|
| 599 | //return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0); |
---|
[1751] | 600 | } |
---|
[1856] | 601 | |
---|
[1907] | 602 | /** |
---|
| 603 | * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones |
---|
| 604 | * @param mem pointer to the bytestream |
---|
| 605 | */ |
---|
| 606 | bool Synchronisable::isMyData(uint8_t* mem) |
---|
[1735] | 607 | { |
---|
[1907] | 608 | synchronisableHeader *header = (synchronisableHeader *)mem; |
---|
| 609 | assert(header->objectID==this->objectID); |
---|
| 610 | return header->dataAvailable; |
---|
[1735] | 611 | } |
---|
[1856] | 612 | |
---|
[1907] | 613 | /** |
---|
| 614 | * This function sets the synchronisation mode of the object |
---|
[2171] | 615 | * If set to 0x0 variables will not be synchronised at all |
---|
[1907] | 616 | * If set to 0x1 variables will only be synchronised to the client |
---|
| 617 | * If set to 0x2 variables will only be synchronised to the server |
---|
| 618 | * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar) |
---|
| 619 | * @param mode same as in registerVar |
---|
| 620 | */ |
---|
[2171] | 621 | void Synchronisable::setObjectMode(uint8_t mode){ |
---|
[2087] | 622 | assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3); |
---|
[1751] | 623 | objectMode_=mode; |
---|
[1505] | 624 | } |
---|
| 625 | |
---|
[1639] | 626 | |
---|
[1505] | 627 | } |
---|