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