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