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