[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> |
---|
[2011] | 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 | |
---|
[2132] | 52 | #include "Host.h" |
---|
[2112] | 53 | namespace orxonox |
---|
[1505] | 54 | { |
---|
[1639] | 55 | |
---|
[1940] | 56 | |
---|
[1907] | 57 | std::map<unsigned int, Synchronisable *> Synchronisable::objectMap_; |
---|
| 58 | std::queue<unsigned int> Synchronisable::deletedObjects_; |
---|
[1639] | 59 | |
---|
[2132] | 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 | */ |
---|
[2112] | 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 |
---|
[2156] | 71 | if ( !Host::running() || ( Host::running() && Host::isServer() ) ) |
---|
[2153] | 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 | } |
---|
[2133] | 77 | else |
---|
| 78 | objectID=OBJECTID_UNKNOWN; |
---|
[1940] | 79 | classID = (unsigned int)-1; |
---|
[1505] | 80 | syncList = new std::list<synchronisableVariable *>; |
---|
[2133] | 81 | |
---|
[2153] | 82 | |
---|
[2133] | 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 |
---|
[2034] | 90 | |
---|
| 91 | this->creatorID = OBJECTID_UNKNOWN; |
---|
| 92 | |
---|
| 93 | searchcreatorID: |
---|
| 94 | if (creator) |
---|
| 95 | { |
---|
[2041] | 96 | Synchronisable* synchronisable_creator = dynamic_cast<Synchronisable*>(creator); |
---|
| 97 | if (synchronisable_creator && synchronisable_creator->objectMode_) |
---|
[2034] | 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 | /** |
---|
[1940] | 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 |
---|
[2112] | 115 | if(!Identifier::isCreatingHierarchy()){ |
---|
[1534] | 116 | for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++) |
---|
| 117 | delete (*it)->callback; |
---|
[2132] | 118 | if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer())) |
---|
[1989] | 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 | } |
---|
[2153] | 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; |
---|
[1940] | 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 | */ |
---|
[2132] | 166 | Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode) |
---|
[1735] | 167 | { |
---|
[1907] | 168 | synchronisableHeader *header = (synchronisableHeader *)mem; |
---|
[1856] | 169 | |
---|
[2062] | 170 | if(!header->dataAvailable) |
---|
| 171 | { |
---|
| 172 | mem += header->size; |
---|
| 173 | return 0; |
---|
| 174 | } |
---|
| 175 | |
---|
[2041] | 176 | COUT(4) << "fabricating object with id: " << header->objectID << std::endl; |
---|
[1856] | 177 | |
---|
[2112] | 178 | Identifier* id = ClassByID(header->classID); |
---|
[1907] | 179 | assert(id); |
---|
[2112] | 180 | BaseObject* creator = 0; |
---|
[2034] | 181 | if (header->creatorID != OBJECTID_UNKNOWN) |
---|
| 182 | { |
---|
[2035] | 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 |
---|
[2112] | 190 | creator = dynamic_cast<BaseObject*>(synchronisable_creator); |
---|
[2034] | 191 | } |
---|
[2133] | 192 | assert(getSynchronisable(header->objectID)==0); //make sure no object with this id exists |
---|
[2112] | 193 | BaseObject *bo = id->fabricate(creator); |
---|
[2034] | 194 | assert(bo); |
---|
[1735] | 195 | Synchronisable *no = dynamic_cast<Synchronisable *>(bo); |
---|
| 196 | assert(no); |
---|
[1907] | 197 | no->objectID=header->objectID; |
---|
[2033] | 198 | no->creatorID=header->creatorID; //TODO: remove this |
---|
[1907] | 199 | no->classID=header->classID; |
---|
[2041] | 200 | COUT(4) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl; |
---|
[1735] | 201 | // update data and create object/entity... |
---|
[1945] | 202 | bool b = no->updateData(mem, mode, true); |
---|
[2035] | 203 | assert(b); |
---|
[2031] | 204 | if (b) |
---|
| 205 | { |
---|
| 206 | b = no->create(); |
---|
| 207 | assert(b); |
---|
| 208 | } |
---|
[1907] | 209 | return no; |
---|
| 210 | } |
---|
| 211 | |
---|
[1940] | 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 | } |
---|
[1940] | 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){ |
---|
[2153] | 238 | std::map<unsigned int, Synchronisable*>::iterator it1; |
---|
| 239 | it1 = objectMap_.find(objectID); |
---|
| 240 | if (it1 != objectMap_.end()) |
---|
| 241 | return it1->second; |
---|
| 242 | |
---|
[2112] | 243 | ObjectList<Synchronisable>::iterator it; |
---|
| 244 | for(it = ObjectList<Synchronisable>::begin(); it; ++it){ |
---|
[2153] | 245 | if( it->getObjectID()==objectID ){ |
---|
| 246 | objectMap_[objectID] = *it; |
---|
| 247 | return *it; |
---|
| 248 | } |
---|
[1907] | 249 | } |
---|
| 250 | return NULL; |
---|
| 251 | } |
---|
| 252 | |
---|
[1940] | 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 |
---|
[2112] | 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 | */ |
---|
[2143] | 263 | void Synchronisable::registerVariable(void *var, int size, variableType t, uint8_t mode, NetworkCallbackBase *cb){ |
---|
[2011] | 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; |
---|
[2011] | 272 | if( ( mode & direction::bidirectional ) ) |
---|
| 273 | { |
---|
[2132] | 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 | } |
---|
[2011] | 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 | } |
---|
[2143] | 297 | |
---|
| 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 | } |
---|
[2144] | 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 |
---|
[2143] | 312 | } |
---|
| 313 | |
---|
[1856] | 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 |
---|
[1990] | 327 | * @return true: if !doSync or if everything was successfully saved |
---|
[1735] | 328 | */ |
---|
[2132] | 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 |
---|
[2132] | 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; |
---|
[1940] | 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; |
---|
[2028] | 353 | header->creatorID = this->creatorID; |
---|
[1907] | 354 | header->classID = this->classID; |
---|
| 355 | header->dataAvailable = true; |
---|
[2132] | 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 | } |
---|
[2132] | 368 | |
---|
| 369 | // =========== start bidirectional stuff ============= |
---|
[2011] | 370 | // if the variable gets synchronised bidirectional, then add the reference to the bytestream |
---|
[2062] | 371 | if( ( (*i)->mode & direction::bidirectional ) == direction::bidirectional ) |
---|
[2011] | 372 | { |
---|
[2132] | 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 |
---|
[2011] | 394 | *(uint8_t*)mem = (*i)->varReference; |
---|
| 395 | mem += sizeof( (*i)->varReference ); |
---|
[2062] | 396 | tempsize += sizeof( (*i)->varReference ); |
---|
[2011] | 397 | } |
---|
[2132] | 398 | // ================== end bidirectional stuff |
---|
| 399 | |
---|
[1735] | 400 | switch((*i)->type){ |
---|
| 401 | case DATA: |
---|
| 402 | memcpy( (void *)(mem), (void*)((*i)->var), (*i)->size); |
---|
[2132] | 403 | mem += (*i)->size; |
---|
| 404 | tempsize += (*i)->size; |
---|
[1735] | 405 | break; |
---|
| 406 | case STRING: |
---|
[2011] | 407 | memcpy( (void *)(mem), (void *)&((*i)->size), sizeof(size_t) ); |
---|
[2132] | 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; |
---|
[2132] | 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 | */ |
---|
[2132] | 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; |
---|
[2132] | 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); |
---|
[2132] | 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){ |
---|
[2132] | 446 | mem += syncHeader->size; |
---|
[1751] | 447 | return true; |
---|
[1907] | 448 | } |
---|
[1856] | 449 | |
---|
[2132] | 450 | mem += sizeof(synchronisableHeader); |
---|
[1907] | 451 | // stop extract header |
---|
[1940] | 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; |
---|
[2132] | 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; |
---|
[2132] | 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 ) |
---|
[2011] | 474 | { |
---|
[2132] | 475 | mem += sizeof((*i)->varReference) + (*i)->size; |
---|
[2011] | 476 | } |
---|
[2132] | 477 | else //STRING |
---|
[2011] | 478 | { |
---|
[2132] | 479 | mem += sizeof(size_t) + *(size_t *)mem; |
---|
[2011] | 480 | } |
---|
[2132] | 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; |
---|
[2011] | 501 | } |
---|
[2132] | 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) |
---|
[2132] | 511 | { |
---|
| 512 | if(memcmp((*i)->var, mem, (*i)->size) != 0) |
---|
[1735] | 513 | callback=true; |
---|
[2132] | 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: |
---|
[2011] | 525 | (*i)->size = *(size_t *)mem; |
---|
| 526 | mem += sizeof(size_t); |
---|
[2132] | 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; |
---|
[2132] | 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 |
---|
[1945] | 544 | if((callback || forceCallback) && (*i)->callback) |
---|
[1735] | 545 | (*i)->callback->call(); |
---|
| 546 | } |
---|
[2132] | 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 | */ |
---|
[2132] | 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_; |
---|
[2132] | 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 | } |
---|
[2062] | 578 | if( ( (*i)->mode & direction::bidirectional ) == direction::bidirectional ) |
---|
[2011] | 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 | */ |
---|
[2132] | 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){ |
---|
[2132] | 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 |
---|
[2132] | 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 | */ |
---|
[2132] | 621 | void Synchronisable::setObjectMode(uint8_t mode){ |
---|
[1989] | 622 | assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3); |
---|
[1751] | 623 | objectMode_=mode; |
---|
[1505] | 624 | } |
---|
| 625 | |
---|
[1639] | 626 | |
---|
[1505] | 627 | } |
---|