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