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