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