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