[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 | |
---|
[8858] | 33 | #include "util/Output.h" |
---|
[8373] | 34 | #include "util/OrxAssert.h" |
---|
[9667] | 35 | #include "core/CoreIncludes.h" |
---|
[2896] | 36 | #include "core/GameMode.h" |
---|
[9667] | 37 | #include "core/object/ObjectList.h" |
---|
[3214] | 38 | #include "network/synchronisable/Synchronisable.h" |
---|
| 39 | #include "network/GamestateHandler.h" |
---|
[7801] | 40 | #include "network/Host.h" |
---|
[1701] | 41 | |
---|
[2171] | 42 | namespace orxonox { |
---|
[1701] | 43 | |
---|
| 44 | namespace packet { |
---|
| 45 | |
---|
[2662] | 46 | #define GAMESTATE_START(data) (data + GamestateHeader::getSize()) |
---|
[1740] | 47 | |
---|
[7801] | 48 | // #define PACKET_FLAG_GAMESTATE PacketFlag::Reliable |
---|
| 49 | #define PACKET_FLAG_GAMESTATE 0 |
---|
[2087] | 50 | |
---|
[7163] | 51 | inline bool memzero( uint8_t* data, uint32_t datalength) |
---|
| 52 | { |
---|
| 53 | uint64_t* d = (uint64_t*)data; |
---|
[2662] | 54 | |
---|
[7163] | 55 | for( unsigned int i=0; i<datalength/8; i++ ) |
---|
| 56 | { |
---|
| 57 | if( *(d+i) != 0 ) |
---|
| 58 | return false; |
---|
| 59 | } |
---|
| 60 | // now process the rest (when datalength isn't a multiple of 4) |
---|
| 61 | for( unsigned int j = 8*(datalength/8); j<datalength; j++ ) |
---|
| 62 | { |
---|
| 63 | if( *(data+j) != 0 ) |
---|
| 64 | return false; |
---|
| 65 | } |
---|
| 66 | return true; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | Gamestate::Gamestate(): |
---|
[7801] | 71 | header_() |
---|
[1701] | 72 | { |
---|
[1907] | 73 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[1701] | 74 | } |
---|
| 75 | |
---|
[7163] | 76 | |
---|
[1907] | 77 | Gamestate::Gamestate(uint8_t *data, unsigned int clientID): |
---|
[7801] | 78 | Packet(data, clientID), header_(data) |
---|
[1701] | 79 | { |
---|
[1907] | 80 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[1701] | 81 | } |
---|
| 82 | |
---|
[7163] | 83 | |
---|
[7801] | 84 | Gamestate::Gamestate(uint8_t *data): |
---|
| 85 | header_(data) |
---|
[1907] | 86 | { |
---|
| 87 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[7163] | 88 | data_ = data; |
---|
[1907] | 89 | } |
---|
[1701] | 90 | |
---|
[7163] | 91 | |
---|
[2662] | 92 | Gamestate::Gamestate(const Gamestate& g) : |
---|
[7801] | 93 | Packet( *(Packet*)&g ), header_(this->data_), nrOfVariables_(0) |
---|
[2662] | 94 | { |
---|
| 95 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[7163] | 96 | sizes_ = g.sizes_; |
---|
[2662] | 97 | } |
---|
[1907] | 98 | |
---|
[2662] | 99 | |
---|
[1701] | 100 | Gamestate::~Gamestate() |
---|
| 101 | { |
---|
| 102 | } |
---|
| 103 | |
---|
[7163] | 104 | |
---|
[2171] | 105 | bool Gamestate::collectData(int id, uint8_t mode) |
---|
[1701] | 106 | { |
---|
[2662] | 107 | uint32_t tempsize=0, currentsize=0; |
---|
[11071] | 108 | assert(data_==nullptr); |
---|
[2662] | 109 | uint32_t size = calcGamestateSize(id, mode); |
---|
[1740] | 110 | |
---|
[8858] | 111 | orxout(verbose_more, context::packets) << "G.ST.Man: producing gamestate with id: " << id << endl; |
---|
[1701] | 112 | if(size==0) |
---|
| 113 | return false; |
---|
[2662] | 114 | data_ = new uint8_t[size + GamestateHeader::getSize()]; |
---|
[7163] | 115 | if(!data_) |
---|
| 116 | { |
---|
[8858] | 117 | orxout(internal_warning, context::packets) << "GameStateManager: could not allocate memory" << endl; |
---|
[1701] | 118 | return false; |
---|
| 119 | } |
---|
[6417] | 120 | |
---|
[7801] | 121 | // tell the gamestate header where to store the data |
---|
| 122 | header_.setData(this->data_); |
---|
[2087] | 123 | |
---|
[1701] | 124 | //start collect data synchronisable by synchronisable |
---|
[7163] | 125 | uint8_t *mem = data_; // in this stream store all data of the variables and the headers of the synchronisable |
---|
[2662] | 126 | mem += GamestateHeader::getSize(); |
---|
[2171] | 127 | ObjectList<Synchronisable>::iterator it; |
---|
[11071] | 128 | for(it = ObjectList<Synchronisable>().begin(); it; ++it) |
---|
[7163] | 129 | { |
---|
[6417] | 130 | |
---|
[3084] | 131 | // tempsize=it->getSize(id, mode); |
---|
| 132 | |
---|
[7163] | 133 | tempsize = it->getData(mem, this->sizes_, id, mode); |
---|
[3084] | 134 | if ( tempsize != 0 ) |
---|
[11071] | 135 | dataVector_.emplace_back(it->getObjectID(), it->getContextID(), tempsize, mem-data_); |
---|
[6417] | 136 | |
---|
[2662] | 137 | #ifndef NDEBUG |
---|
[7163] | 138 | if(currentsize+tempsize > size) |
---|
| 139 | { |
---|
[2171] | 140 | assert(0); // if we don't use multithreading this part shouldn't be neccessary |
---|
[1701] | 141 | // start allocate additional memory |
---|
[8858] | 142 | orxout(internal_info, context::packets) << "Gamestate: need additional memory" << endl; |
---|
[2171] | 143 | ObjectList<Synchronisable>::iterator temp = it; |
---|
[2662] | 144 | uint32_t addsize=tempsize; |
---|
[1701] | 145 | while(++temp) |
---|
[1907] | 146 | addsize+=temp->getSize(id, mode); |
---|
[2662] | 147 | data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize); |
---|
[1701] | 148 | if(!data_) |
---|
| 149 | return false; |
---|
| 150 | size = currentsize+addsize; |
---|
| 151 | }// stop allocate additional memory |
---|
[2662] | 152 | #endif |
---|
[3084] | 153 | // if(!it->getData(mem, id, mode)) |
---|
| 154 | // return false; // mem pointer gets automatically increased because of call by reference |
---|
[1701] | 155 | // increase size counter by size of current synchronisable |
---|
| 156 | currentsize+=tempsize; |
---|
| 157 | } |
---|
[1740] | 158 | |
---|
| 159 | |
---|
[1701] | 160 | //start write gamestate header |
---|
[7801] | 161 | header_.setDataSize( currentsize ); |
---|
[8327] | 162 | header_.setCompSize( 0 ); |
---|
[7801] | 163 | header_.setID( id ); |
---|
| 164 | header_.setBaseID( GAMESTATEID_INITIAL ); |
---|
| 165 | header_.setDiffed( false ); |
---|
| 166 | header_.setComplete( true ); |
---|
| 167 | header_.setCompressed( false ); |
---|
[1701] | 168 | //stop write gamestate header |
---|
[1740] | 169 | |
---|
[8858] | 170 | orxout(verbose_more, context::packets) << "Gamestate: Gamestate size: " << currentsize << endl; |
---|
| 171 | orxout(verbose_more, context::packets) << "Gamestate: 'estimated' (and corrected) Gamestate size: " << size << endl; |
---|
[1701] | 172 | return true; |
---|
| 173 | } |
---|
| 174 | |
---|
[7163] | 175 | |
---|
[2171] | 176 | bool Gamestate::spreadData(uint8_t mode) |
---|
[1701] | 177 | { |
---|
[8858] | 178 | orxout(verbose_more, context::packets) << "processing gamestate with id " << header_.getID() << endl; |
---|
[1751] | 179 | assert(data_); |
---|
[7801] | 180 | assert(!header_.isCompressed()); |
---|
[2662] | 181 | uint8_t *mem=data_+GamestateHeader::getSize(); |
---|
[1907] | 182 | Synchronisable *s; |
---|
[7801] | 183 | |
---|
[1907] | 184 | // update the data of the objects we received |
---|
[7801] | 185 | while(mem < data_+GamestateHeader::getSize()+header_.getDataSize()) |
---|
[7163] | 186 | { |
---|
[2662] | 187 | SynchronisableHeader objectheader(mem); |
---|
[1701] | 188 | |
---|
[2662] | 189 | s = Synchronisable::getSynchronisable( objectheader.getObjectID() ); |
---|
[1907] | 190 | if(!s) |
---|
[1701] | 191 | { |
---|
[2896] | 192 | if (!GameMode::isMaster()) |
---|
[2662] | 193 | { |
---|
| 194 | Synchronisable::fabricate(mem, mode); |
---|
| 195 | } |
---|
| 196 | else |
---|
| 197 | { |
---|
[8858] | 198 | // orxout(verbose, context::packets) << "not creating object of classid " << objectheader.getClassID() << endl; |
---|
[7163] | 199 | mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() ); |
---|
[2662] | 200 | } |
---|
[1701] | 201 | } |
---|
[1907] | 202 | else |
---|
| 203 | { |
---|
[8858] | 204 | // orxout(verbose, context::packets) << "updating object of classid " << objectheader.getClassID() << endl; |
---|
[8394] | 205 | OrxVerify(s->updateData(mem, mode), "ERROR: could not update Synchronisable with Gamestate data"); |
---|
[1907] | 206 | } |
---|
[1701] | 207 | } |
---|
[7844] | 208 | assert((uintptr_t)(mem-data_) == GamestateHeader::getSize()+header_.getDataSize()); |
---|
[7801] | 209 | |
---|
[2662] | 210 | // In debug mode, check first, whether there are no duplicate objectIDs |
---|
| 211 | #ifndef NDEBUG |
---|
[7163] | 212 | if(this->getID()%1000==1) |
---|
| 213 | { |
---|
[3084] | 214 | std::list<uint32_t> v1; |
---|
[11071] | 215 | for (Synchronisable* synchronisable : ObjectList<Synchronisable>()) |
---|
[7163] | 216 | { |
---|
[11071] | 217 | if (synchronisable->getObjectID() == OBJECTID_UNKNOWN) |
---|
[7163] | 218 | { |
---|
[11071] | 219 | if (synchronisable->objectMode_ != 0x0) |
---|
[7163] | 220 | { |
---|
[8858] | 221 | orxout(user_error, context::packets) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << endl; |
---|
| 222 | orxout(user_error, context::packets) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << endl; |
---|
[11071] | 223 | orxout(user_error, context::packets) << "Objects class: " << synchronisable->getIdentifier()->getName() << endl; |
---|
[3084] | 224 | assert(false); |
---|
| 225 | } |
---|
[2662] | 226 | } |
---|
[7163] | 227 | else |
---|
| 228 | { |
---|
[11071] | 229 | for (uint32_t id : v1) |
---|
[7163] | 230 | { |
---|
[11071] | 231 | if (synchronisable->getObjectID() == id) |
---|
[7163] | 232 | { |
---|
[8858] | 233 | orxout(user_error, context::packets) << "Found duplicate objectIDs on the client!" << endl |
---|
| 234 | << "Are you sure you don't create a Sychnronisable objcect with 'new' \ |
---|
| 235 | that doesn't have objectMode = 0x0?" << endl; |
---|
[3084] | 236 | assert(false); |
---|
| 237 | } |
---|
[2662] | 238 | } |
---|
[11071] | 239 | v1.push_back(synchronisable->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(); |
---|
[8394] | 264 | GamestateHeader h1(data_); |
---|
| 265 | GamestateHeader h2(gs.data_); |
---|
| 266 | assert(h1.getDataSize() == h2.getDataSize()); |
---|
[1751] | 267 | assert(!isCompressed()); |
---|
| 268 | assert(!gs.isCompressed()); |
---|
[8394] | 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 | { |
---|
[8858] | 294 | case Z_OK: orxout(verbose_more, context::packets) << "G.St.Man: compress: successfully compressed" << endl; break; |
---|
| 295 | case Z_MEM_ERROR: orxout(internal_error, context::packets) << "G.St.Man: compress: not enough memory available in gamestate.compress" << endl; return false; |
---|
| 296 | case Z_BUF_ERROR: orxout(internal_warning, context::packets) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << endl; return false; |
---|
| 297 | case Z_DATA_ERROR: orxout(internal_warning, context::packets) << "G.St.Man: compress: data corrupted in gamestate.compress" << 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 ); |
---|
[8858] | 311 | orxout(verbose, context::packets) << "gamestate compress datasize: " << header_.getDataSize() << " compsize: " << header_.getCompSize() << endl; |
---|
[1701] | 312 | return true; |
---|
| 313 | } |
---|
[7163] | 314 | |
---|
| 315 | |
---|
[1701] | 316 | bool Gamestate::decompressData() |
---|
| 317 | { |
---|
[2662] | 318 | assert(data_); |
---|
[7801] | 319 | assert(header_.isCompressed()); |
---|
[8858] | 320 | orxout(verbose, context::packets) << "GameStateClient: uncompressing gamestate. id: " << header_.getID() << ", baseid: " << header_.getBaseID() << ", datasize: " << header_.getDataSize() << ", compsize: " << header_.getCompSize() << endl; |
---|
[7801] | 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 | { |
---|
[8858] | 334 | case Z_OK: orxout(verbose_more, context::packets) << "successfully decompressed" << endl; break; |
---|
| 335 | case Z_MEM_ERROR: orxout(internal_error, context::packets) << "not enough memory available" << endl; return false; |
---|
| 336 | case Z_BUF_ERROR: orxout(internal_warning, context::packets) << "not enough memory available in the buffer" << endl; return false; |
---|
| 337 | case Z_DATA_ERROR: orxout(internal_warning, context::packets) << "data corrupted (zlib)" << endl; return false; |
---|
[1701] | 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 | { |
---|
[8858] | 376 | // orxout(verbose, context::packets) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
[7801] | 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 | { |
---|
[8858] | 432 | // orxout(verbose, context::packets) << "docopy" << endl; |
---|
[7801] | 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(); |
---|
[8858] | 441 | // orxout(verbose, context::packets) << "copy " << h.getObjectID() << endl; |
---|
| 442 | // orxout(verbose, context::packets) << "copy " << h.getObjectID() << ":"; |
---|
[7801] | 443 | sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); |
---|
| 444 | // for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i ) |
---|
| 445 | // { |
---|
[8858] | 446 | // // orxout(verbose, context::packets) << " " << *sizes; |
---|
[7801] | 447 | // ++sizes; |
---|
| 448 | // } |
---|
[8858] | 449 | // orxout(verbose, context::packets) << endl; |
---|
[7801] | 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() ); |
---|
[9667] | 469 | assert( objectHeader.getContextID() == htemp.getContextID() ); |
---|
[7801] | 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 | { |
---|
[11083] | 491 | assert(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 | if( findObject(baseDataPtr, baseDataEnd, origHeader) ) |
---|
[7163] | 534 | { |
---|
[7801] | 535 | SynchronisableHeader baseHeader(baseDataPtr); |
---|
| 536 | assert(Synchronisable::getSynchronisable(baseHeader.getObjectID())); |
---|
| 537 | assert(ClassByID(baseHeader.getClassID())); |
---|
| 538 | assert(baseHeader.getDataSize() < 500); |
---|
| 539 | if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() ) |
---|
[7163] | 540 | { |
---|
[8858] | 541 | // orxout(verbose, context::packets) << "diffing object in order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
[7801] | 542 | diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
[1701] | 543 | } |
---|
[7801] | 544 | else |
---|
[7163] | 545 | { |
---|
[8858] | 546 | // orxout(verbose, context::packets) << "copy object because of different data sizes (1): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
[7801] | 547 | copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
| 548 | assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); |
---|
[7163] | 549 | } |
---|
[7801] | 550 | |
---|
[7163] | 551 | } |
---|
[7801] | 552 | else |
---|
[7163] | 553 | { |
---|
[7801] | 554 | assert( baseDataPtr == baseDataEnd ); |
---|
| 555 | baseDataPtr = GAMESTATE_START(base->data_); |
---|
| 556 | if( findObject(baseDataPtr, oldBaseDataPtr, origHeader) ) |
---|
[7163] | 557 | { |
---|
[7801] | 558 | SynchronisableHeader baseHeader(baseDataPtr); |
---|
| 559 | assert(Synchronisable::getSynchronisable(baseHeader.getObjectID())); |
---|
| 560 | assert(ClassByID(baseHeader.getClassID())); |
---|
| 561 | assert(baseHeader.getDataSize() < 500); |
---|
| 562 | if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() ) |
---|
[7163] | 563 | { |
---|
[8858] | 564 | // orxout(verbose, context::packets) << "diffing object out of order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
[7801] | 565 | diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
[7163] | 566 | } |
---|
[7801] | 567 | else |
---|
| 568 | { |
---|
[8858] | 569 | // orxout(verbose, context::packets) << "copy object because of different data sizes (2): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
[7801] | 570 | copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
| 571 | assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); |
---|
| 572 | } |
---|
[7163] | 573 | } |
---|
[7801] | 574 | else |
---|
[7163] | 575 | { |
---|
[8858] | 576 | // orxout(verbose, context::packets) << "copy object: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; |
---|
[7801] | 577 | assert(baseDataPtr == oldBaseDataPtr); |
---|
| 578 | copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); |
---|
| 579 | assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); |
---|
[7163] | 580 | } |
---|
| 581 | } |
---|
[1701] | 582 | } |
---|
[7801] | 583 | assert(sizesIt==this->sizes_.end()); |
---|
[1701] | 584 | |
---|
[7163] | 585 | |
---|
[7801] | 586 | Gamestate *g = new Gamestate(newData, getPeerID()); |
---|
| 587 | (g->header_) = header_; |
---|
| 588 | g->header_.setBaseID( base->getID() ); |
---|
| 589 | g->header_.setDataSize(destDataPtr - newData - GamestateHeader::getSize()); |
---|
[1751] | 590 | g->flags_=flags_; |
---|
| 591 | g->packetDirection_ = packetDirection_; |
---|
[7163] | 592 | assert(!g->isCompressed()); |
---|
[1701] | 593 | return g; |
---|
[7163] | 594 | } |
---|
[3198] | 595 | |
---|
[7163] | 596 | |
---|
[7801] | 597 | /*Gamestate* Gamestate::diffData(Gamestate *base) |
---|
[3198] | 598 | { |
---|
| 599 | assert(this && base); assert(data_ && base->data_); |
---|
[7801] | 600 | assert(!header_.isCompressed() && !base->header_.isCompressed()); |
---|
| 601 | assert(!header_.isDiffed()); |
---|
[6417] | 602 | |
---|
[3198] | 603 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
| 604 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
[7801] | 605 | uint32_t dest_length = header_.getDataSize(); |
---|
[6417] | 606 | |
---|
[3198] | 607 | if(dest_length==0) |
---|
[11071] | 608 | return nullptr; |
---|
[6417] | 609 | |
---|
[3198] | 610 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 611 | uint8_t *dest = GAMESTATE_START(ndata); |
---|
[6417] | 612 | |
---|
[7801] | 613 | rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); |
---|
[3198] | 614 | #ifndef NDEBUG |
---|
| 615 | uint8_t *dest2 = new uint8_t[dest_length]; |
---|
[7801] | 616 | rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() ); |
---|
[3198] | 617 | assert( memcmp( dest2, gs, dest_length) == 0 ); |
---|
[5929] | 618 | delete dest2; |
---|
[3198] | 619 | #endif |
---|
| 620 | |
---|
| 621 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
| 622 | assert(g->header_); |
---|
| 623 | *(g->header_) = *header_; |
---|
[7801] | 624 | g->header_.setDiffed( true ); |
---|
| 625 | g->header_.setBaseID( base->getID() ); |
---|
[3198] | 626 | g->flags_=flags_; |
---|
| 627 | g->packetDirection_ = packetDirection_; |
---|
| 628 | assert(g->isDiffed()); |
---|
| 629 | assert(!g->isCompressed()); |
---|
| 630 | return g; |
---|
[1701] | 631 | } |
---|
| 632 | |
---|
[7163] | 633 | |
---|
| 634 | Gamestate* Gamestate::undiff(Gamestate *base) |
---|
[3198] | 635 | { |
---|
| 636 | assert(this && base); assert(data_ && base->data_); |
---|
[7801] | 637 | assert(!header_.isCompressed() && !base->header_.isCompressed()); |
---|
| 638 | assert(header_.isDiffed()); |
---|
[6417] | 639 | |
---|
[3198] | 640 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
| 641 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
[7801] | 642 | uint32_t dest_length = header_.getDataSize(); |
---|
[6417] | 643 | |
---|
[3198] | 644 | if(dest_length==0) |
---|
[11071] | 645 | return nullptr; |
---|
[6417] | 646 | |
---|
[3198] | 647 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 648 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
[6417] | 649 | |
---|
[7801] | 650 | rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); |
---|
[6417] | 651 | |
---|
[3198] | 652 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
| 653 | assert(g->header_); |
---|
| 654 | *(g->header_) = *header_; |
---|
[7801] | 655 | g->header_.setDiffed( false ); |
---|
[3198] | 656 | g->flags_=flags_; |
---|
| 657 | g->packetDirection_ = packetDirection_; |
---|
| 658 | assert(!g->isDiffed()); |
---|
| 659 | assert(!g->isCompressed()); |
---|
| 660 | return g; |
---|
| 661 | } |
---|
| 662 | |
---|
| 663 | |
---|
| 664 | void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength) |
---|
| 665 | { |
---|
| 666 | uint64_t* gd = (uint64_t*)data; |
---|
| 667 | uint64_t* bd = (uint64_t*)basedata; |
---|
| 668 | uint64_t* nd = (uint64_t*)newdata; |
---|
[6417] | 669 | |
---|
[3198] | 670 | unsigned int i; |
---|
| 671 | for( i=0; i<datalength/8; i++ ) |
---|
| 672 | { |
---|
| 673 | if( i<baselength/8 ) |
---|
| 674 | *(nd+i) = *(gd+i) ^ *(bd+i); // xor the data |
---|
| 675 | else |
---|
| 676 | *(nd+i) = *(gd+i); // just copy over the data |
---|
| 677 | } |
---|
| 678 | unsigned int j; |
---|
| 679 | // now process the rest (when datalength isn't a multiple of 4) |
---|
| 680 | for( j = 8*(datalength/8); j<datalength; j++ ) |
---|
| 681 | { |
---|
| 682 | if( j<baselength ) |
---|
| 683 | *(newdata+j) = *(data+j) ^ *(basedata+j); // xor |
---|
| 684 | else |
---|
| 685 | *(newdata+j) = *(data+j); // just copy |
---|
| 686 | } |
---|
| 687 | assert(j==datalength); |
---|
[7801] | 688 | }*/ |
---|
[3198] | 689 | |
---|
[7163] | 690 | |
---|
[7801] | 691 | /*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ |
---|
[1907] | 692 | assert(data_); |
---|
[2662] | 693 | std::list<obj>::iterator it; |
---|
[2087] | 694 | |
---|
[1907] | 695 | // allocate memory for new data |
---|
[7801] | 696 | uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()]; |
---|
[1907] | 697 | // create a gamestate out of it |
---|
| 698 | Gamestate *gs = new Gamestate(gdata); |
---|
[2662] | 699 | uint8_t *newdata = gdata + GamestateHeader::getSize(); |
---|
[1907] | 700 | uint8_t *origdata = GAMESTATE_START(data_); |
---|
[2087] | 701 | |
---|
[1907] | 702 | //copy the GamestateHeader |
---|
[2662] | 703 | assert(gs->header_); |
---|
| 704 | *(gs->header_) = *header_; |
---|
[2087] | 705 | |
---|
[2662] | 706 | uint32_t objectOffset; |
---|
| 707 | unsigned int objectsize, destsize=0; |
---|
| 708 | // TODO: Why is this variable not used? |
---|
| 709 | //Synchronisable *object; |
---|
[2087] | 710 | |
---|
[2662] | 711 | //call TrafficControl |
---|
[7801] | 712 | TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ ); |
---|
[2662] | 713 | |
---|
[1907] | 714 | //copy in the zeros |
---|
[3084] | 715 | // std::list<obj>::iterator itt; |
---|
[8858] | 716 | // orxout() << "myvector contains:"; |
---|
[3084] | 717 | // for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ ) |
---|
[8858] | 718 | // orxout() << " " << (*itt).objID; |
---|
| 719 | // orxout() << endl; |
---|
[3084] | 720 | for(it=dataVector_.begin(); it!=dataVector_.end();){ |
---|
[2662] | 721 | SynchronisableHeader oldobjectheader(origdata); |
---|
| 722 | SynchronisableHeader newobjectheader(newdata); |
---|
[7163] | 723 | if ( (*it).objSize == 0 ) |
---|
[2662] | 724 | { |
---|
| 725 | ++it; |
---|
| 726 | continue; |
---|
| 727 | } |
---|
[7163] | 728 | objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize(); |
---|
[2662] | 729 | objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader |
---|
[7163] | 730 | if ( (*it).objID == oldobjectheader.getObjectID() ){ |
---|
[2662] | 731 | memcpy(newdata, origdata, objectsize); |
---|
| 732 | ++it; |
---|
[1907] | 733 | }else{ |
---|
[2662] | 734 | newobjectheader = oldobjectheader; |
---|
[2171] | 735 | memset(newdata+objectOffset, 0, objectsize-objectOffset); |
---|
[1907] | 736 | } |
---|
| 737 | newdata += objectsize; |
---|
| 738 | origdata += objectsize; |
---|
[2662] | 739 | destsize += objectsize; |
---|
[1907] | 740 | } |
---|
[2662] | 741 | #ifndef NDEBUG |
---|
| 742 | uint32_t origsize = destsize; |
---|
[7801] | 743 | while ( origsize < header_.getDataSize() ) |
---|
[2662] | 744 | { |
---|
| 745 | SynchronisableHeader oldobjectheader(origdata); |
---|
[7163] | 746 | objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize(); |
---|
[2662] | 747 | origdata += objectsize; |
---|
| 748 | origsize += objectsize; |
---|
[1907] | 749 | } |
---|
[7801] | 750 | assert(origsize==header_.getDataSize()); |
---|
[2662] | 751 | assert(destsize!=0); |
---|
| 752 | #endif |
---|
[7801] | 753 | gs->header_.setDataSize( destsize ); |
---|
[1907] | 754 | return gs; |
---|
[7801] | 755 | }*/ |
---|
[1907] | 756 | |
---|
[2087] | 757 | |
---|
[7801] | 758 | uint32_t Gamestate::calcGamestateSize(uint32_t id, uint8_t mode) |
---|
[1701] | 759 | { |
---|
[7163] | 760 | uint32_t size = 0; |
---|
| 761 | uint32_t nrOfVariables = 0; |
---|
[1701] | 762 | // get total size of gamestate |
---|
[11071] | 763 | for(Synchronisable* synchronisable : ObjectList<Synchronisable>()){ |
---|
| 764 | size+=synchronisable->getSize(id, mode); // size of the actual data of the synchronisable |
---|
| 765 | nrOfVariables += synchronisable->getNrOfVariables(); |
---|
[7163] | 766 | } |
---|
[8858] | 767 | // orxout() << "allocating " << nrOfVariables << " ints" << endl; |
---|
[7163] | 768 | this->sizes_.reserve(nrOfVariables); |
---|
[1701] | 769 | return size; |
---|
| 770 | } |
---|
| 771 | |
---|
[7163] | 772 | |
---|
[2662] | 773 | } //namespace packet |
---|
| 774 | } //namespace orxonox |
---|