[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 | } |
---|
[6387] | 97 | |
---|
[2662] | 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){ |
---|
[6387] | 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_) ); |
---|
[6387] | 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()); |
---|
[6387] | 364 | |
---|
[3198] | 365 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
| 366 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
| 367 | uint32_t dest_length = header_->getDataSize(); |
---|
[6387] | 368 | |
---|
[3198] | 369 | if(dest_length==0) |
---|
| 370 | return NULL; |
---|
[6387] | 371 | |
---|
[3198] | 372 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 373 | uint8_t *dest = GAMESTATE_START(ndata); |
---|
[6387] | 374 | |
---|
[3198] | 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 ); |
---|
[5929] | 380 | delete dest2; |
---|
[3198] | 381 | #endif |
---|
| 382 | |
---|
| 383 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
| 384 | assert(g->header_); |
---|
| 385 | *(g->header_) = *header_; |
---|
| 386 | g->header_->setDiffed( true ); |
---|
| 387 | g->header_->setBaseID( base->getID() ); |
---|
| 388 | g->flags_=flags_; |
---|
| 389 | g->packetDirection_ = packetDirection_; |
---|
| 390 | assert(g->isDiffed()); |
---|
| 391 | assert(!g->isCompressed()); |
---|
| 392 | return g; |
---|
[1701] | 393 | } |
---|
| 394 | |
---|
[3198] | 395 | Gamestate *Gamestate::undiff(Gamestate *base) |
---|
| 396 | { |
---|
| 397 | assert(this && base); assert(data_ && base->data_); |
---|
| 398 | assert(!header_->isCompressed() && !base->header_->isCompressed()); |
---|
| 399 | assert(header_->isDiffed()); |
---|
[6387] | 400 | |
---|
[3198] | 401 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
| 402 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
| 403 | uint32_t dest_length = header_->getDataSize(); |
---|
[6387] | 404 | |
---|
[3198] | 405 | if(dest_length==0) |
---|
| 406 | return NULL; |
---|
[6387] | 407 | |
---|
[3198] | 408 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 409 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
[6387] | 410 | |
---|
[3198] | 411 | rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() ); |
---|
[6387] | 412 | |
---|
[3198] | 413 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
| 414 | assert(g->header_); |
---|
| 415 | *(g->header_) = *header_; |
---|
| 416 | g->header_->setDiffed( false ); |
---|
| 417 | g->flags_=flags_; |
---|
| 418 | g->packetDirection_ = packetDirection_; |
---|
| 419 | assert(!g->isDiffed()); |
---|
| 420 | assert(!g->isCompressed()); |
---|
| 421 | return g; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | |
---|
[3084] | 425 | // Gamestate *Gamestate::diff(Gamestate *base) |
---|
| 426 | // { |
---|
| 427 | // assert(data_); |
---|
| 428 | // assert(!header_->isCompressed()); |
---|
| 429 | // assert(!header_->isDiffed()); |
---|
| 430 | // GamestateHeader diffHeader(base->data_); |
---|
| 431 | // uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); |
---|
| 432 | // uint32_t of=0; // pointers offset |
---|
| 433 | // uint32_t dest_length=0; |
---|
| 434 | // dest_length=header_->getDataSize(); |
---|
| 435 | // if(dest_length==0) |
---|
| 436 | // return NULL; |
---|
| 437 | // uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 438 | // uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
[6388] | 439 | // |
---|
| 440 | // |
---|
[3084] | 441 | // // LOOP-UNROLLED DIFFING |
---|
| 442 | // uint32_t *dest32 = (uint32_t*)dest, *base32 = (uint32_t*)basep, *gs32 = (uint32_t*)gs; |
---|
| 443 | // // diff in 4-byte steps |
---|
| 444 | // while( of < (uint32_t)(header_->getDataSize())/4 ){ |
---|
| 445 | // if( of < (uint32_t)(diffHeader.getDataSize())/4 ) |
---|
| 446 | // { |
---|
| 447 | // *(dest32+of)=*(base32+of) ^ *(gs32+of); // do the xor |
---|
| 448 | // ++of; |
---|
| 449 | // } |
---|
| 450 | // else |
---|
| 451 | // { |
---|
| 452 | // *(dest32+of)=*(gs32+of); // same as 0 ^ *(gs32+of) |
---|
| 453 | // ++of; |
---|
| 454 | // } |
---|
| 455 | // } |
---|
| 456 | // for( unsigned int of2 = 0; of2 < header_->getDataSize()%4; ++of2 ) |
---|
| 457 | // { |
---|
| 458 | // if( of*4+of2 < diffHeader.getDataSize() ) |
---|
| 459 | // { |
---|
| 460 | // *(dest+4*of+of2)=*(basep+4*of+of2) ^ *(gs+4*of+of2); // do the xor |
---|
| 461 | // } |
---|
| 462 | // else |
---|
| 463 | // { |
---|
| 464 | // *(dest+4*of+of2)=*(gs+4*of+of2); // same as 0 ^ *(gs32+of) |
---|
| 465 | // } |
---|
| 466 | // } |
---|
[6388] | 467 | // |
---|
[3084] | 468 | // Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
| 469 | // *(g->header_) = *header_; |
---|
| 470 | // g->header_->setDiffed( true ); |
---|
| 471 | // g->header_->setBaseID( base->getID() ); |
---|
| 472 | // g->flags_=flags_; |
---|
| 473 | // g->packetDirection_ = packetDirection_; |
---|
| 474 | // return g; |
---|
| 475 | // } |
---|
| 476 | |
---|
[3198] | 477 | |
---|
| 478 | void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength) |
---|
| 479 | { |
---|
| 480 | uint64_t* gd = (uint64_t*)data; |
---|
| 481 | uint64_t* bd = (uint64_t*)basedata; |
---|
| 482 | uint64_t* nd = (uint64_t*)newdata; |
---|
[6387] | 483 | |
---|
[3198] | 484 | unsigned int i; |
---|
| 485 | for( i=0; i<datalength/8; i++ ) |
---|
| 486 | { |
---|
| 487 | if( i<baselength/8 ) |
---|
| 488 | *(nd+i) = *(gd+i) ^ *(bd+i); // xor the data |
---|
| 489 | else |
---|
| 490 | *(nd+i) = *(gd+i); // just copy over the data |
---|
| 491 | } |
---|
| 492 | unsigned int j; |
---|
| 493 | // now process the rest (when datalength isn't a multiple of 4) |
---|
| 494 | for( j = 8*(datalength/8); j<datalength; j++ ) |
---|
| 495 | { |
---|
| 496 | if( j<baselength ) |
---|
| 497 | *(newdata+j) = *(data+j) ^ *(basedata+j); // xor |
---|
| 498 | else |
---|
| 499 | *(newdata+j) = *(data+j); // just copy |
---|
| 500 | } |
---|
| 501 | assert(j==datalength); |
---|
| 502 | } |
---|
| 503 | |
---|
[2662] | 504 | Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ |
---|
[1907] | 505 | assert(data_); |
---|
[2662] | 506 | std::list<obj>::iterator it; |
---|
[2087] | 507 | |
---|
[1907] | 508 | // allocate memory for new data |
---|
[2662] | 509 | uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()]; |
---|
[1907] | 510 | // create a gamestate out of it |
---|
| 511 | Gamestate *gs = new Gamestate(gdata); |
---|
[2662] | 512 | uint8_t *newdata = gdata + GamestateHeader::getSize(); |
---|
[1907] | 513 | uint8_t *origdata = GAMESTATE_START(data_); |
---|
[2087] | 514 | |
---|
[1907] | 515 | //copy the GamestateHeader |
---|
[2662] | 516 | assert(gs->header_); |
---|
| 517 | *(gs->header_) = *header_; |
---|
[2087] | 518 | |
---|
[2662] | 519 | uint32_t objectOffset; |
---|
| 520 | unsigned int objectsize, destsize=0; |
---|
| 521 | // TODO: Why is this variable not used? |
---|
| 522 | //Synchronisable *object; |
---|
[2087] | 523 | |
---|
[2662] | 524 | //call TrafficControl |
---|
[3084] | 525 | TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), dataVector_ ); |
---|
[2662] | 526 | |
---|
[1907] | 527 | //copy in the zeros |
---|
[3084] | 528 | // std::list<obj>::iterator itt; |
---|
| 529 | // COUT(0) << "myvector contains:"; |
---|
| 530 | // for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ ) |
---|
[6394] | 531 | // COUT(0) << ' ' << (*itt).objID; |
---|
[3084] | 532 | // COUT(0) << endl; |
---|
| 533 | for(it=dataVector_.begin(); it!=dataVector_.end();){ |
---|
[2662] | 534 | SynchronisableHeader oldobjectheader(origdata); |
---|
| 535 | SynchronisableHeader newobjectheader(newdata); |
---|
[6400] | 536 | if ( it->objSize == 0 ) |
---|
[2662] | 537 | { |
---|
| 538 | ++it; |
---|
| 539 | continue; |
---|
| 540 | } |
---|
| 541 | objectsize = oldobjectheader.getDataSize(); |
---|
| 542 | objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader |
---|
[6400] | 543 | if ( it->objID == oldobjectheader.getObjectID() ){ |
---|
[2662] | 544 | memcpy(newdata, origdata, objectsize); |
---|
| 545 | assert(newobjectheader.isDataAvailable()==true); |
---|
| 546 | ++it; |
---|
[1907] | 547 | }else{ |
---|
[2662] | 548 | newobjectheader = oldobjectheader; |
---|
| 549 | newobjectheader.setDataAvailable(false); |
---|
[2171] | 550 | memset(newdata+objectOffset, 0, objectsize-objectOffset); |
---|
[1907] | 551 | } |
---|
| 552 | newdata += objectsize; |
---|
| 553 | origdata += objectsize; |
---|
[2662] | 554 | destsize += objectsize; |
---|
[1907] | 555 | } |
---|
[2662] | 556 | #ifndef NDEBUG |
---|
| 557 | uint32_t origsize = destsize; |
---|
| 558 | while ( origsize < header_->getDataSize() ) |
---|
| 559 | { |
---|
| 560 | SynchronisableHeader oldobjectheader(origdata); |
---|
| 561 | objectsize = oldobjectheader.getDataSize(); |
---|
| 562 | origdata += objectsize; |
---|
| 563 | origsize += objectsize; |
---|
[1907] | 564 | } |
---|
[2662] | 565 | assert(origsize==header_->getDataSize()); |
---|
| 566 | assert(destsize!=0); |
---|
| 567 | #endif |
---|
| 568 | gs->header_->setDataSize( destsize ); |
---|
[1907] | 569 | return gs; |
---|
| 570 | } |
---|
| 571 | |
---|
[2087] | 572 | |
---|
[3198] | 573 | /*Gamestate *Gamestate::undiff(Gamestate *base) |
---|
[1701] | 574 | { |
---|
[2662] | 575 | assert(this && base);assert(data_); |
---|
| 576 | assert(header_->isDiffed()); |
---|
| 577 | assert(!header_->isCompressed() && !base->header_->isCompressed()); |
---|
[1907] | 578 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
| 579 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
[2662] | 580 | uint32_t of=0; // pointers offset |
---|
| 581 | uint32_t dest_length=0; |
---|
| 582 | dest_length=header_->getDataSize(); |
---|
[1701] | 583 | if(dest_length==0) |
---|
| 584 | return NULL; |
---|
[2662] | 585 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 586 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
| 587 | while(of < base->header_->getDataSize() && of < header_->getDataSize()){ |
---|
[1701] | 588 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
| 589 | ++of; |
---|
| 590 | } |
---|
[2662] | 591 | if(base->header_->getDataSize()!=header_->getDataSize()){ |
---|
[1907] | 592 | uint8_t n=0; |
---|
[2662] | 593 | if(base->header_->getDataSize() < header_->getDataSize()){ |
---|
[1701] | 594 | while(of < dest_length){ |
---|
| 595 | *(dest+of)=n^*(gs+of); |
---|
| 596 | of++; |
---|
| 597 | } |
---|
| 598 | } |
---|
| 599 | } |
---|
[1751] | 600 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
[2662] | 601 | assert(g->header_); |
---|
| 602 | *(g->header_) = *header_; |
---|
| 603 | g->header_->setDiffed( false ); |
---|
[1751] | 604 | g->flags_=flags_; |
---|
| 605 | g->packetDirection_ = packetDirection_; |
---|
| 606 | assert(!g->isDiffed()); |
---|
| 607 | assert(!g->isCompressed()); |
---|
[1701] | 608 | return g; |
---|
[3198] | 609 | }*/ |
---|
[1701] | 610 | |
---|
[2662] | 611 | uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode) |
---|
[1701] | 612 | { |
---|
[2662] | 613 | uint32_t size=0; |
---|
[1701] | 614 | // get the start of the Synchronisable list |
---|
[2171] | 615 | ObjectList<Synchronisable>::iterator it; |
---|
[1701] | 616 | // get total size of gamestate |
---|
[2171] | 617 | for(it = ObjectList<Synchronisable>::begin(); it; ++it) |
---|
[1907] | 618 | size+=it->getSize(id, mode); // size of the actual data of the synchronisable |
---|
[1701] | 619 | return size; |
---|
| 620 | } |
---|
| 621 | |
---|
[2662] | 622 | } //namespace packet |
---|
| 623 | } //namespace orxonox |
---|