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