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