[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> |
---|
[1505] | 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 | { |
---|
[1639] | 53 | |
---|
| 54 | |
---|
[1505] | 55 | int Synchronisable::state_=0x1; // detemines wheter we are server (default) or client |
---|
[1639] | 56 | |
---|
[1505] | 57 | /** |
---|
| 58 | * Constructor: |
---|
| 59 | * calls registarAllVariables, that has to be implemented by the inheriting classID |
---|
| 60 | */ |
---|
| 61 | Synchronisable::Synchronisable(){ |
---|
| 62 | RegisterRootObject(Synchronisable); |
---|
| 63 | static int idCounter=0; |
---|
| 64 | datasize=0; |
---|
[1751] | 65 | objectFrequency_=1; |
---|
| 66 | objectMode_=0x1; // by default do not send data to servere |
---|
[1505] | 67 | objectID=idCounter++; |
---|
| 68 | syncList = new std::list<synchronisableVariable *>; |
---|
| 69 | //registerAllVariables(); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | Synchronisable::~Synchronisable(){ |
---|
[1534] | 73 | // delete callback function objects |
---|
| 74 | if(!orxonox::Identifier::isCreatingHierarchy()) |
---|
| 75 | for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++) |
---|
| 76 | delete (*it)->callback; |
---|
[1505] | 77 | } |
---|
[1639] | 78 | |
---|
[1505] | 79 | bool Synchronisable::create(){ |
---|
| 80 | this->classID = this->getIdentifier()->getNetworkID(); |
---|
| 81 | COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl; |
---|
| 82 | return true; |
---|
| 83 | } |
---|
[1639] | 84 | |
---|
[1856] | 85 | |
---|
[1505] | 86 | void Synchronisable::setClient(bool b){ |
---|
| 87 | if(b) // client |
---|
| 88 | state_=0x2; |
---|
| 89 | else // server |
---|
| 90 | state_=0x1; |
---|
| 91 | } |
---|
[1856] | 92 | |
---|
[1735] | 93 | bool Synchronisable::fabricate(unsigned char*& mem, int mode) |
---|
| 94 | { |
---|
| 95 | unsigned int size, objectID, classID; |
---|
| 96 | size = *(unsigned int *)mem; |
---|
| 97 | objectID = *(unsigned int*)(mem+sizeof(unsigned int)); |
---|
| 98 | classID = *(unsigned int*)(mem+2*sizeof(unsigned int)); |
---|
[1856] | 99 | |
---|
[1758] | 100 | if(size==3*sizeof(unsigned int)){ //not our turn, dont do anything |
---|
| 101 | mem+=3*sizeof(unsigned int); |
---|
[1751] | 102 | return true; |
---|
[1758] | 103 | } |
---|
[1856] | 104 | |
---|
| 105 | orxonox::Identifier* id = ClassByID(classID); |
---|
[1735] | 106 | if(!id){ |
---|
| 107 | COUT(3) << "We could not identify a new object; classid: " << classID << " uint: " << (unsigned int)classID << " objectID: " << objectID << " size: " << size << std::endl; |
---|
[1751] | 108 | assert(0); |
---|
[1735] | 109 | return false; // most probably the gamestate is corrupted |
---|
| 110 | } |
---|
| 111 | orxonox::BaseObject *bo = id->fabricate(); |
---|
| 112 | Synchronisable *no = dynamic_cast<Synchronisable *>(bo); |
---|
| 113 | assert(no); |
---|
| 114 | no->objectID=objectID; |
---|
| 115 | no->classID=classID; |
---|
| 116 | COUT(3) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl; |
---|
| 117 | // update data and create object/entity... |
---|
| 118 | if( !no->updateData(mem, mode) ){ |
---|
| 119 | COUT(1) << "We couldn't update the object: " << objectID << std::endl; |
---|
| 120 | return false; |
---|
| 121 | } |
---|
| 122 | if( !no->create() ) |
---|
| 123 | { |
---|
| 124 | COUT(1) << "We couldn't manifest (create() ) the object: " << objectID << std::endl; |
---|
| 125 | return false; |
---|
| 126 | } |
---|
| 127 | return true; |
---|
| 128 | } |
---|
[1505] | 129 | |
---|
| 130 | /** |
---|
| 131 | * This function is used to register a variable to be synchronized |
---|
| 132 | * also counts the total datasize needed to save the variables |
---|
| 133 | * @param var pointer to the variable |
---|
| 134 | * @param size size of the datatype the variable consists of |
---|
| 135 | */ |
---|
[1534] | 136 | void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){ |
---|
[1505] | 137 | // create temporary synch.Var struct |
---|
| 138 | synchronisableVariable *temp = new synchronisableVariable; |
---|
| 139 | temp->size = size; |
---|
| 140 | temp->var = var; |
---|
[1639] | 141 | temp->mode = mode; |
---|
[1505] | 142 | temp->type = t; |
---|
[1534] | 143 | temp->callback = cb; |
---|
[1639] | 144 | COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; |
---|
[1505] | 145 | // increase datasize |
---|
| 146 | datasize+=sizeof(int)+size; |
---|
| 147 | //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; |
---|
| 148 | COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl; |
---|
| 149 | syncList->push_back(temp); |
---|
| 150 | } |
---|
[1856] | 151 | |
---|
[1735] | 152 | /** |
---|
| 153 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
[1751] | 154 | * Difference to the above function: |
---|
[1735] | 155 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
[1751] | 156 | * structure of the bitstream: |
---|
[1735] | 157 | * (var1_size,var1,var2_size,var2,...) |
---|
| 158 | * varx_size: size = sizeof(int) |
---|
| 159 | * varx: size = varx_size |
---|
| 160 | * @return data containing all variables and their sizes |
---|
| 161 | */ |
---|
[1751] | 162 | bool Synchronisable::getData(unsigned char*& mem, unsigned int id, int mode){ |
---|
[1735] | 163 | //std::cout << "inside getData" << std::endl; |
---|
| 164 | unsigned int tempsize = 0; |
---|
| 165 | if(mode==0x0) |
---|
| 166 | mode=state_; |
---|
| 167 | if(classID==0) |
---|
| 168 | COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl; |
---|
[1751] | 169 | this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this |
---|
[1735] | 170 | std::list<synchronisableVariable *>::iterator i; |
---|
| 171 | unsigned int size; |
---|
[1751] | 172 | size=getSize2(id, mode); |
---|
[1856] | 173 | |
---|
[1735] | 174 | // start copy header |
---|
| 175 | memcpy(mem, &size, sizeof(unsigned int)); |
---|
| 176 | mem+=sizeof(unsigned int); |
---|
| 177 | memcpy(mem, &(this->objectID), sizeof(unsigned int)); |
---|
| 178 | mem+=sizeof(unsigned int); |
---|
| 179 | memcpy(mem, &(this->classID), sizeof(unsigned int)); |
---|
| 180 | mem+=sizeof(unsigned int); |
---|
| 181 | tempsize+=12; |
---|
| 182 | // end copy header |
---|
[1856] | 183 | |
---|
[1751] | 184 | //if this tick is we dont synchronise, then abort now |
---|
| 185 | if(!isMyTick(id)) |
---|
| 186 | return true; |
---|
[1856] | 187 | |
---|
[1735] | 188 | COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl; |
---|
| 189 | // copy to location |
---|
| 190 | for(i=syncList->begin(); i!=syncList->end(); ++i){ |
---|
| 191 | //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); |
---|
| 192 | if( ((*i)->mode & mode) == 0 ){ |
---|
| 193 | COUT(5) << "not getting data: " << std::endl; |
---|
| 194 | continue; // this variable should only be received |
---|
| 195 | } |
---|
| 196 | switch((*i)->type){ |
---|
| 197 | case DATA: |
---|
| 198 | memcpy( (void *)(mem), (void*)((*i)->var), (*i)->size); |
---|
| 199 | mem+=(*i)->size; |
---|
| 200 | tempsize+=(*i)->size; |
---|
| 201 | break; |
---|
| 202 | case STRING: |
---|
| 203 | memcpy( (void *)(mem), (void *)&((*i)->size), sizeof(int) ); |
---|
| 204 | mem+=sizeof(int); |
---|
| 205 | const char *data = ( ( *(std::string *) (*i)->var).c_str()); |
---|
| 206 | memcpy( mem, (void*)data, (*i)->size); |
---|
| 207 | COUT(5) << "synchronisable: char: " << (const char *)(mem) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl; |
---|
| 208 | mem+=(*i)->size; |
---|
| 209 | tempsize+=(*i)->size + 4; |
---|
| 210 | break; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | assert(tempsize==size); |
---|
| 214 | return true; |
---|
| 215 | } |
---|
[1505] | 216 | |
---|
[1856] | 217 | |
---|
[1505] | 218 | /** |
---|
[1735] | 219 | * This function takes a syncData struct and takes it to update the variables |
---|
| 220 | * @param vars data of the variables |
---|
| 221 | * @return true/false |
---|
| 222 | */ |
---|
| 223 | bool Synchronisable::updateData(unsigned char*& mem, int mode){ |
---|
| 224 | unsigned char *data = mem; |
---|
| 225 | if(mode==0x0) |
---|
| 226 | mode=state_; |
---|
| 227 | std::list<synchronisableVariable *>::iterator i; |
---|
| 228 | if(syncList->empty()){ |
---|
| 229 | COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; |
---|
| 230 | return false; |
---|
| 231 | } |
---|
[1856] | 232 | |
---|
[1735] | 233 | // start extract header |
---|
| 234 | unsigned int objectID, classID, size; |
---|
| 235 | size = *(int *)mem; |
---|
| 236 | mem+=sizeof(size); |
---|
| 237 | objectID = *(int *)mem; |
---|
| 238 | mem+=sizeof(objectID); |
---|
| 239 | classID = *(int *)mem; |
---|
| 240 | mem+=sizeof(classID); |
---|
| 241 | // stop extract header |
---|
| 242 | assert(this->objectID==objectID); |
---|
| 243 | assert(this->classID==classID); |
---|
[1751] | 244 | if(size==3*sizeof(unsigned int)) //if true, only the header is available |
---|
| 245 | return true; |
---|
| 246 | //assert(0); |
---|
[1856] | 247 | |
---|
[1735] | 248 | COUT(5) << "Synchronisable: objectID " << objectID << ", classID " << classID << " size: " << size << " synchronising data" << std::endl; |
---|
| 249 | for(i=syncList->begin(); i!=syncList->end() && mem <= data+size; i++){ |
---|
| 250 | if( ((*i)->mode ^ mode) == 0 ){ |
---|
| 251 | COUT(5) << "synchronisable: not updating variable " << std::endl; |
---|
| 252 | continue; // this variable should only be set |
---|
| 253 | } |
---|
| 254 | COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl; |
---|
| 255 | bool callback=false; |
---|
| 256 | switch((*i)->type){ |
---|
| 257 | case DATA: |
---|
| 258 | if((*i)->callback) // check whether this variable changed (but only if callback was set) |
---|
| 259 | if(strncmp((char *)(*i)->var, (char *)mem, (*i)->size)!=0) |
---|
| 260 | callback=true; |
---|
| 261 | memcpy((void*)(*i)->var, mem, (*i)->size); |
---|
| 262 | mem+=(*i)->size; |
---|
| 263 | break; |
---|
| 264 | case STRING: |
---|
| 265 | (*i)->size = *(int *)mem; |
---|
| 266 | COUT(5) << "string size: " << (*i)->size << std::endl; |
---|
| 267 | mem+=sizeof(int); |
---|
| 268 | if((*i)->callback) // check whether this string changed |
---|
| 269 | if( *(std::string *)((*i)->var) != std::string((char *)mem) ) |
---|
| 270 | callback=true; |
---|
| 271 | *((std::string *)((*i)->var)) = std::string((const char*)mem); |
---|
| 272 | COUT(5) << "synchronisable: char: " << (const char*)mem << " string: " << std::string((const char*)mem) << std::endl; |
---|
| 273 | mem += (*i)->size; |
---|
| 274 | break; |
---|
| 275 | } |
---|
| 276 | // call the callback function, if defined |
---|
| 277 | if(callback && (*i)->callback) |
---|
| 278 | (*i)->callback->call(); |
---|
| 279 | } |
---|
| 280 | return true; |
---|
| 281 | } |
---|
[1505] | 282 | |
---|
| 283 | /** |
---|
| 284 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
| 285 | * @return amount of bytes |
---|
| 286 | */ |
---|
[1751] | 287 | int Synchronisable::getSize(unsigned int id, int mode){ |
---|
| 288 | if(!isMyTick(id)) |
---|
| 289 | return 0; |
---|
[1505] | 290 | int tsize=0; |
---|
| 291 | if(mode==0x0) |
---|
| 292 | mode=state_; |
---|
| 293 | std::list<synchronisableVariable *>::iterator i; |
---|
| 294 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
| 295 | if( ((*i)->mode & mode) == 0 ) |
---|
| 296 | continue; // this variable should only be received, so dont add its size to the send-size |
---|
| 297 | switch((*i)->type){ |
---|
| 298 | case DATA: |
---|
| 299 | tsize+=(*i)->size; |
---|
| 300 | break; |
---|
| 301 | case STRING: |
---|
| 302 | tsize+=sizeof(int); |
---|
| 303 | (*i)->size=((std::string *)(*i)->var)->length()+1; |
---|
| 304 | COUT(5) << "String size: " << (*i)->size << std::endl; |
---|
| 305 | tsize+=(*i)->size; |
---|
| 306 | break; |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | return tsize; |
---|
| 310 | } |
---|
[1639] | 311 | |
---|
[1735] | 312 | /** |
---|
| 313 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
| 314 | * @return amount of bytes |
---|
| 315 | */ |
---|
[1751] | 316 | int Synchronisable::getSize2(unsigned int id, int mode){ |
---|
| 317 | return sizeof(synchronisableHeader) + getSize( id, mode ); |
---|
[1735] | 318 | } |
---|
[1856] | 319 | |
---|
[1751] | 320 | /** |
---|
[1856] | 321 | * |
---|
| 322 | * @param id |
---|
| 323 | * @return |
---|
[1751] | 324 | */ |
---|
| 325 | bool Synchronisable::isMyTick(unsigned int id){ |
---|
| 326 | // return true; |
---|
| 327 | return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0); |
---|
| 328 | } |
---|
[1856] | 329 | |
---|
[1735] | 330 | bool Synchronisable::isMyData(unsigned char* mem) |
---|
| 331 | { |
---|
| 332 | unsigned int objectID, classID, size; |
---|
| 333 | size = *(int *)mem; |
---|
| 334 | mem+=sizeof(size); |
---|
| 335 | objectID = *(int *)mem; |
---|
| 336 | mem+=sizeof(objectID); |
---|
| 337 | classID = *(int *)mem; |
---|
| 338 | mem+=sizeof(classID); |
---|
[1856] | 339 | |
---|
[1735] | 340 | assert(classID == this->classID); |
---|
| 341 | return (objectID == this->objectID); |
---|
| 342 | } |
---|
[1856] | 343 | |
---|
[1751] | 344 | void Synchronisable::setObjectMode(int mode){ |
---|
| 345 | assert(mode==0x1 || mode==0x2 || mode==0x3); |
---|
| 346 | objectMode_=mode; |
---|
[1505] | 347 | } |
---|
| 348 | |
---|
[1639] | 349 | |
---|
[1505] | 350 | } |
---|