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 | * Oliver Scheuss |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Gamestate.h" |
---|
30 | |
---|
31 | #include <zlib.h> |
---|
32 | |
---|
33 | #include "util/Debug.h" |
---|
34 | #include "core/GameMode.h" |
---|
35 | #include "core/ObjectList.h" |
---|
36 | #include "network/synchronisable/Synchronisable.h" |
---|
37 | #include "network/GamestateHandler.h" |
---|
38 | |
---|
39 | namespace orxonox { |
---|
40 | |
---|
41 | namespace packet { |
---|
42 | |
---|
43 | #define GAMESTATE_START(data) (data + GamestateHeader::getSize()) |
---|
44 | |
---|
45 | #define PACKET_FLAG_GAMESTATE 0 //PacketFlag::Reliable |
---|
46 | |
---|
47 | inline bool memzero( uint8_t* data, uint32_t datalength) |
---|
48 | { |
---|
49 | uint64_t* d = (uint64_t*)data; |
---|
50 | |
---|
51 | for( unsigned int i=0; i<datalength/8; i++ ) |
---|
52 | { |
---|
53 | if( *(d+i) != 0 ) |
---|
54 | return false; |
---|
55 | } |
---|
56 | // now process the rest (when datalength isn't a multiple of 4) |
---|
57 | for( unsigned int j = 8*(datalength/8); j<datalength; j++ ) |
---|
58 | { |
---|
59 | if( *(data+j) != 0 ) |
---|
60 | return false; |
---|
61 | } |
---|
62 | return true; |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | Gamestate::Gamestate(): |
---|
67 | header_() |
---|
68 | { |
---|
69 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | Gamestate::Gamestate(uint8_t *data, unsigned int clientID): |
---|
74 | Packet(data, clientID), header_(data) |
---|
75 | { |
---|
76 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | Gamestate::Gamestate(uint8_t *data): |
---|
81 | header_(data) |
---|
82 | { |
---|
83 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
84 | data_ = data; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | Gamestate::Gamestate(const Gamestate& g) : |
---|
89 | Packet( *(Packet*)&g ), header_(this->data_), nrOfVariables_(0) |
---|
90 | { |
---|
91 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
92 | sizes_ = g.sizes_; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | Gamestate::~Gamestate() |
---|
97 | { |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | bool Gamestate::collectData(int id, uint8_t mode) |
---|
102 | { |
---|
103 | uint32_t tempsize=0, currentsize=0; |
---|
104 | assert(data_==0); |
---|
105 | uint32_t size = calcGamestateSize(id, mode); |
---|
106 | |
---|
107 | COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl; |
---|
108 | if(size==0) |
---|
109 | return false; |
---|
110 | data_ = new uint8_t[size + GamestateHeader::getSize()]; |
---|
111 | if(!data_) |
---|
112 | { |
---|
113 | COUT(2) << "GameStateManager: could not allocate memory" << std::endl; |
---|
114 | return false; |
---|
115 | } |
---|
116 | |
---|
117 | // tell the gamestate header where to store the data |
---|
118 | header_.setData(this->data_); |
---|
119 | |
---|
120 | //start collect data synchronisable by synchronisable |
---|
121 | uint8_t *mem = data_; // in this stream store all data of the variables and the headers of the synchronisable |
---|
122 | mem += GamestateHeader::getSize(); |
---|
123 | ObjectList<Synchronisable>::iterator it; |
---|
124 | for(it = ObjectList<Synchronisable>::begin(); it; ++it) |
---|
125 | { |
---|
126 | |
---|
127 | // tempsize=it->getSize(id, mode); |
---|
128 | |
---|
129 | tempsize = it->getData(mem, this->sizes_, id, mode); |
---|
130 | if ( tempsize != 0 ) |
---|
131 | dataVector_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) ); |
---|
132 | |
---|
133 | #ifndef NDEBUG |
---|
134 | if(currentsize+tempsize > size) |
---|
135 | { |
---|
136 | assert(0); // if we don't use multithreading this part shouldn't be neccessary |
---|
137 | // start allocate additional memory |
---|
138 | COUT(3) << "G.St.Man: need additional memory" << std::endl; |
---|
139 | ObjectList<Synchronisable>::iterator temp = it; |
---|
140 | uint32_t addsize=tempsize; |
---|
141 | while(++temp) |
---|
142 | addsize+=temp->getSize(id, mode); |
---|
143 | data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize); |
---|
144 | if(!data_) |
---|
145 | return false; |
---|
146 | size = currentsize+addsize; |
---|
147 | }// stop allocate additional memory |
---|
148 | #endif |
---|
149 | // if(!it->getData(mem, id, mode)) |
---|
150 | // return false; // mem pointer gets automatically increased because of call by reference |
---|
151 | // increase size counter by size of current synchronisable |
---|
152 | currentsize+=tempsize; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | //start write gamestate header |
---|
157 | header_.setDataSize( currentsize ); |
---|
158 | header_.setID( id ); |
---|
159 | header_.setBaseID( GAMESTATEID_INITIAL ); |
---|
160 | header_.setDiffed( false ); |
---|
161 | header_.setComplete( true ); |
---|
162 | header_.setCompressed( false ); |
---|
163 | //stop write gamestate header |
---|
164 | |
---|
165 | COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl; |
---|
166 | COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl; |
---|
167 | return true; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | bool Gamestate::spreadData(uint8_t mode) |
---|
172 | { |
---|
173 | COUT(4) << "processing gamestate with id " << header_.getID() << endl; |
---|
174 | assert(data_); |
---|
175 | assert(!header_.isCompressed()); |
---|
176 | uint8_t *mem=data_+GamestateHeader::getSize(); |
---|
177 | Synchronisable *s; |
---|
178 | |
---|
179 | // update the data of the objects we received |
---|
180 | while(mem < data_+GamestateHeader::getSize()+header_.getDataSize()) |
---|
181 | { |
---|
182 | SynchronisableHeader objectheader(mem); |
---|
183 | |
---|
184 | s = Synchronisable::getSynchronisable( objectheader.getObjectID() ); |
---|
185 | if(!s) |
---|
186 | { |
---|
187 | if (!GameMode::isMaster()) |
---|
188 | { |
---|
189 | Synchronisable::fabricate(mem, mode); |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | // COUT(4) << "not creating object of classid " << objectheader.getClassID() << endl; |
---|
194 | mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() ); |
---|
195 | } |
---|
196 | } |
---|
197 | else |
---|
198 | { |
---|
199 | // COUT(4) << "updating object of classid " << objectheader.getClassID() << endl; |
---|
200 | bool b = s->updateData(mem, mode); |
---|
201 | assert(b); |
---|
202 | } |
---|
203 | } |
---|
204 | // In debug mode, check first, whether there are no duplicate objectIDs |
---|
205 | #ifndef NDEBUG |
---|
206 | if(this->getID()%1000==1) |
---|
207 | { |
---|
208 | std::list<uint32_t> v1; |
---|
209 | ObjectList<Synchronisable>::iterator it; |
---|
210 | for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) |
---|
211 | { |
---|
212 | if (it->getObjectID() == OBJECTID_UNKNOWN) |
---|
213 | { |
---|
214 | if (it->objectMode_ != 0x0) |
---|
215 | { |
---|
216 | COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl; |
---|
217 | COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl; |
---|
218 | COUT(0) << "Objects class: " << it->getIdentifier()->getName() << std::endl; |
---|
219 | assert(false); |
---|
220 | } |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | std::list<uint32_t>::iterator it2; |
---|
225 | for (it2 = v1.begin(); it2 != v1.end(); ++it2) |
---|
226 | { |
---|
227 | if (it->getObjectID() == *it2) |
---|
228 | { |
---|
229 | COUT(0) << "Found duplicate objectIDs on the client!" << std::endl |
---|
230 | << "Are you sure you don't create a Sychnronisable objcect with 'new' \ |
---|
231 | that doesn't have objectMode = 0x0?" << std::endl; |
---|
232 | assert(false); |
---|
233 | } |
---|
234 | } |
---|
235 | v1.push_back(it->getObjectID()); |
---|
236 | } |
---|
237 | } |
---|
238 | } |
---|
239 | #endif |
---|
240 | return true; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | uint32_t Gamestate::getSize() const |
---|
245 | { |
---|
246 | assert(data_); |
---|
247 | if(header_.isCompressed()) |
---|
248 | return header_.getCompSize()+GamestateHeader::getSize(); |
---|
249 | else |
---|
250 | { |
---|
251 | return header_.getDataSize()+GamestateHeader::getSize(); |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | bool Gamestate::operator==(packet::Gamestate gs) |
---|
257 | { |
---|
258 | uint8_t *d1 = data_+GamestateHeader::getSize(); |
---|
259 | uint8_t *d2 = gs.data_+GamestateHeader::getSize(); |
---|
260 | GamestateHeader* h1 = new GamestateHeader(data_); |
---|
261 | GamestateHeader* h2 = new GamestateHeader(gs.data_); |
---|
262 | assert(h1->getDataSize() == h2->getDataSize()); |
---|
263 | assert(!isCompressed()); |
---|
264 | assert(!gs.isCompressed()); |
---|
265 | return memcmp(d1, d2, h1->getDataSize())==0; |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | bool Gamestate::process() |
---|
270 | { |
---|
271 | return GamestateHandler::addGamestate(this, getClientID()); |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | bool Gamestate::compressData() |
---|
276 | { |
---|
277 | assert(data_); |
---|
278 | assert(!header_.isCompressed()); |
---|
279 | uLongf buffer = (uLongf)(((header_.getDataSize() + 12)*1.01)+1); |
---|
280 | if(buffer==0) |
---|
281 | return false; |
---|
282 | |
---|
283 | uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()]; |
---|
284 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
285 | uint8_t *source = data_ + GamestateHeader::getSize(); |
---|
286 | int retval; |
---|
287 | retval = compress( dest, &buffer, source, (uLong)(header_.getDataSize()) ); |
---|
288 | switch ( retval ) |
---|
289 | { |
---|
290 | case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; |
---|
291 | case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false; |
---|
292 | case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false; |
---|
293 | case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false; |
---|
294 | } |
---|
295 | |
---|
296 | //copy and modify header |
---|
297 | GamestateHeader *temp = new GamestateHeader(data_); |
---|
298 | header_.setData(ndata); |
---|
299 | header_ = *temp; |
---|
300 | delete temp; |
---|
301 | //delete old data |
---|
302 | delete[] data_; |
---|
303 | //save new data |
---|
304 | data_ = ndata; |
---|
305 | header_.setCompSize( buffer ); |
---|
306 | header_.setCompressed( true ); |
---|
307 | COUT(4) << "gamestate compress datasize: " << header_.getDataSize() << " compsize: " << header_.getCompSize() << std::endl; |
---|
308 | return true; |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | bool Gamestate::decompressData() |
---|
313 | { |
---|
314 | assert(data_); |
---|
315 | assert(header_.isCompressed()); |
---|
316 | COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_.getID() << ", baseid: " << header_.getBaseID() << ", datasize: " << header_.getDataSize() << ", compsize: " << header_.getCompSize() << std::endl; |
---|
317 | uint32_t datasize = header_.getDataSize(); |
---|
318 | uint32_t compsize = header_.getCompSize(); |
---|
319 | uint32_t bufsize; |
---|
320 | bufsize = datasize; |
---|
321 | assert(bufsize!=0); |
---|
322 | uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()]; |
---|
323 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
324 | uint8_t *source = data_ + GamestateHeader::getSize(); |
---|
325 | int retval; |
---|
326 | uLongf length=bufsize; |
---|
327 | retval = uncompress( dest, &length, source, (uLong)compsize ); |
---|
328 | switch ( retval ) |
---|
329 | { |
---|
330 | case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; |
---|
331 | case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false; |
---|
332 | case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false; |
---|
333 | case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false; |
---|
334 | } |
---|
335 | |
---|
336 | //copy over the header |
---|
337 | GamestateHeader* temp = new GamestateHeader( data_ ); |
---|
338 | header_.setData(ndata); |
---|
339 | header_ = *temp; |
---|
340 | delete temp; |
---|
341 | |
---|
342 | if (this->bDataENetAllocated_) |
---|
343 | { |
---|
344 | // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will |
---|
345 | // deallocated it anyway. So data and packet stay together. |
---|
346 | this->bDataENetAllocated_ = false; |
---|
347 | } |
---|
348 | else |
---|
349 | { |
---|
350 | // We allocated the memory in the first place (unlikely). So we destroy the old data |
---|
351 | // and overwrite it with the new decompressed data. |
---|
352 | delete[] this->data_; |
---|
353 | } |
---|
354 | |
---|
355 | //set new pointers |
---|
356 | data_ = ndata; |
---|
357 | header_.setCompressed( false ); |
---|
358 | assert(header_.getDataSize()==datasize); |
---|
359 | assert(header_.getCompSize()==compsize); |
---|
360 | return true; |
---|
361 | } |
---|
362 | |
---|
363 | |
---|
364 | inline void /*Gamestate::*/diffObject( uint8_t*& newDataPtr, uint8_t*& origDataPtr, uint8_t*& baseDataPtr, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ) |
---|
365 | { |
---|
366 | // COUT(4) << "dodiff" << endl; |
---|
367 | // if(baseOffset==0) |
---|
368 | // { |
---|
369 | // assert(origOffset==0); |
---|
370 | // } |
---|
371 | assert( objectHeader.getDataSize() == SynchronisableHeader(baseDataPtr).getDataSize() ); |
---|
372 | |
---|
373 | uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData |
---|
374 | // Check whether the whole object stayed the same |
---|
375 | if( memcmp( origDataPtr+objectOffset, baseDataPtr+objectOffset, objectHeader.getDataSize()) == 0 ) |
---|
376 | { |
---|
377 | // COUT(4) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
378 | origDataPtr += objectOffset + objectHeader.getDataSize(); // skip the whole object |
---|
379 | baseDataPtr += objectOffset + objectHeader.getDataSize(); |
---|
380 | sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); |
---|
381 | } |
---|
382 | else |
---|
383 | { |
---|
384 | // if( Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() == "Bot" ) |
---|
385 | // COUT(0) << "blub" << endl; |
---|
386 | // COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl; |
---|
387 | // COUT(4) << "diff " << h.getObjectID() << ":"; |
---|
388 | // Now start to diff the Object |
---|
389 | SynchronisableHeaderLight newObjectHeader(newDataPtr); |
---|
390 | newObjectHeader = objectHeader; // copy over the objectheader |
---|
391 | VariableID variableID = 0; |
---|
392 | uint32_t diffedObjectOffset = SynchronisableHeaderLight::getSize(); |
---|
393 | // iterate through all variables |
---|
394 | while( objectOffset < objectHeader.getDataSize()+SynchronisableHeader::getSize() ) |
---|
395 | { |
---|
396 | // check whether variable changed and write id and copy over variable to the new stream |
---|
397 | // otherwise skip variable |
---|
398 | // assert(sizes != this->sizes_.end()); |
---|
399 | uint32_t varSize = *sizes; |
---|
400 | assert( varSize == Synchronisable::getSynchronisable(objectHeader.getObjectID())->getVarSize(variableID) ); |
---|
401 | if ( varSize != 0 ) |
---|
402 | { |
---|
403 | if ( memcmp(origDataPtr+objectOffset, baseDataPtr+objectOffset, varSize) != 0 ) |
---|
404 | { |
---|
405 | // COUT(4) << "copy variable" << endl; |
---|
406 | *(VariableID*)(newDataPtr+diffedObjectOffset) = variableID; // copy over the variableID |
---|
407 | diffedObjectOffset += sizeof(VariableID); |
---|
408 | memcpy( newDataPtr+diffedObjectOffset, origDataPtr+objectOffset, varSize ); |
---|
409 | diffedObjectOffset += varSize; |
---|
410 | objectOffset += varSize; |
---|
411 | } |
---|
412 | else |
---|
413 | { |
---|
414 | // COUT(4) << "skip variable" << endl; |
---|
415 | objectOffset += varSize; |
---|
416 | } |
---|
417 | } |
---|
418 | // else |
---|
419 | // COUT(4) << "varsize 0" << endl; |
---|
420 | |
---|
421 | ++variableID; |
---|
422 | ++sizes; |
---|
423 | } |
---|
424 | |
---|
425 | if( Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() != variableID ) |
---|
426 | sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() - variableID; |
---|
427 | // COUT(4) << endl; |
---|
428 | |
---|
429 | newObjectHeader.setDiffed(true); |
---|
430 | newObjectHeader.setDataSize(diffedObjectOffset-SynchronisableHeaderLight::getSize()); |
---|
431 | assert(objectOffset == objectHeader.getDataSize()+SynchronisableHeader::getSize()); |
---|
432 | assert(newObjectHeader.getDataSize()>0); |
---|
433 | origDataPtr += objectOffset; |
---|
434 | // baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData; |
---|
435 | //baseOffset += objectOffset; |
---|
436 | // SynchronisableHeader htemp(temp); |
---|
437 | // baseOffset += SynchronisableHeader::getSize() + htemp.getDataSize(); |
---|
438 | // { |
---|
439 | // SynchronisableHeader htemp2( baseData+(temp-baseData+objectOffset) ); |
---|
440 | // if( baseData+(temp-baseData+objectOffset) < baseData+baseLength ) |
---|
441 | // { |
---|
442 | // assert(htemp2.getClassID()<500); |
---|
443 | // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); |
---|
444 | // assert(htemp2.isDiffed()==false); |
---|
445 | // } |
---|
446 | // } |
---|
447 | baseDataPtr += objectOffset; |
---|
448 | newDataPtr += diffedObjectOffset; |
---|
449 | } |
---|
450 | } |
---|
451 | |
---|
452 | inline void /*Gamestate::*/copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ) |
---|
453 | { |
---|
454 | // COUT(4) << "docopy" << endl; |
---|
455 | // Just copy over the whole Object |
---|
456 | memcpy( newData, origData, objectHeader.getDataSize()+SynchronisableHeader::getSize() ); |
---|
457 | newData += objectHeader.getDataSize()+SynchronisableHeader::getSize(); |
---|
458 | origData += objectHeader.getDataSize()+SynchronisableHeader::getSize(); |
---|
459 | // SynchronisableHeader baseHeader( baseData ); |
---|
460 | // baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize(); |
---|
461 | // COUT(4) << "copy " << h.getObjectID() << endl; |
---|
462 | // COUT(4) << "copy " << h.getObjectID() << ":"; |
---|
463 | sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); |
---|
464 | // for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i ) |
---|
465 | // { |
---|
466 | // // COUT(4) << " " << *sizes; |
---|
467 | // ++sizes; |
---|
468 | // } |
---|
469 | // COUT(4) << endl; |
---|
470 | } |
---|
471 | |
---|
472 | inline bool findObject(uint8_t*& dataPtr, uint8_t* endPtr, SynchronisableHeader& objectHeader) |
---|
473 | { |
---|
474 | // Some assertions to make sure the dataPtr is valid (pointing to a SynchronisableHeader) |
---|
475 | { |
---|
476 | SynchronisableHeader htemp2(dataPtr); |
---|
477 | assert(htemp2.getClassID()<500); |
---|
478 | assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); |
---|
479 | assert(htemp2.isDiffed()==false); |
---|
480 | } |
---|
481 | uint32_t objectID = objectHeader.getObjectID(); |
---|
482 | while ( dataPtr < endPtr ) |
---|
483 | { |
---|
484 | SynchronisableHeader htemp(dataPtr); |
---|
485 | assert( htemp.getDataSize()!=0 ); |
---|
486 | if ( htemp.getObjectID() == objectID ) |
---|
487 | { |
---|
488 | assert( objectHeader.getClassID() == htemp.getClassID() ); |
---|
489 | assert( objectHeader.getCreatorID() == htemp.getCreatorID() ); |
---|
490 | return true; |
---|
491 | } |
---|
492 | { |
---|
493 | if( dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize() < endPtr ) |
---|
494 | { |
---|
495 | SynchronisableHeader htemp2(dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize()); |
---|
496 | assert(htemp2.getClassID()<500); |
---|
497 | assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); |
---|
498 | assert(htemp2.isDiffed()==false); |
---|
499 | } |
---|
500 | } |
---|
501 | dataPtr += htemp.getDataSize()+SynchronisableHeader::getSize(); |
---|
502 | |
---|
503 | } |
---|
504 | assert(dataPtr == endPtr); |
---|
505 | |
---|
506 | return false; |
---|
507 | } |
---|
508 | |
---|
509 | Gamestate* Gamestate::diffVariables(Gamestate *base) |
---|
510 | { |
---|
511 | assert(this && base); assert(data_ && base->data_); |
---|
512 | assert(!header_.isCompressed() && !base->header_.isCompressed()); |
---|
513 | assert(!header_.isDiffed()); |
---|
514 | assert( header_.getDataSize() && base->header_.getDataSize() ); |
---|
515 | |
---|
516 | |
---|
517 | // *** first do a raw diff of the two gamestates |
---|
518 | |
---|
519 | uint8_t *baseDataPtr = GAMESTATE_START(base->data_); |
---|
520 | uint8_t *origDataPtr = GAMESTATE_START(this->data_); |
---|
521 | uint8_t *origDataEnd = origDataPtr + header_.getDataSize(); |
---|
522 | uint8_t *baseDataEnd = baseDataPtr + base->header_.getDataSize(); |
---|
523 | // uint32_t origLength = header_.getDataSize(); |
---|
524 | // uint32_t baseLength = base->header_.getDataSize(); |
---|
525 | |
---|
526 | // Allocate new space for diffed gamestate |
---|
527 | uint32_t newDataSize = header_.getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_; |
---|
528 | uint8_t *newData = new uint8_t[newDataSize]; // this is the maximum size needed in the worst case |
---|
529 | uint8_t *destDataPtr = GAMESTATE_START(newData); |
---|
530 | |
---|
531 | std::vector<uint32_t>::iterator sizesIt = this->sizes_.begin(); |
---|
532 | |
---|
533 | while( origDataPtr < origDataEnd ) |
---|
534 | { |
---|
535 | //iterate through all objects |
---|
536 | |
---|
537 | SynchronisableHeader origHeader(origDataPtr); |
---|
538 | |
---|
539 | // Find (if possible) the current object in the datastream of the old gamestate |
---|
540 | // Start at the current offset position |
---|
541 | if(baseDataPtr == baseDataEnd) |
---|
542 | baseDataPtr = GAMESTATE_START(base->data_); |
---|
543 | uint8_t* oldBaseDataPtr = baseDataPtr; |
---|
544 | |
---|
545 | assert(baseDataPtr < baseDataEnd); |
---|
546 | assert(destDataPtr < newData + newDataSize); |
---|
547 | assert(sizesIt != this->sizes_.end()); |
---|
548 | |
---|
549 | bool diffedObject = false; |
---|
550 | if( findObject(baseDataPtr, baseDataEnd, origHeader) ) |
---|
551 | { |
---|
552 | if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() ) |
---|
553 | { |
---|
554 | // COUT(4) << "diffing object in order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
555 | diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
556 | diffedObject = true; |
---|
557 | } |
---|
558 | else |
---|
559 | { |
---|
560 | // COUT(4) << "copy object because of different data sizes (1): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
561 | copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
562 | assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); |
---|
563 | } |
---|
564 | |
---|
565 | } |
---|
566 | else |
---|
567 | { |
---|
568 | assert( baseDataPtr == baseDataEnd ); |
---|
569 | baseDataPtr = GAMESTATE_START(base->data_); |
---|
570 | if( findObject(baseDataPtr, oldBaseDataPtr, origHeader) ) |
---|
571 | { |
---|
572 | if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() ) |
---|
573 | { |
---|
574 | // COUT(4) << "diffing object out of order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
575 | diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
576 | diffedObject = true; |
---|
577 | } |
---|
578 | else |
---|
579 | { |
---|
580 | // COUT(4) << "copy object because of different data sizes (2): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
581 | copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
582 | assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); |
---|
583 | } |
---|
584 | } |
---|
585 | else |
---|
586 | { |
---|
587 | // COUT(4) << "copy object: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
588 | assert(baseDataPtr == oldBaseDataPtr); |
---|
589 | copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
590 | assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); |
---|
591 | } |
---|
592 | } |
---|
593 | } |
---|
594 | assert(sizesIt==this->sizes_.end()); |
---|
595 | |
---|
596 | |
---|
597 | Gamestate *g = new Gamestate(newData, getClientID()); |
---|
598 | (g->header_) = header_; |
---|
599 | g->header_.setBaseID( base->getID() ); |
---|
600 | g->header_.setDataSize(destDataPtr - newData - GamestateHeader::getSize()); |
---|
601 | g->flags_=flags_; |
---|
602 | g->packetDirection_ = packetDirection_; |
---|
603 | assert(!g->isCompressed()); |
---|
604 | return g; |
---|
605 | } |
---|
606 | |
---|
607 | |
---|
608 | /*Gamestate* Gamestate::diffData(Gamestate *base) |
---|
609 | { |
---|
610 | assert(this && base); assert(data_ && base->data_); |
---|
611 | assert(!header_.isCompressed() && !base->header_.isCompressed()); |
---|
612 | assert(!header_.isDiffed()); |
---|
613 | |
---|
614 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
615 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
616 | uint32_t dest_length = header_.getDataSize(); |
---|
617 | |
---|
618 | if(dest_length==0) |
---|
619 | return NULL; |
---|
620 | |
---|
621 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
622 | uint8_t *dest = GAMESTATE_START(ndata); |
---|
623 | |
---|
624 | rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); |
---|
625 | #ifndef NDEBUG |
---|
626 | uint8_t *dest2 = new uint8_t[dest_length]; |
---|
627 | rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() ); |
---|
628 | assert( memcmp( dest2, gs, dest_length) == 0 ); |
---|
629 | delete dest2; |
---|
630 | #endif |
---|
631 | |
---|
632 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
633 | assert(g->header_); |
---|
634 | *(g->header_) = *header_; |
---|
635 | g->header_.setDiffed( true ); |
---|
636 | g->header_.setBaseID( base->getID() ); |
---|
637 | g->flags_=flags_; |
---|
638 | g->packetDirection_ = packetDirection_; |
---|
639 | assert(g->isDiffed()); |
---|
640 | assert(!g->isCompressed()); |
---|
641 | return g; |
---|
642 | } |
---|
643 | |
---|
644 | |
---|
645 | Gamestate* Gamestate::undiff(Gamestate *base) |
---|
646 | { |
---|
647 | assert(this && base); assert(data_ && base->data_); |
---|
648 | assert(!header_.isCompressed() && !base->header_.isCompressed()); |
---|
649 | assert(header_.isDiffed()); |
---|
650 | |
---|
651 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
652 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
653 | uint32_t dest_length = header_.getDataSize(); |
---|
654 | |
---|
655 | if(dest_length==0) |
---|
656 | return NULL; |
---|
657 | |
---|
658 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
659 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
660 | |
---|
661 | rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); |
---|
662 | |
---|
663 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
664 | assert(g->header_); |
---|
665 | *(g->header_) = *header_; |
---|
666 | g->header_.setDiffed( false ); |
---|
667 | g->flags_=flags_; |
---|
668 | g->packetDirection_ = packetDirection_; |
---|
669 | assert(!g->isDiffed()); |
---|
670 | assert(!g->isCompressed()); |
---|
671 | return g; |
---|
672 | } |
---|
673 | |
---|
674 | |
---|
675 | void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength) |
---|
676 | { |
---|
677 | uint64_t* gd = (uint64_t*)data; |
---|
678 | uint64_t* bd = (uint64_t*)basedata; |
---|
679 | uint64_t* nd = (uint64_t*)newdata; |
---|
680 | |
---|
681 | unsigned int i; |
---|
682 | for( i=0; i<datalength/8; i++ ) |
---|
683 | { |
---|
684 | if( i<baselength/8 ) |
---|
685 | *(nd+i) = *(gd+i) ^ *(bd+i); // xor the data |
---|
686 | else |
---|
687 | *(nd+i) = *(gd+i); // just copy over the data |
---|
688 | } |
---|
689 | unsigned int j; |
---|
690 | // now process the rest (when datalength isn't a multiple of 4) |
---|
691 | for( j = 8*(datalength/8); j<datalength; j++ ) |
---|
692 | { |
---|
693 | if( j<baselength ) |
---|
694 | *(newdata+j) = *(data+j) ^ *(basedata+j); // xor |
---|
695 | else |
---|
696 | *(newdata+j) = *(data+j); // just copy |
---|
697 | } |
---|
698 | assert(j==datalength); |
---|
699 | }*/ |
---|
700 | |
---|
701 | |
---|
702 | /*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ |
---|
703 | assert(data_); |
---|
704 | std::list<obj>::iterator it; |
---|
705 | |
---|
706 | // allocate memory for new data |
---|
707 | uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()]; |
---|
708 | // create a gamestate out of it |
---|
709 | Gamestate *gs = new Gamestate(gdata); |
---|
710 | uint8_t *newdata = gdata + GamestateHeader::getSize(); |
---|
711 | uint8_t *origdata = GAMESTATE_START(data_); |
---|
712 | |
---|
713 | //copy the GamestateHeader |
---|
714 | assert(gs->header_); |
---|
715 | *(gs->header_) = *header_; |
---|
716 | |
---|
717 | uint32_t objectOffset; |
---|
718 | unsigned int objectsize, destsize=0; |
---|
719 | // TODO: Why is this variable not used? |
---|
720 | //Synchronisable *object; |
---|
721 | |
---|
722 | //call TrafficControl |
---|
723 | TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ ); |
---|
724 | |
---|
725 | //copy in the zeros |
---|
726 | // std::list<obj>::iterator itt; |
---|
727 | // COUT(0) << "myvector contains:"; |
---|
728 | // for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ ) |
---|
729 | // COUT(0) << " " << (*itt).objID; |
---|
730 | // COUT(0) << endl; |
---|
731 | for(it=dataVector_.begin(); it!=dataVector_.end();){ |
---|
732 | SynchronisableHeader oldobjectheader(origdata); |
---|
733 | SynchronisableHeader newobjectheader(newdata); |
---|
734 | if ( (*it).objSize == 0 ) |
---|
735 | { |
---|
736 | ++it; |
---|
737 | continue; |
---|
738 | } |
---|
739 | objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize(); |
---|
740 | objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader |
---|
741 | if ( (*it).objID == oldobjectheader.getObjectID() ){ |
---|
742 | memcpy(newdata, origdata, objectsize); |
---|
743 | ++it; |
---|
744 | }else{ |
---|
745 | newobjectheader = oldobjectheader; |
---|
746 | memset(newdata+objectOffset, 0, objectsize-objectOffset); |
---|
747 | } |
---|
748 | newdata += objectsize; |
---|
749 | origdata += objectsize; |
---|
750 | destsize += objectsize; |
---|
751 | } |
---|
752 | #ifndef NDEBUG |
---|
753 | uint32_t origsize = destsize; |
---|
754 | while ( origsize < header_.getDataSize() ) |
---|
755 | { |
---|
756 | SynchronisableHeader oldobjectheader(origdata); |
---|
757 | objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize(); |
---|
758 | origdata += objectsize; |
---|
759 | origsize += objectsize; |
---|
760 | } |
---|
761 | assert(origsize==header_.getDataSize()); |
---|
762 | assert(destsize!=0); |
---|
763 | #endif |
---|
764 | gs->header_.setDataSize( destsize ); |
---|
765 | return gs; |
---|
766 | }*/ |
---|
767 | |
---|
768 | |
---|
769 | uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode) |
---|
770 | { |
---|
771 | uint32_t size = 0; |
---|
772 | uint32_t nrOfVariables = 0; |
---|
773 | // get the start of the Synchronisable list |
---|
774 | ObjectList<Synchronisable>::iterator it; |
---|
775 | // get total size of gamestate |
---|
776 | for(it = ObjectList<Synchronisable>::begin(); it; ++it){ |
---|
777 | size+=it->getSize(id, mode); // size of the actual data of the synchronisable |
---|
778 | nrOfVariables += it->getNrOfVariables(); |
---|
779 | } |
---|
780 | // COUT(0) << "allocating " << nrOfVariables << " ints" << endl; |
---|
781 | this->sizes_.reserve(nrOfVariables); |
---|
782 | return size; |
---|
783 | } |
---|
784 | |
---|
785 | |
---|
786 | } //namespace packet |
---|
787 | } //namespace orxonox |
---|