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