[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 | #include "Synchronisable.h" |
---|
| 32 | |
---|
[1837] | 33 | #include <cstring> |
---|
[2087] | 34 | #include <string> |
---|
[1505] | 35 | #include <iostream> |
---|
[2773] | 36 | #include <cassert> |
---|
[1505] | 37 | |
---|
| 38 | #include "core/CoreIncludes.h" |
---|
[1735] | 39 | #include "core/BaseObject.h" |
---|
[1534] | 40 | // #include "core/Identifier.h" |
---|
[1505] | 41 | |
---|
[2211] | 42 | #include "network/Host.h" |
---|
[2171] | 43 | namespace orxonox |
---|
[1505] | 44 | { |
---|
[1639] | 45 | |
---|
[2309] | 46 | std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_; |
---|
| 47 | std::queue<uint32_t> Synchronisable::deletedObjects_; |
---|
[1639] | 48 | |
---|
[2171] | 49 | uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client |
---|
[1639] | 50 | |
---|
[1505] | 51 | /** |
---|
| 52 | * Constructor: |
---|
[1907] | 53 | * Initializes all Variables and sets the right objectID |
---|
[1505] | 54 | */ |
---|
[2171] | 55 | Synchronisable::Synchronisable(BaseObject* creator){ |
---|
[1505] | 56 | RegisterRootObject(Synchronisable); |
---|
[1907] | 57 | static uint32_t idCounter=0; |
---|
| 58 | objectMode_=0x1; // by default do not send data to server |
---|
[2171] | 59 | if ( !Host::running() || ( Host::running() && Host::isServer() ) ) |
---|
| 60 | { |
---|
| 61 | this->objectID = idCounter++; //this is only needed when running a server |
---|
| 62 | //add synchronisable to the objectMap |
---|
| 63 | objectMap_[this->objectID] = this; |
---|
| 64 | } |
---|
| 65 | else |
---|
| 66 | objectID=OBJECTID_UNKNOWN; |
---|
[2309] | 67 | classID = static_cast<uint32_t>(-1); |
---|
[2822] | 68 | |
---|
| 69 | // set dataSize to 0 |
---|
| 70 | this->dataSize_ = 0; |
---|
[2415] | 71 | // set standard priority |
---|
| 72 | this->setPriority( priority::normal ); |
---|
[2171] | 73 | |
---|
[2415] | 74 | // get creator id |
---|
[2087] | 75 | this->creatorID = OBJECTID_UNKNOWN; |
---|
| 76 | |
---|
| 77 | searchcreatorID: |
---|
| 78 | if (creator) |
---|
| 79 | { |
---|
| 80 | Synchronisable* synchronisable_creator = dynamic_cast<Synchronisable*>(creator); |
---|
| 81 | if (synchronisable_creator && synchronisable_creator->objectMode_) |
---|
| 82 | { |
---|
| 83 | this->creatorID = synchronisable_creator->getObjectID(); |
---|
| 84 | } |
---|
| 85 | else if (creator != creator->getCreator()) |
---|
| 86 | { |
---|
| 87 | creator = creator->getCreator(); |
---|
| 88 | goto searchcreatorID; |
---|
| 89 | } |
---|
| 90 | } |
---|
[1505] | 91 | } |
---|
| 92 | |
---|
[1907] | 93 | /** |
---|
[2087] | 94 | * Destructor: |
---|
[1907] | 95 | * Delete all callback objects and remove objectID from the objectMap_ |
---|
| 96 | */ |
---|
[1505] | 97 | Synchronisable::~Synchronisable(){ |
---|
[1534] | 98 | // delete callback function objects |
---|
[2171] | 99 | if(!Identifier::isCreatingHierarchy()){ |
---|
[2823] | 100 | for(std::vector<SynchronisableVariableBase*>::iterator it = syncList.begin(); it!=syncList.end(); it++) |
---|
[2245] | 101 | delete (*it); |
---|
[2171] | 102 | if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer())) |
---|
[2087] | 103 | deletedObjects_.push(objectID); |
---|
[1907] | 104 | } |
---|
[2309] | 105 | std::map<uint32_t, Synchronisable*>::iterator it; |
---|
[2171] | 106 | it = objectMap_.find(objectID); |
---|
| 107 | if (it != objectMap_.end()) |
---|
| 108 | objectMap_.erase(it); |
---|
[2485] | 109 | |
---|
[1505] | 110 | } |
---|
[1639] | 111 | |
---|
[2087] | 112 | |
---|
[1907] | 113 | /** |
---|
| 114 | * This function sets the internal mode for synchronisation |
---|
| 115 | * @param b true if this object is located on a client or on a server |
---|
| 116 | */ |
---|
[1505] | 117 | void Synchronisable::setClient(bool b){ |
---|
| 118 | if(b) // client |
---|
| 119 | state_=0x2; |
---|
| 120 | else // server |
---|
| 121 | state_=0x1; |
---|
| 122 | } |
---|
[1856] | 123 | |
---|
[1907] | 124 | /** |
---|
| 125 | * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create |
---|
| 126 | * After calling this function the mem pointer will be increased by the size of the needed data |
---|
| 127 | * @param mem pointer to where the appropriate data is located |
---|
| 128 | * @param mode defines the mode, how the data should be loaded |
---|
| 129 | * @return pointer to the newly created synchronisable |
---|
| 130 | */ |
---|
[2171] | 131 | Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode) |
---|
[1735] | 132 | { |
---|
[2662] | 133 | SynchronisableHeader header(mem); |
---|
[1856] | 134 | |
---|
[2662] | 135 | if(!header.isDataAvailable()) |
---|
[2087] | 136 | { |
---|
[2662] | 137 | mem += header.getDataSize(); |
---|
[2087] | 138 | return 0; |
---|
| 139 | } |
---|
[2171] | 140 | |
---|
[2662] | 141 | COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl; |
---|
[1856] | 142 | |
---|
[2662] | 143 | Identifier* id = ClassByID(header.getClassID()); |
---|
[2485] | 144 | if (!id) |
---|
| 145 | { |
---|
[2759] | 146 | for(int i = 0; i<100; i++) |
---|
| 147 | COUT(0) << "classid: " << i << " identifier: " << ClassByID(i) << endl; |
---|
[2485] | 148 | COUT(0) << "Assertion failed: id" << std::endl; |
---|
| 149 | COUT(0) << "Possible reason for this error: Client received a synchronizable object whose class has no factory." << std::endl; |
---|
| 150 | abort(); |
---|
| 151 | } |
---|
[1907] | 152 | assert(id); |
---|
[2171] | 153 | BaseObject* creator = 0; |
---|
[2662] | 154 | if (header.getCreatorID() != OBJECTID_UNKNOWN) |
---|
[2087] | 155 | { |
---|
[2662] | 156 | Synchronisable* synchronisable_creator = Synchronisable::getSynchronisable(header.getCreatorID()); |
---|
[2087] | 157 | if (!synchronisable_creator) |
---|
| 158 | { |
---|
[2662] | 159 | mem += header.getDataSize(); //.TODO: this suckz.... remove size from header |
---|
| 160 | assert(0); // TODO: uncomment this if we have a clean objecthierarchy (with destruction of children of objects) ^^ |
---|
[2087] | 161 | return 0; |
---|
| 162 | } |
---|
| 163 | else |
---|
[2171] | 164 | creator = dynamic_cast<BaseObject*>(synchronisable_creator); |
---|
[2087] | 165 | } |
---|
[2662] | 166 | assert(getSynchronisable(header.getObjectID())==0); //make sure no object with this id exists |
---|
[2171] | 167 | BaseObject *bo = id->fabricate(creator); |
---|
[2087] | 168 | assert(bo); |
---|
[1735] | 169 | Synchronisable *no = dynamic_cast<Synchronisable *>(bo); |
---|
| 170 | assert(no); |
---|
[2662] | 171 | no->objectID=header.getObjectID(); |
---|
| 172 | no->creatorID=header.getCreatorID(); //TODO: remove this |
---|
| 173 | no->classID=header.getClassID(); |
---|
[2087] | 174 | COUT(4) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl; |
---|
[1735] | 175 | // update data and create object/entity... |
---|
[2087] | 176 | bool b = no->updateData(mem, mode, true); |
---|
[1907] | 177 | assert(b); |
---|
[2087] | 178 | if (b) |
---|
| 179 | { |
---|
[2245] | 180 | // b = no->create(); |
---|
[2087] | 181 | assert(b); |
---|
| 182 | } |
---|
[1907] | 183 | return no; |
---|
| 184 | } |
---|
| 185 | |
---|
[2087] | 186 | |
---|
[1907] | 187 | /** |
---|
| 188 | * Finds and deletes the Synchronisable with the appropriate objectID |
---|
| 189 | * @param objectID objectID of the Synchronisable |
---|
| 190 | * @return true/false |
---|
| 191 | */ |
---|
[2309] | 192 | bool Synchronisable::deleteObject(uint32_t objectID){ |
---|
[1907] | 193 | if(!getSynchronisable(objectID)) |
---|
[1735] | 194 | return false; |
---|
[1907] | 195 | assert(getSynchronisable(objectID)->objectID==objectID); |
---|
| 196 | Synchronisable *s = getSynchronisable(objectID); |
---|
| 197 | if(s) |
---|
| 198 | delete s; |
---|
| 199 | else |
---|
[1735] | 200 | return false; |
---|
| 201 | return true; |
---|
| 202 | } |
---|
[2087] | 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 | */ |
---|
[2309] | 209 | Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID){ |
---|
| 210 | std::map<uint32_t, Synchronisable*>::iterator it1; |
---|
[2171] | 211 | it1 = objectMap_.find(objectID); |
---|
| 212 | if (it1 != objectMap_.end()) |
---|
| 213 | return it1->second; |
---|
| 214 | |
---|
| 215 | ObjectList<Synchronisable>::iterator it; |
---|
| 216 | for(it = ObjectList<Synchronisable>::begin(); it; ++it){ |
---|
| 217 | if( it->getObjectID()==objectID ){ |
---|
| 218 | objectMap_[objectID] = *it; |
---|
| 219 | return *it; |
---|
| 220 | } |
---|
[1907] | 221 | } |
---|
| 222 | return NULL; |
---|
| 223 | } |
---|
| 224 | |
---|
[2087] | 225 | |
---|
[1505] | 226 | /** |
---|
[1907] | 227 | * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory |
---|
[1735] | 228 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
[1751] | 229 | * structure of the bitstream: |
---|
[1907] | 230 | * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...| |
---|
| 231 | * length of varx: size saved int syncvarlist |
---|
| 232 | * @param mem pointer to allocated memory with enough size |
---|
| 233 | * @param id gamestateid of the gamestate to be saved (important for priorities) |
---|
| 234 | * @param mode defines the direction in which the data will be send/received |
---|
| 235 | * 0x1: server->client |
---|
| 236 | * 0x2: client->server (not recommended) |
---|
| 237 | * 0x3: bidirectional |
---|
[2087] | 238 | * @return true: if !doSync or if everything was successfully saved |
---|
[1735] | 239 | */ |
---|
[2832] | 240 | uint32_t Synchronisable::getData(uint8_t*& mem, int32_t id, uint8_t mode){ |
---|
[2171] | 241 | if(mode==0x0) |
---|
| 242 | mode=state_; |
---|
[1907] | 243 | //if this tick is we dont synchronise, then abort now |
---|
[2171] | 244 | if(!doSync(id, mode)) |
---|
[2832] | 245 | return 0; |
---|
[2309] | 246 | uint32_t tempsize = 0; |
---|
[2316] | 247 | if (this->classID==0) |
---|
[1735] | 248 | COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl; |
---|
[2087] | 249 | |
---|
[2309] | 250 | if (this->classID == static_cast<uint32_t>(-1)) |
---|
[2087] | 251 | this->classID = this->getIdentifier()->getNetworkID(); |
---|
| 252 | |
---|
[1907] | 253 | assert(this->classID==this->getIdentifier()->getNetworkID()); |
---|
[2823] | 254 | std::vector<SynchronisableVariableBase*>::iterator i; |
---|
[1856] | 255 | |
---|
[1735] | 256 | // start copy header |
---|
[2662] | 257 | SynchronisableHeader header(mem); |
---|
| 258 | mem += SynchronisableHeader::getSize(); |
---|
[1735] | 259 | // end copy header |
---|
[1856] | 260 | |
---|
| 261 | |
---|
[2822] | 262 | COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << std::endl; |
---|
[1735] | 263 | // copy to location |
---|
[2245] | 264 | for(i=syncList.begin(); i!=syncList.end(); ++i){ |
---|
[2822] | 265 | tempsize += (*i)->getData( mem, mode ); |
---|
| 266 | //tempsize += (*i)->getSize( mode ); |
---|
[1735] | 267 | } |
---|
[2822] | 268 | |
---|
| 269 | tempsize += SynchronisableHeader::getSize(); |
---|
| 270 | header.setObjectID( this->objectID ); |
---|
| 271 | header.setCreatorID( this->creatorID ); |
---|
| 272 | header.setClassID( this->classID ); |
---|
| 273 | header.setDataAvailable( true ); |
---|
| 274 | header.setDataSize( tempsize ); |
---|
| 275 | |
---|
| 276 | #ifndef NDEBUG |
---|
| 277 | uint32_t size; |
---|
| 278 | size=getSize(id, mode); |
---|
[1735] | 279 | assert(tempsize==size); |
---|
[2822] | 280 | #endif |
---|
[2832] | 281 | return tempsize; |
---|
[1735] | 282 | } |
---|
[1505] | 283 | |
---|
[1856] | 284 | |
---|
[1505] | 285 | /** |
---|
[1907] | 286 | * This function takes a bytestream and loads the data into the registered variables |
---|
| 287 | * @param mem pointer to the bytestream |
---|
| 288 | * @param mode same as in getData |
---|
[1735] | 289 | * @return true/false |
---|
| 290 | */ |
---|
[2171] | 291 | bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){ |
---|
[1735] | 292 | if(mode==0x0) |
---|
| 293 | mode=state_; |
---|
[2823] | 294 | std::vector<SynchronisableVariableBase *>::iterator i; |
---|
[2245] | 295 | if(syncList.empty()){ |
---|
[2419] | 296 | assert(0); |
---|
[1735] | 297 | COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; |
---|
| 298 | return false; |
---|
| 299 | } |
---|
[1856] | 300 | |
---|
[2245] | 301 | uint8_t* data=mem; |
---|
[1735] | 302 | // start extract header |
---|
[2662] | 303 | SynchronisableHeader syncHeader(mem); |
---|
| 304 | assert(syncHeader.getObjectID()==this->objectID); |
---|
| 305 | assert(syncHeader.getCreatorID()==this->creatorID); |
---|
| 306 | assert(syncHeader.getClassID()==this->classID); |
---|
| 307 | if(syncHeader.isDataAvailable()==false){ |
---|
| 308 | mem += syncHeader.getDataSize(); |
---|
[1751] | 309 | return true; |
---|
[1907] | 310 | } |
---|
[1856] | 311 | |
---|
[2662] | 312 | mem += SynchronisableHeader::getSize(); |
---|
[1907] | 313 | // stop extract header |
---|
[2087] | 314 | |
---|
[2662] | 315 | //COUT(5) << "Synchronisable: objectID " << syncHeader.getObjectID() << ", classID " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << std::endl; |
---|
| 316 | for(i=syncList.begin(); i!=syncList.end(); i++) |
---|
[2245] | 317 | { |
---|
[2662] | 318 | assert( mem <= data+syncHeader.getDataSize() ); // always make sure we don't exceed the datasize in our stream |
---|
[2245] | 319 | (*i)->putData( mem, mode, forceCallback ); |
---|
[1735] | 320 | } |
---|
[2662] | 321 | assert(mem == data+syncHeader.getDataSize()); |
---|
[1735] | 322 | return true; |
---|
| 323 | } |
---|
[1505] | 324 | |
---|
| 325 | /** |
---|
| 326 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
[1907] | 327 | * @param id id of the gamestate |
---|
| 328 | * @param mode same as getData |
---|
[1505] | 329 | * @return amount of bytes |
---|
| 330 | */ |
---|
[2309] | 331 | uint32_t Synchronisable::getSize(int32_t id, uint8_t mode){ |
---|
[2662] | 332 | int tsize=SynchronisableHeader::getSize(); |
---|
[2822] | 333 | if (mode==0x0) |
---|
[1505] | 334 | mode=state_; |
---|
[2822] | 335 | if (!doSync(id, mode)) |
---|
[2171] | 336 | return 0; |
---|
[2822] | 337 | assert( mode==state_ ); |
---|
| 338 | tsize += this->dataSize_; |
---|
[2823] | 339 | std::vector<SynchronisableVariableBase*>::iterator i; |
---|
[2822] | 340 | for(i=stringList.begin(); i!=stringList.end(); ++i){ |
---|
[2245] | 341 | tsize += (*i)->getSize( mode ); |
---|
[1505] | 342 | } |
---|
| 343 | return tsize; |
---|
| 344 | } |
---|
[1639] | 345 | |
---|
[1735] | 346 | /** |
---|
[1907] | 347 | * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction) |
---|
| 348 | * @param id gamestate id |
---|
| 349 | * @return true/false |
---|
[1735] | 350 | */ |
---|
[2309] | 351 | bool Synchronisable::doSync(int32_t id, uint8_t mode){ |
---|
[2171] | 352 | if(mode==0x0) |
---|
| 353 | mode=state_; |
---|
[2245] | 354 | return ( (objectMode_&mode)!=0 && (!syncList.empty() ) ); |
---|
[1735] | 355 | } |
---|
[1856] | 356 | |
---|
[1907] | 357 | /** |
---|
| 358 | * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones |
---|
| 359 | * @param mem pointer to the bytestream |
---|
| 360 | */ |
---|
| 361 | bool Synchronisable::isMyData(uint8_t* mem) |
---|
[1735] | 362 | { |
---|
[2662] | 363 | SynchronisableHeader header(mem); |
---|
| 364 | assert(header.getObjectID()==this->objectID); |
---|
| 365 | return header.isDataAvailable(); |
---|
[1735] | 366 | } |
---|
[1856] | 367 | |
---|
[1907] | 368 | /** |
---|
| 369 | * This function sets the synchronisation mode of the object |
---|
[2171] | 370 | * If set to 0x0 variables will not be synchronised at all |
---|
[1907] | 371 | * If set to 0x1 variables will only be synchronised to the client |
---|
| 372 | * If set to 0x2 variables will only be synchronised to the server |
---|
| 373 | * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar) |
---|
| 374 | * @param mode same as in registerVar |
---|
| 375 | */ |
---|
[2171] | 376 | void Synchronisable::setObjectMode(uint8_t mode){ |
---|
[2087] | 377 | assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3); |
---|
[1751] | 378 | objectMode_=mode; |
---|
[1505] | 379 | } |
---|
| 380 | |
---|
[2485] | 381 | |
---|
[1505] | 382 | } |
---|