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