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