[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 | |
---|
| 43 | #include <string> |
---|
| 44 | #include <iostream> |
---|
[1735] | 45 | #include <assert.h> |
---|
[1505] | 46 | |
---|
| 47 | #include "core/CoreIncludes.h" |
---|
[1735] | 48 | #include "core/BaseObject.h" |
---|
[1534] | 49 | // #include "core/Identifier.h" |
---|
[1505] | 50 | |
---|
| 51 | namespace network |
---|
| 52 | { |
---|
[1793] | 53 | |
---|
[1639] | 54 | |
---|
[1775] | 55 | std::map<unsigned int, Synchronisable *> Synchronisable::objectMap_; |
---|
| 56 | std::queue<unsigned int> Synchronisable::deletedObjects_; |
---|
[1639] | 57 | |
---|
[1505] | 58 | int Synchronisable::state_=0x1; // detemines wheter we are server (default) or client |
---|
[1639] | 59 | |
---|
[1505] | 60 | /** |
---|
| 61 | * Constructor: |
---|
[1793] | 62 | * Initializes all Variables and sets the right objectID |
---|
[1505] | 63 | */ |
---|
| 64 | Synchronisable::Synchronisable(){ |
---|
| 65 | RegisterRootObject(Synchronisable); |
---|
[1775] | 66 | static unsigned int idCounter=0; |
---|
[1505] | 67 | datasize=0; |
---|
[1751] | 68 | objectFrequency_=1; |
---|
| 69 | objectMode_=0x1; // by default do not send data to servere |
---|
[1505] | 70 | objectID=idCounter++; |
---|
| 71 | syncList = new std::list<synchronisableVariable *>; |
---|
| 72 | } |
---|
| 73 | |
---|
[1793] | 74 | /** |
---|
| 75 | * Destructor: |
---|
| 76 | * Delete all callback objects and remove objectID from the objectMap_ |
---|
| 77 | */ |
---|
[1505] | 78 | Synchronisable::~Synchronisable(){ |
---|
[1534] | 79 | // delete callback function objects |
---|
[1775] | 80 | if(!orxonox::Identifier::isCreatingHierarchy()){ |
---|
[1534] | 81 | for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++) |
---|
| 82 | delete (*it)->callback; |
---|
[1775] | 83 | assert(objectMap_[objectID]->objectID==objectID); |
---|
| 84 | objectMap_.erase(objectID); |
---|
| 85 | } |
---|
[1505] | 86 | } |
---|
[1639] | 87 | |
---|
[1793] | 88 | /** |
---|
| 89 | * This function gets called after all neccessary data has been passed to the object |
---|
| 90 | * Overload this function and recall the create function of the parent class |
---|
| 91 | * @return true/false |
---|
| 92 | */ |
---|
[1505] | 93 | bool Synchronisable::create(){ |
---|
[1775] | 94 | objectMap_[objectID]=this; |
---|
| 95 | assert(objectMap_[objectID]==this); |
---|
[1505] | 96 | this->classID = this->getIdentifier()->getNetworkID(); |
---|
| 97 | COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl; |
---|
| 98 | return true; |
---|
| 99 | } |
---|
[1639] | 100 | |
---|
[1751] | 101 | |
---|
[1793] | 102 | /** |
---|
| 103 | * This function sets the internal mode for synchronisation |
---|
| 104 | * @param b true if this object is located on a client or on a server |
---|
| 105 | */ |
---|
[1505] | 106 | void Synchronisable::setClient(bool b){ |
---|
| 107 | if(b) // client |
---|
| 108 | state_=0x2; |
---|
| 109 | else // server |
---|
| 110 | state_=0x1; |
---|
| 111 | } |
---|
[1735] | 112 | |
---|
[1793] | 113 | /** |
---|
| 114 | * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create |
---|
| 115 | * After calling this function the mem pointer will be increased by the size of the needed data |
---|
| 116 | * @param mem pointer to where the appropriate data is located |
---|
| 117 | * @param mode defines the mode, how the data should be loaded |
---|
| 118 | * @return pointer to the newly created synchronisable |
---|
| 119 | */ |
---|
[1775] | 120 | Synchronisable *Synchronisable::fabricate(unsigned char*& mem, int mode) |
---|
[1735] | 121 | { |
---|
[1827] | 122 | synchronisableHeader *header = (synchronisableHeader *)mem; |
---|
[1735] | 123 | |
---|
[1827] | 124 | orxonox::Identifier* id = GetIdentifier(header->classID); |
---|
[1775] | 125 | assert(id); |
---|
[1735] | 126 | orxonox::BaseObject *bo = id->fabricate(); |
---|
| 127 | Synchronisable *no = dynamic_cast<Synchronisable *>(bo); |
---|
| 128 | assert(no); |
---|
[1827] | 129 | no->objectID=header->objectID; |
---|
| 130 | no->classID=header->classID; |
---|
[1735] | 131 | COUT(3) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl; |
---|
| 132 | // update data and create object/entity... |
---|
[1775] | 133 | assert(no->updateData(mem, mode)); |
---|
| 134 | assert( no->create() ); |
---|
| 135 | return no; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
[1793] | 139 | /** |
---|
| 140 | * Finds and deletes the Synchronisable with the appropriate objectID |
---|
| 141 | * @param objectID objectID of the Synchronisable |
---|
| 142 | * @return true/false |
---|
| 143 | */ |
---|
[1775] | 144 | bool Synchronisable::deleteObject(unsigned int objectID){ |
---|
| 145 | assert(getSynchronisable(objectID)); |
---|
| 146 | assert(getSynchronisable(objectID)->objectID==objectID); |
---|
| 147 | delete objectMap_[objectID]; |
---|
[1735] | 148 | return true; |
---|
| 149 | } |
---|
[1775] | 150 | |
---|
[1793] | 151 | /** |
---|
| 152 | * This function looks up the objectID in the objectMap_ and returns a pointer to the right Synchronisable |
---|
| 153 | * @param objectID objectID of the Synchronisable |
---|
| 154 | * @return pointer to the Synchronisable with the objectID |
---|
| 155 | */ |
---|
[1775] | 156 | Synchronisable* Synchronisable::getSynchronisable(unsigned int objectID){ |
---|
| 157 | std::map<unsigned int, Synchronisable *>::iterator i = objectMap_.find(objectID); |
---|
| 158 | if(i==objectMap_.end()) |
---|
| 159 | return NULL; |
---|
| 160 | assert(i->second->objectID==objectID); |
---|
| 161 | return (*i).second; |
---|
| 162 | } |
---|
[1505] | 163 | |
---|
[1775] | 164 | |
---|
[1505] | 165 | /** |
---|
| 166 | * This function is used to register a variable to be synchronized |
---|
| 167 | * also counts the total datasize needed to save the variables |
---|
| 168 | * @param var pointer to the variable |
---|
| 169 | * @param size size of the datatype the variable consists of |
---|
[1793] | 170 | * @param t the type of the variable (network::DATA or network::STRING |
---|
| 171 | * @param mode same as in getData |
---|
| 172 | * @param cb callback object that should get called, if the value of the variable changes |
---|
[1505] | 173 | */ |
---|
[1534] | 174 | void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){ |
---|
[1505] | 175 | // create temporary synch.Var struct |
---|
| 176 | synchronisableVariable *temp = new synchronisableVariable; |
---|
| 177 | temp->size = size; |
---|
| 178 | temp->var = var; |
---|
[1639] | 179 | temp->mode = mode; |
---|
[1505] | 180 | temp->type = t; |
---|
[1534] | 181 | temp->callback = cb; |
---|
[1639] | 182 | COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; |
---|
[1505] | 183 | // increase datasize |
---|
| 184 | datasize+=sizeof(int)+size; |
---|
| 185 | //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; |
---|
| 186 | COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl; |
---|
| 187 | syncList->push_back(temp); |
---|
[1794] | 188 | #ifndef NDEBUG |
---|
| 189 | std::list<synchronisableVariable *>::iterator it = syncList->begin(); |
---|
| 190 | while(it!=syncList->end()){ |
---|
| 191 | assert(*it!=var); |
---|
| 192 | it++; |
---|
| 193 | } |
---|
| 194 | #endif |
---|
[1505] | 195 | } |
---|
[1735] | 196 | |
---|
| 197 | /** |
---|
[1793] | 198 | * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory |
---|
[1735] | 199 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
[1751] | 200 | * structure of the bitstream: |
---|
[1793] | 201 | * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...| |
---|
| 202 | * length of varx: size saved int syncvarlist |
---|
| 203 | * @param mem pointer to allocated memory with enough size |
---|
| 204 | * @param id gamestateid of the gamestate to be saved (important for priorities) |
---|
| 205 | * @param mode defines the direction in which the data will be send/received |
---|
| 206 | * 0x1: server->client |
---|
| 207 | * 0x2: client->server (not recommended) |
---|
| 208 | * 0x3: bidirectional |
---|
| 209 | * @return true: if !isMyTick or if everything was successfully saved |
---|
[1735] | 210 | */ |
---|
[1751] | 211 | bool Synchronisable::getData(unsigned char*& mem, unsigned int id, int mode){ |
---|
[1775] | 212 | //if this tick is we dont synchronise, then abort now |
---|
| 213 | if(!isMyTick(id)) |
---|
| 214 | return true; |
---|
[1735] | 215 | //std::cout << "inside getData" << std::endl; |
---|
| 216 | unsigned int tempsize = 0; |
---|
| 217 | if(mode==0x0) |
---|
| 218 | mode=state_; |
---|
| 219 | if(classID==0) |
---|
| 220 | COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl; |
---|
[1751] | 221 | this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this |
---|
[1735] | 222 | std::list<synchronisableVariable *>::iterator i; |
---|
| 223 | unsigned int size; |
---|
[1775] | 224 | size=getSize(id, mode); |
---|
[1735] | 225 | |
---|
| 226 | // start copy header |
---|
[1827] | 227 | synchronisableHeader *header = (synchronisableHeader *)mem; |
---|
| 228 | header->size = size; |
---|
| 229 | header->objectID = this->objectID; |
---|
| 230 | header->classID = this->classID; |
---|
| 231 | header->dataAvailable = true; |
---|
| 232 | tempsize+=sizeof(synchronisableHeader); |
---|
| 233 | mem+=sizeof(synchronisableHeader); |
---|
[1735] | 234 | // end copy header |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl; |
---|
| 238 | // copy to location |
---|
| 239 | for(i=syncList->begin(); i!=syncList->end(); ++i){ |
---|
| 240 | if( ((*i)->mode & mode) == 0 ){ |
---|
| 241 | COUT(5) << "not getting data: " << std::endl; |
---|
| 242 | continue; // this variable should only be received |
---|
| 243 | } |
---|
| 244 | switch((*i)->type){ |
---|
| 245 | case DATA: |
---|
| 246 | memcpy( (void *)(mem), (void*)((*i)->var), (*i)->size); |
---|
| 247 | mem+=(*i)->size; |
---|
| 248 | tempsize+=(*i)->size; |
---|
| 249 | break; |
---|
| 250 | case STRING: |
---|
| 251 | memcpy( (void *)(mem), (void *)&((*i)->size), sizeof(int) ); |
---|
| 252 | mem+=sizeof(int); |
---|
| 253 | const char *data = ( ( *(std::string *) (*i)->var).c_str()); |
---|
| 254 | memcpy( mem, (void*)data, (*i)->size); |
---|
| 255 | COUT(5) << "synchronisable: char: " << (const char *)(mem) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl; |
---|
| 256 | mem+=(*i)->size; |
---|
| 257 | tempsize+=(*i)->size + 4; |
---|
| 258 | break; |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | assert(tempsize==size); |
---|
| 262 | return true; |
---|
| 263 | } |
---|
[1505] | 264 | |
---|
[1735] | 265 | |
---|
[1505] | 266 | /** |
---|
[1793] | 267 | * This function takes a bytestream and loads the data into the registered variables |
---|
| 268 | * @param mem pointer to the bytestream |
---|
| 269 | * @param mode same as in getData |
---|
[1735] | 270 | * @return true/false |
---|
| 271 | */ |
---|
| 272 | bool Synchronisable::updateData(unsigned char*& mem, int mode){ |
---|
| 273 | if(mode==0x0) |
---|
| 274 | mode=state_; |
---|
| 275 | std::list<synchronisableVariable *>::iterator i; |
---|
| 276 | if(syncList->empty()){ |
---|
| 277 | COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; |
---|
| 278 | return false; |
---|
| 279 | } |
---|
[1775] | 280 | unsigned char *data=mem; |
---|
[1735] | 281 | // start extract header |
---|
[1827] | 282 | synchronisableHeader *syncHeader = (synchronisableHeader *)mem; |
---|
| 283 | assert(syncHeader->objectID==this->objectID); |
---|
| 284 | if(syncHeader->dataAvailable==false){ |
---|
| 285 | mem+=syncHeader->size; |
---|
[1775] | 286 | return true; |
---|
[1827] | 287 | } |
---|
| 288 | |
---|
| 289 | mem+=sizeof(synchronisableHeader); |
---|
[1735] | 290 | // stop extract header |
---|
[1827] | 291 | assert(this->objectID==syncHeader->objectID); |
---|
| 292 | // assert(this->classID==syncHeader->classID); //TODO: fix this!!! maybe a problem with the identifier ? |
---|
[1735] | 293 | |
---|
[1827] | 294 | COUT(5) << "Synchronisable: objectID " << syncHeader->objectID << ", classID " << syncHeader->classID << " size: " << syncHeader->size << " synchronising data" << std::endl; |
---|
| 295 | for(i=syncList->begin(); i!=syncList->end() && mem <= data+syncHeader->size; i++){ |
---|
[1735] | 296 | if( ((*i)->mode ^ mode) == 0 ){ |
---|
| 297 | COUT(5) << "synchronisable: not updating variable " << std::endl; |
---|
| 298 | continue; // this variable should only be set |
---|
| 299 | } |
---|
| 300 | COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl; |
---|
| 301 | bool callback=false; |
---|
| 302 | switch((*i)->type){ |
---|
| 303 | case DATA: |
---|
| 304 | if((*i)->callback) // check whether this variable changed (but only if callback was set) |
---|
| 305 | if(strncmp((char *)(*i)->var, (char *)mem, (*i)->size)!=0) |
---|
| 306 | callback=true; |
---|
| 307 | memcpy((void*)(*i)->var, mem, (*i)->size); |
---|
| 308 | mem+=(*i)->size; |
---|
| 309 | break; |
---|
| 310 | case STRING: |
---|
| 311 | (*i)->size = *(int *)mem; |
---|
| 312 | COUT(5) << "string size: " << (*i)->size << std::endl; |
---|
| 313 | mem+=sizeof(int); |
---|
| 314 | if((*i)->callback) // check whether this string changed |
---|
| 315 | if( *(std::string *)((*i)->var) != std::string((char *)mem) ) |
---|
| 316 | callback=true; |
---|
| 317 | *((std::string *)((*i)->var)) = std::string((const char*)mem); |
---|
| 318 | COUT(5) << "synchronisable: char: " << (const char*)mem << " string: " << std::string((const char*)mem) << std::endl; |
---|
| 319 | mem += (*i)->size; |
---|
| 320 | break; |
---|
| 321 | } |
---|
| 322 | // call the callback function, if defined |
---|
| 323 | if(callback && (*i)->callback) |
---|
| 324 | (*i)->callback->call(); |
---|
| 325 | } |
---|
| 326 | return true; |
---|
| 327 | } |
---|
[1505] | 328 | |
---|
| 329 | /** |
---|
| 330 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
[1793] | 331 | * @param id id of the gamestate |
---|
| 332 | * @param mode same as getData |
---|
[1505] | 333 | * @return amount of bytes |
---|
| 334 | */ |
---|
[1751] | 335 | int Synchronisable::getSize(unsigned int id, int mode){ |
---|
| 336 | if(!isMyTick(id)) |
---|
| 337 | return 0; |
---|
[1775] | 338 | int tsize=sizeof(synchronisableHeader); |
---|
[1505] | 339 | if(mode==0x0) |
---|
| 340 | mode=state_; |
---|
| 341 | std::list<synchronisableVariable *>::iterator i; |
---|
| 342 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
| 343 | if( ((*i)->mode & mode) == 0 ) |
---|
| 344 | continue; // this variable should only be received, so dont add its size to the send-size |
---|
| 345 | switch((*i)->type){ |
---|
| 346 | case DATA: |
---|
| 347 | tsize+=(*i)->size; |
---|
| 348 | break; |
---|
| 349 | case STRING: |
---|
| 350 | tsize+=sizeof(int); |
---|
| 351 | (*i)->size=((std::string *)(*i)->var)->length()+1; |
---|
| 352 | COUT(5) << "String size: " << (*i)->size << std::endl; |
---|
| 353 | tsize+=(*i)->size; |
---|
| 354 | break; |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | return tsize; |
---|
| 358 | } |
---|
[1639] | 359 | |
---|
[1735] | 360 | /** |
---|
[1800] | 361 | * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction) |
---|
[1793] | 362 | * @param id gamestate id |
---|
| 363 | * @return true/false |
---|
[1751] | 364 | */ |
---|
| 365 | bool Synchronisable::isMyTick(unsigned int id){ |
---|
[1800] | 366 | return ( (objectMode_&state_)!=0 ); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | bool Synchronisable::doSelection(unsigned int id){ |
---|
[1751] | 370 | return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0); |
---|
| 371 | } |
---|
| 372 | |
---|
[1793] | 373 | /** |
---|
| 374 | * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones |
---|
| 375 | * @param mem pointer to the bytestream |
---|
| 376 | */ |
---|
[1735] | 377 | bool Synchronisable::isMyData(unsigned char* mem) |
---|
| 378 | { |
---|
[1827] | 379 | synchronisableHeader *header = (synchronisableHeader *)mem; |
---|
| 380 | assert(header->objectID==this->objectID); |
---|
| 381 | return header->dataAvailable; |
---|
[1735] | 382 | } |
---|
| 383 | |
---|
[1793] | 384 | /** |
---|
| 385 | * This function sets the synchronisation mode of the object |
---|
| 386 | * If set to 0x1 variables will only be synchronised to the client |
---|
| 387 | * If set to 0x2 variables will only be synchronised to the server |
---|
| 388 | * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar) |
---|
| 389 | * @param mode same as in registerVar |
---|
| 390 | */ |
---|
[1751] | 391 | void Synchronisable::setObjectMode(int mode){ |
---|
| 392 | assert(mode==0x1 || mode==0x2 || mode==0x3); |
---|
| 393 | objectMode_=mode; |
---|
[1505] | 394 | } |
---|
| 395 | |
---|
[1639] | 396 | |
---|
[1505] | 397 | } |
---|