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