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