Changeset 2655 for code/branches/presentation/src/network/packet
- Timestamp:
- Feb 13, 2009, 6:41:08 PM (16 years ago)
- Location:
- code/branches/presentation/src/network/packet
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation/src/network/packet/Gamestate.cc
r2575 r2655 44 44 namespace packet { 45 45 46 #define GAMESTATE_START(data) (data + GamestateHeader::getSize()) 46 47 47 48 #define PACKET_FLAG_GAMESTATE ENET_PACKET_FLAG_RELIABLE 48 49 50 // Gamestate::Gamestate() 51 // { 52 // flags_ = flags_ | PACKET_FLAG_GAMESTATE; 53 // } 54 49 55 Gamestate::Gamestate() 50 56 { 51 57 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 58 header_ = 0; 52 59 } 53 60 … … 56 63 { 57 64 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 65 header_ = new GamestateHeader(data_); 58 66 } 59 67 … … 62 70 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 63 71 data_=data; 72 header_ = new GamestateHeader(data_); 64 73 } 65 74 … … 71 80 bool Gamestate::collectData(int id, uint8_t mode) 72 81 { 82 assert(this->header_==0); // make sure the header didn't exist before 73 83 uint32_t tempsize=0, currentsize=0; 74 84 assert(data_==0); … … 78 88 if(size==0) 79 89 return false; 80 data_ = new u nsigned char[size + sizeof(GamestateHeader)];90 data_ = new uint8_t[size + GamestateHeader::getSize()]; 81 91 if(!data_){ 82 92 COUT(2) << "GameStateManager: could not allocate memory" << std::endl; 83 93 return false; 84 94 } 95 96 // create the header object 97 header_ = new GamestateHeader(data_); 85 98 86 99 //start collect data synchronisable by synchronisable 87 100 uint8_t *mem=data_; 88 mem +=sizeof(GamestateHeader);101 mem += GamestateHeader::getSize(); 89 102 ObjectList<Synchronisable>::iterator it; 90 103 for(it = ObjectList<Synchronisable>::begin(); it; ++it){ … … 100 113 while(++temp) 101 114 addsize+=temp->getSize(id, mode); 102 data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);115 data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize); 103 116 if(!data_) 104 117 return false; … … 119 132 120 133 //start write gamestate header 121 HEADER->packetType = ENUM::Gamestate; 122 HEADER->datasize = currentsize; 123 HEADER->id = id; 124 HEADER->diffed = false; 125 HEADER->complete = true; 126 HEADER->compressed = false; 134 header_->setDataSize( currentsize ); 135 header_->setID( id ); 136 header_->setDiffed( false ); 137 header_->setComplete( true ); 138 header_->setCompressed( false ); 127 139 //stop write gamestate header 128 140 … … 135 147 { 136 148 assert(data_); 137 assert(! HEADER->compressed);138 assert(! HEADER->diffed);139 uint8_t *mem=data_+ sizeof(GamestateHeader);149 assert(!header_->isCompressed()); 150 assert(!header_->isDiffed()); 151 uint8_t *mem=data_+GamestateHeader::getSize(); 140 152 // get the start of the Synchronisable list 141 153 //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin(); … … 143 155 144 156 // update the data of the objects we received 145 while(mem < data_+ sizeof(GamestateHeader)+HEADER->datasize){146 synchronisableHeader *objectheader = (synchronisableHeader*)mem;147 148 s = Synchronisable::getSynchronisable( objectheader ->objectID);157 while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()){ 158 SynchronisableHeader objectheader(mem); 159 160 s = Synchronisable::getSynchronisable( objectheader.getObjectID() ); 149 161 if(!s) 150 162 { 151 163 if (!Core::isMaster()) 164 { 152 165 Synchronisable::fabricate(mem, mode); 166 } 153 167 else 154 mem += objectheader->size; 168 { 169 mem += objectheader.getDataSize(); 170 } 155 171 // COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl; 156 172 // else … … 196 212 { 197 213 assert(data_); 198 if( HEADER->compressed)199 return HEADER->compsize+sizeof(GamestateHeader);214 if(header_->isCompressed()) 215 return header_->getCompSize()+GamestateHeader::getSize(); 200 216 else 201 217 { 202 return HEADER->datasize+sizeof(GamestateHeader);218 return header_->getDataSize()+GamestateHeader::getSize(); 203 219 } 204 220 } 205 221 206 222 bool Gamestate::operator==(packet::Gamestate gs){ 207 uint8_t *d1 = data_+ sizeof(GamestateHeader);208 uint8_t *d2 = gs.data_+ sizeof(GamestateHeader);223 uint8_t *d1 = data_+GamestateHeader::getSize(); 224 uint8_t *d2 = gs.data_+GamestateHeader::getSize(); 209 225 assert(!isCompressed()); 210 226 assert(!gs.isCompressed()); 211 while(d1<data_+ HEADER->datasize)227 while(d1<data_+header_->getDataSize()) 212 228 { 213 229 if(*d1!=*d2) … … 228 244 bool Gamestate::compressData() 229 245 { 230 assert( HEADER);231 assert(! HEADER->compressed);232 uLongf buffer = (uLongf)((( HEADER->datasize+ 12)*1.01)+1);246 assert(data_); 247 assert(!header_->isCompressed()); 248 uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1); 233 249 if(buffer==0) 234 250 return false; 235 251 236 uint8_t *ndata = new uint8_t[buffer+ sizeof(GamestateHeader)];237 uint8_t *dest = GAMESTATE_START(ndata);252 uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()]; 253 uint8_t *dest = ndata + GamestateHeader::getSize(); 238 254 //unsigned char *dest = new unsigned char[buffer]; 239 uint8_t *source = GAMESTATE_START(data_);255 uint8_t *source = data_ + GamestateHeader::getSize(); 240 256 int retval; 241 retval = compress( dest, &buffer, source, (uLong)( HEADER->datasize) );257 retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) ); 242 258 switch ( retval ) { 243 259 case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; … … 248 264 249 265 //copy and modify header 250 *GAMESTATE_HEADER(ndata) = *HEADER; 266 GamestateHeader *temp = header_; 267 header_ = new GamestateHeader(ndata, temp); 268 delete temp; 251 269 //delete old data 252 270 delete[] data_; 253 271 //save new data 254 272 data_ = ndata; 255 HEADER->compsize = buffer; 256 HEADER->compressed = true; 257 assert(HEADER->compressed); 258 COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl; 273 header_->setCompSize( buffer ); 274 header_->setCompressed( true ); 275 COUT(5) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl; 259 276 return true; 260 277 } 261 278 bool Gamestate::decompressData() 262 279 { 263 assert( HEADER);264 assert( HEADER->compressed);265 COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize<< std::endl;266 uint32_t datasize = HEADER->datasize;267 uint32_t compsize = HEADER->compsize;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(); 268 285 uint32_t bufsize; 269 286 // assert(compsize<=datasize); 270 287 bufsize = datasize; 271 288 assert(bufsize!=0); 272 uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)];273 uint8_t *dest = ndata + sizeof(GamestateHeader);274 uint8_t *source = data_ + sizeof(GamestateHeader);289 uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()]; 290 uint8_t *dest = ndata + GamestateHeader::getSize(); 291 uint8_t *source = data_ + GamestateHeader::getSize(); 275 292 int retval; 276 293 uLongf length=bufsize; … … 284 301 285 302 //copy over the header 286 *GAMESTATE_HEADER(ndata) = *HEADER; 303 GamestateHeader *temp = header_; 304 header_ = new GamestateHeader( data_, header_ ); 305 delete temp; 287 306 288 307 if (this->bDataENetAllocated_){ … … 299 318 //set new pointers 300 319 data_ = ndata; 301 HEADER->compressed = false;302 assert( HEADER->datasize==datasize);303 assert( HEADER->compsize==compsize);320 header_->setCompressed( false ); 321 assert(header_->getDataSize()==datasize); 322 assert(header_->getCompSize()==compsize); 304 323 return true; 305 324 } … … 307 326 Gamestate *Gamestate::diff(Gamestate *base) 308 327 { 309 assert(HEADER); 310 assert(!HEADER->compressed); 311 assert(!HEADER->diffed); 328 assert(data_); 329 assert(!header_->isCompressed()); 330 assert(!header_->isDiffed()); 331 GamestateHeader diffHeader(base->data_); 312 332 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; 313 333 uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); 314 334 uint32_t of=0; // pointers offset 315 335 uint32_t dest_length=0; 316 dest_length= HEADER->datasize;336 dest_length=header_->getDataSize(); 317 337 if(dest_length==0) 318 338 return NULL; 319 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+ sizeof(GamestateHeader)];320 uint8_t *dest = ndata + sizeof(GamestateHeader);321 while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){339 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; 340 uint8_t *dest = ndata + GamestateHeader::getSize(); 341 while(of < diffHeader.getDataSize() && of < header_->getDataSize()){ 322 342 *(dest+of)=*(basep+of)^*(gs+of); // do the xor 323 343 ++of; 324 344 } 325 if( GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){345 if(diffHeader.getDataSize()!=header_->getDataSize()){ 326 346 uint8_t n=0; 327 if( GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){347 if(diffHeader.getDataSize() < header_->getDataSize()){ 328 348 while(of<dest_length){ 329 349 *(dest+of)=n^*(gs+of); … … 333 353 } 334 354 335 *GAMESTATE_HEADER(ndata) = *HEADER;336 GAMESTATE_HEADER(ndata)->diffed = true;337 GAMESTATE_HEADER(ndata)->base_id = base->getID();338 355 Gamestate *g = new Gamestate(ndata, getClientID()); 356 *(g->header_) = *header_; 357 g->header_->setDiffed( true ); 358 g->header_->setBaseID( base->getID() ); 339 359 g->flags_=flags_; 340 360 g->packetDirection_ = packetDirection_; … … 347 367 348 368 // allocate memory for new data 349 uint8_t *gdata = new uint8_t[ HEADER->datasize+sizeof(GamestateHeader)];369 uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()]; 350 370 // create a gamestate out of it 351 371 Gamestate *gs = new Gamestate(gdata); 352 uint8_t *newdata = gdata + sizeof(GamestateHeader);372 uint8_t *newdata = gdata + GamestateHeader::getSize(); 353 373 uint8_t *origdata = GAMESTATE_START(data_); 354 374 355 375 //copy the GamestateHeader 356 *(GamestateHeader*)gdata = *HEADER;357 358 synchronisableHeader *oldobjectheader, *newobjectheader; 376 assert(gs->header_); 377 *(gs->header_) = *header_; 378 359 379 uint32_t objectOffset; 360 380 unsigned int objectsize, destsize=0; … … 363 383 364 384 //call TrafficControl 365 TrafficControl::getInstance()->processObjectList( clientID, HEADER->id, &dataMap_ );385 TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), &dataMap_ ); 366 386 367 387 //copy in the zeros … … 371 391 // if(it->second->getSize(HEADER->id)==0) // merged from objecthierarchy2, doesn't work anymore; TODO: change this 372 392 // continue; // merged from objecthierarchy2, doesn't work anymore; TODO: change this 373 oldobjectheader = (synchronisableHeader*)origdata;374 newobjectheader = (synchronisableHeader*)newdata;393 SynchronisableHeader oldobjectheader(origdata); 394 SynchronisableHeader newobjectheader(newdata); 375 395 if ( (*it).objSize == 0 ) 376 396 { … … 380 400 // object = Synchronisable::getSynchronisable( (*it).objID ); 381 401 // assert(object->objectID == oldobjectheader->objectID); 382 objectsize = oldobjectheader ->size;383 objectOffset= sizeof(synchronisableHeader); //skip the size and the availableData variables in the objectheader384 if ( (*it).objID == oldobjectheader ->objectID){402 objectsize = oldobjectheader.getDataSize(); 403 objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader 404 if ( (*it).objID == oldobjectheader.getObjectID() ){ 385 405 memcpy(newdata, origdata, objectsize); 386 assert(newobjectheader ->dataAvailable==true);406 assert(newobjectheader.isDataAvailable()==true); 387 407 ++it; 388 408 }else{ 389 *newobjectheader = *oldobjectheader;390 newobjectheader ->dataAvailable=false;409 newobjectheader = oldobjectheader; 410 newobjectheader.setDataAvailable(false); 391 411 memset(newdata+objectOffset, 0, objectsize-objectOffset); 392 412 } … … 397 417 #ifndef NDEBUG 398 418 uint32_t origsize = destsize; 399 while ( origsize < HEADER->datasize)419 while ( origsize < header_->getDataSize() ) 400 420 { 401 oldobjectheader = (synchronisableHeader*)origdata;402 objectsize = oldobjectheader ->size;421 SynchronisableHeader oldobjectheader(origdata); 422 objectsize = oldobjectheader.getDataSize(); 403 423 origdata += objectsize; 404 424 origsize += objectsize; 405 425 } 406 assert(origsize== HEADER->datasize);426 assert(origsize==header_->getDataSize()); 407 427 assert(destsize!=0); 408 428 #endif 409 ((GamestateHeader*)gdata)->datasize = destsize;429 gs->header_->setDataSize( destsize ); 410 430 return gs; 411 431 } … … 414 434 Gamestate *Gamestate::undiff(Gamestate *base) 415 435 { 416 assert(this && base);assert( HEADER);417 assert( HEADER->diffed);418 assert(! HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);436 assert(this && base);assert(data_); 437 assert(header_->isDiffed()); 438 assert(!header_->isCompressed() && !base->header_->isCompressed()); 419 439 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; 420 440 uint8_t *basep = GAMESTATE_START(base->data_); … … 422 442 uint32_t of=0; // pointers offset 423 443 uint32_t dest_length=0; 424 dest_length= HEADER->datasize;444 dest_length=header_->getDataSize(); 425 445 if(dest_length==0) 426 446 return NULL; 427 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+ sizeof(GamestateHeader)];428 uint8_t *dest = ndata + sizeof(GamestateHeader);429 while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){447 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; 448 uint8_t *dest = ndata + GamestateHeader::getSize(); 449 while(of < base->header_->getDataSize() && of < header_->getDataSize()){ 430 450 *(dest+of)=*(basep+of)^*(gs+of); // do the xor 431 451 ++of; 432 452 } 433 if( GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){453 if(base->header_->getDataSize()!=header_->getDataSize()){ 434 454 uint8_t n=0; 435 if( GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){455 if(base->header_->getDataSize() < header_->getDataSize()){ 436 456 while(of < dest_length){ 437 457 *(dest+of)=n^*(gs+of); … … 440 460 } 441 461 } 442 *GAMESTATE_HEADER(ndata) = *HEADER;443 GAMESTATE_HEADER(ndata)->diffed = false;444 462 Gamestate *g = new Gamestate(ndata, getClientID()); 463 assert(g->header_); 464 *(g->header_) = *header_; 465 g->header_->setDiffed( false ); 445 466 g->flags_=flags_; 446 467 g->packetDirection_ = packetDirection_; … … 463 484 464 485 } //namespace packet 465 466 486 } //namespace orxonox -
code/branches/presentation/src/network/packet/Gamestate.h
r2575 r2655 46 46 namespace packet { 47 47 48 #define GAMESTATE_START(data) (data + sizeof(GamestateHeader)) 49 #define GAMESTATE_HEADER(data) ((GamestateHeader *)data) 50 #define HEADER GAMESTATE_HEADER(data_) 48 class _NetworkExport GamestateHeader{ 49 public: 50 GamestateHeader(uint8_t *data){ data_ = data; *(uint32_t*)data_ = ENUM::Gamestate; } 51 GamestateHeader(uint8_t *data, GamestateHeader* h) 52 { data_=data; memcpy(data_, h->data_, getSize()); } 53 static inline uint32_t getSize() 54 { return 21; } 51 55 52 struct _NetworkExport GamestateHeader{ 53 ENUM::Type packetType; 54 int32_t id; // id of the gamestate 55 uint32_t compsize; 56 uint32_t datasize; 57 int32_t base_id; // id of the base-gamestate diffed from 58 bool diffed:1; // wheter diffed or not 59 bool complete:1; // wheter it is a complete gamestate or only partial 60 bool compressed:1; 56 inline int32_t getID() const 57 { return *(int32_t*)(data_+4); } 58 inline void setID(int32_t id) 59 { *(int32_t*)(data_+4) = id; } 60 61 inline int32_t getBaseID() const 62 { return *(int32_t*)(data_+8); } 63 inline void setBaseID(int32_t id) 64 { *(int32_t*)(data_+8) = id; } 65 66 inline uint32_t getDataSize() const 67 { return *(uint32_t*)(data_+12); } 68 inline void setDataSize(uint32_t size) 69 { *(uint32_t*)(data_+12) = size; } 70 71 inline uint32_t getCompSize() const 72 { return *(uint32_t*)(data_+16); } 73 inline void setCompSize(uint32_t size) 74 { *(uint32_t*)(data_+16) = size; } 75 76 inline bool isDiffed() const 77 { return *(int8_t*)(data_+20) & 0x1; } 78 inline void setDiffed(bool b) 79 { *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); } 80 81 inline bool isComplete() const 82 { return *(int8_t*)(data_+20) & 0x2; } 83 inline void setComplete(bool b) 84 { *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); } 85 86 inline bool isCompressed() const 87 { return *(int8_t*)(data_+20) & 0x4; } 88 inline void setCompressed(bool b) 89 { *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); } 90 91 inline void operator=(GamestateHeader& h) 92 { memcpy( data_, h.data_, getSize()); } 93 private: 94 uint8_t *data_; 95 //#define GAMESTATE_START(data) (data + sizeof(GamestateHeader)) 96 //#define GAMESTATE_HEADER(data) ((GamestateHeader *)data) 97 //#define HEADER GAMESTATE_HEADER(data_) 98 61 99 }; 62 100 … … 74 112 bool collectData(int id, uint8_t mode=0x0); 75 113 bool spreadData( uint8_t mode=0x0); 76 inline int32_t getID() const { return HEADER->id; }77 inline bool isDiffed() const { return HEADER->diffed; }78 inline bool isCompressed() const { return HEADER->compressed; }79 inline int32_t getBaseID() const { return HEADER->base_id; }114 inline int32_t getID() const { return header_->getID(); } 115 inline bool isDiffed() const { return header_->isDiffed(); } 116 inline bool isCompressed() const { return header_->isCompressed(); } 117 inline int32_t getBaseID() const { return header_->getBaseID(); } 80 118 Gamestate *diff(Gamestate *base); 81 119 Gamestate *undiff(Gamestate *base); … … 93 131 uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0); 94 132 std::list<obj> dataMap_; 133 GamestateHeader* header_; 95 134 }; 96 135
Note: See TracChangeset
for help on using the changeset viewer.