Changeset 7754
- Timestamp:
- Dec 10, 2010, 8:13:57 PM (14 years ago)
- Location:
- code/branches/network4/src/libraries/network
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network4/src/libraries/network/packet/Gamestate.cc
r7753 r7754 72 72 73 73 Gamestate::Gamestate(uint8_t *data, unsigned int clientID): 74 Packet(data, clientID) 74 Packet(data, clientID), header_(data) 75 75 { 76 76 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 77 header_ = new GamestateHeader(data_); 78 } 79 80 81 Gamestate::Gamestate(uint8_t *data)77 } 78 79 80 Gamestate::Gamestate(uint8_t *data): 81 header_(data) 82 82 { 83 83 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 84 84 data_ = data; 85 header_ = new GamestateHeader(data_);86 85 } 87 86 88 87 89 88 Gamestate::Gamestate(const Gamestate& g) : 90 Packet( *(Packet*)&g), nrOfVariables_(0)89 Packet( *(Packet*)&g ), header_(this->data_), nrOfVariables_(0) 91 90 { 92 91 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 93 header_ = new GamestateHeader(data_);94 92 sizes_ = g.sizes_; 95 93 } … … 98 96 Gamestate::~Gamestate() 99 97 { 100 if( header_ )101 delete header_;102 98 } 103 99 … … 105 101 bool Gamestate::collectData(int id, uint8_t mode) 106 102 { 107 assert(this->header_==0); // make sure the header didn't exist before108 103 uint32_t tempsize=0, currentsize=0; 109 104 assert(data_==0); … … 120 115 } 121 116 122 // create the header object 123 assert( header_ == 0 ); 124 header_ = new GamestateHeader(data_); 117 // tell the gamestate header where to store the data 118 header_.setData(this->data_); 125 119 126 120 //start collect data synchronisable by synchronisable … … 161 155 162 156 //start write gamestate header 163 header_ ->setDataSize( currentsize );164 header_ ->setID( id );165 header_ ->setBaseID( GAMESTATEID_INITIAL );166 header_ ->setDiffed( false );167 header_ ->setComplete( true );168 header_ ->setCompressed( false );157 header_.setDataSize( currentsize ); 158 header_.setID( id ); 159 header_.setBaseID( GAMESTATEID_INITIAL ); 160 header_.setDiffed( false ); 161 header_.setComplete( true ); 162 header_.setCompressed( false ); 169 163 //stop write gamestate header 170 164 … … 177 171 bool Gamestate::spreadData(uint8_t mode) 178 172 { 179 COUT(4) << "processing gamestate with id " << header_ ->getID() << endl;173 COUT(4) << "processing gamestate with id " << header_.getID() << endl; 180 174 assert(data_); 181 assert(!header_ ->isCompressed());175 assert(!header_.isCompressed()); 182 176 uint8_t *mem=data_+GamestateHeader::getSize(); 183 177 Synchronisable *s; 184 178 185 179 // update the data of the objects we received 186 while(mem < data_+GamestateHeader::getSize()+header_ ->getDataSize())180 while(mem < data_+GamestateHeader::getSize()+header_.getDataSize()) 187 181 { 188 182 SynchronisableHeader objectheader(mem); … … 249 243 { 250 244 assert(data_); 251 if(header_ ->isCompressed())252 return header_ ->getCompSize()+GamestateHeader::getSize();245 if(header_.isCompressed()) 246 return header_.getCompSize()+GamestateHeader::getSize(); 253 247 else 254 248 { 255 return header_ ->getDataSize()+GamestateHeader::getSize();249 return header_.getDataSize()+GamestateHeader::getSize(); 256 250 } 257 251 } … … 280 274 { 281 275 assert(data_); 282 assert(!header_ ->isCompressed());283 uLongf buffer = (uLongf)(((header_ ->getDataSize() + 12)*1.01)+1);276 assert(!header_.isCompressed()); 277 uLongf buffer = (uLongf)(((header_.getDataSize() + 12)*1.01)+1); 284 278 if(buffer==0) 285 279 return false; … … 289 283 uint8_t *source = data_ + GamestateHeader::getSize(); 290 284 int retval; 291 retval = compress( dest, &buffer, source, (uLong)(header_ ->getDataSize()) );285 retval = compress( dest, &buffer, source, (uLong)(header_.getDataSize()) ); 292 286 switch ( retval ) 293 287 { … … 299 293 300 294 //copy and modify header 301 GamestateHeader *temp = header_; 302 header_ = new GamestateHeader(ndata, temp); 295 GamestateHeader *temp = new GamestateHeader(data_); 296 header_.setData(ndata); 297 header_ = *temp; 303 298 delete temp; 304 299 //delete old data … … 306 301 //save new data 307 302 data_ = ndata; 308 header_ ->setCompSize( buffer );309 header_ ->setCompressed( true );310 COUT(0) << "gamestate compress datasize: " << header_ ->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;303 header_.setCompSize( buffer ); 304 header_.setCompressed( true ); 305 COUT(0) << "gamestate compress datasize: " << header_.getDataSize() << " compsize: " << header_.getCompSize() << std::endl; 311 306 return true; 312 307 } … … 316 311 { 317 312 assert(data_); 318 assert(header_ ->isCompressed());319 COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_ ->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl;320 uint32_t datasize = header_ ->getDataSize();321 uint32_t compsize = header_ ->getCompSize();313 assert(header_.isCompressed()); 314 COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_.getID() << ", baseid: " << header_.getBaseID() << ", datasize: " << header_.getDataSize() << ", compsize: " << header_.getCompSize() << std::endl; 315 uint32_t datasize = header_.getDataSize(); 316 uint32_t compsize = header_.getCompSize(); 322 317 uint32_t bufsize; 323 318 bufsize = datasize; … … 338 333 339 334 //copy over the header 340 GamestateHeader *temp = header_; 341 header_ = new GamestateHeader( data_, header_ ); 335 GamestateHeader* temp = new GamestateHeader( data_ ); 336 header_.setData(ndata); 337 header_ = *temp; 342 338 delete temp; 343 339 … … 357 353 //set new pointers 358 354 data_ = ndata; 359 header_ ->setCompressed( false );360 assert(header_ ->getDataSize()==datasize);361 assert(header_ ->getCompSize()==compsize);355 header_.setCompressed( false ); 356 assert(header_.getDataSize()==datasize); 357 assert(header_.getCompSize()==compsize); 362 358 return true; 363 359 } … … 512 508 { 513 509 assert(this && base); assert(data_ && base->data_); 514 assert(!header_ ->isCompressed() && !base->header_->isCompressed());515 assert(!header_ ->isDiffed());516 assert( header_ ->getDataSize() && base->header_->getDataSize() );510 assert(!header_.isCompressed() && !base->header_.isCompressed()); 511 assert(!header_.isDiffed()); 512 assert( header_.getDataSize() && base->header_.getDataSize() ); 517 513 518 514 … … 521 517 uint8_t *baseDataPtr = GAMESTATE_START(base->data_); 522 518 uint8_t *origDataPtr = GAMESTATE_START(this->data_); 523 uint8_t *origDataEnd = origDataPtr + header_ ->getDataSize();524 uint8_t *baseDataEnd = baseDataPtr + base->header_ ->getDataSize();525 // uint32_t origLength = header_ ->getDataSize();526 // uint32_t baseLength = base->header_ ->getDataSize();519 uint8_t *origDataEnd = origDataPtr + header_.getDataSize(); 520 uint8_t *baseDataEnd = baseDataPtr + base->header_.getDataSize(); 521 // uint32_t origLength = header_.getDataSize(); 522 // uint32_t baseLength = base->header_.getDataSize(); 527 523 528 524 // Allocate new space for diffed gamestate 529 uint32_t newDataSize = header_ ->getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_;525 uint32_t newDataSize = header_.getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_; 530 526 uint8_t *newData = new uint8_t[newDataSize]; // this is the maximum size needed in the worst case 531 527 uint8_t *destDataPtr = GAMESTATE_START(newData); … … 598 594 599 595 Gamestate *g = new Gamestate(newData, getClientID()); 600 assert(g->header_); 601 *(g->header_) = *header_; 602 g->header_->setBaseID( base->getID() ); 603 g->header_->setDataSize(destDataPtr - newData - GamestateHeader::getSize()); 596 (g->header_) = header_; 597 g->header_.setBaseID( base->getID() ); 598 g->header_.setDataSize(destDataPtr - newData - GamestateHeader::getSize()); 604 599 g->flags_=flags_; 605 600 g->packetDirection_ = packetDirection_; … … 612 607 { 613 608 assert(this && base); assert(data_ && base->data_); 614 assert(!header_ ->isCompressed() && !base->header_->isCompressed());615 assert(!header_ ->isDiffed());609 assert(!header_.isCompressed() && !base->header_.isCompressed()); 610 assert(!header_.isDiffed()); 616 611 617 612 uint8_t *basep = GAMESTATE_START(base->data_); 618 613 uint8_t *gs = GAMESTATE_START(this->data_); 619 uint32_t dest_length = header_ ->getDataSize();614 uint32_t dest_length = header_.getDataSize(); 620 615 621 616 if(dest_length==0) … … 625 620 uint8_t *dest = GAMESTATE_START(ndata); 626 621 627 rawDiff( dest, gs, basep, header_ ->getDataSize(), base->header_->getDataSize() );622 rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); 628 623 #ifndef NDEBUG 629 624 uint8_t *dest2 = new uint8_t[dest_length]; 630 rawDiff( dest2, dest, basep, header_ ->getDataSize(), base->header_->getDataSize() );625 rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() ); 631 626 assert( memcmp( dest2, gs, dest_length) == 0 ); 632 627 delete dest2; … … 636 631 assert(g->header_); 637 632 *(g->header_) = *header_; 638 g->header_ ->setDiffed( true );639 g->header_ ->setBaseID( base->getID() );633 g->header_.setDiffed( true ); 634 g->header_.setBaseID( base->getID() ); 640 635 g->flags_=flags_; 641 636 g->packetDirection_ = packetDirection_; … … 649 644 { 650 645 assert(this && base); assert(data_ && base->data_); 651 assert(!header_ ->isCompressed() && !base->header_->isCompressed());652 assert(header_ ->isDiffed());646 assert(!header_.isCompressed() && !base->header_.isCompressed()); 647 assert(header_.isDiffed()); 653 648 654 649 uint8_t *basep = GAMESTATE_START(base->data_); 655 650 uint8_t *gs = GAMESTATE_START(this->data_); 656 uint32_t dest_length = header_ ->getDataSize();651 uint32_t dest_length = header_.getDataSize(); 657 652 658 653 if(dest_length==0) … … 662 657 uint8_t *dest = ndata + GamestateHeader::getSize(); 663 658 664 rawDiff( dest, gs, basep, header_ ->getDataSize(), base->header_->getDataSize() );659 rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); 665 660 666 661 Gamestate *g = new Gamestate(ndata, getClientID()); 667 662 assert(g->header_); 668 663 *(g->header_) = *header_; 669 g->header_ ->setDiffed( false );664 g->header_.setDiffed( false ); 670 665 g->flags_=flags_; 671 666 g->packetDirection_ = packetDirection_; … … 708 703 709 704 // allocate memory for new data 710 uint8_t *gdata = new uint8_t[header_ ->getDataSize()+GamestateHeader::getSize()];705 uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()]; 711 706 // create a gamestate out of it 712 707 Gamestate *gs = new Gamestate(gdata); … … 724 719 725 720 //call TrafficControl 726 TrafficControl::getInstance()->processObjectList( clientID, header_ ->getID(), dataVector_ );721 TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ ); 727 722 728 723 //copy in the zeros … … 755 750 #ifndef NDEBUG 756 751 uint32_t origsize = destsize; 757 while ( origsize < header_ ->getDataSize() )752 while ( origsize < header_.getDataSize() ) 758 753 { 759 754 SynchronisableHeader oldobjectheader(origdata); … … 762 757 origsize += objectsize; 763 758 } 764 assert(origsize==header_ ->getDataSize());759 assert(origsize==header_.getDataSize()); 765 760 assert(destsize!=0); 766 761 #endif 767 gs->header_ ->setDataSize( destsize );762 gs->header_.setDataSize( destsize ); 768 763 return gs; 769 764 }*/ -
code/branches/network4/src/libraries/network/packet/Gamestate.h
r7753 r7754 48 48 class _NetworkExport GamestateHeader{ 49 49 public: 50 GamestateHeader(uint8_t *data){ assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } 51 GamestateHeader(uint8_t *data, GamestateHeader* h) 52 { assert(data); data_=data; memcpy(data_, h->data_, getSize()); } 50 GamestateHeader(); 51 GamestateHeader(uint8_t* data) 52 { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } 53 /*GamestateHeader(uint8_t* data, GamestateHeader* h) 54 { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }*/ 55 void setData(uint8_t* data) 56 { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } 53 57 static inline uint32_t getSize() 54 { return 21; }58 { return 21; } 55 59 56 60 inline int32_t getID() const 57 { assert(data_); return *(int32_t*)(data_+4); }61 { assert(data_); return *(int32_t*)(data_+4); } 58 62 inline void setID(int32_t id) 59 { assert(data_); *(int32_t*)(data_+4) = id; }63 { assert(data_); *(int32_t*)(data_+4) = id; } 60 64 61 65 inline int32_t getBaseID() const 62 { assert(data_); return *(int32_t*)(data_+8); }66 { assert(data_); return *(int32_t*)(data_+8); } 63 67 inline void setBaseID(int32_t id) 64 { assert(data_); *(int32_t*)(data_+8) = id; }68 { assert(data_); *(int32_t*)(data_+8) = id; } 65 69 66 70 inline uint32_t getDataSize() const 67 { assert(data_); return *(uint32_t*)(data_+12); }71 { assert(data_); return *(uint32_t*)(data_+12); } 68 72 inline void setDataSize(uint32_t size) 69 { assert(data_); *(uint32_t*)(data_+12) = size; }73 { assert(data_); *(uint32_t*)(data_+12) = size; } 70 74 71 75 inline uint32_t getCompSize() const … … 75 79 76 80 inline bool isDiffed() const 77 { assert(data_); return *(int8_t*)(data_+20) & 0x1; }81 { assert(data_); return *(int8_t*)(data_+20) & 0x1; } 78 82 inline void setDiffed(bool b) 79 { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }83 { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); } 80 84 81 85 inline bool isComplete() const 82 { assert(data_); return *(int8_t*)(data_+20) & 0x2; }86 { assert(data_); return *(int8_t*)(data_+20) & 0x2; } 83 87 inline void setComplete(bool b) 84 { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }88 { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); } 85 89 86 90 inline bool isCompressed() const 87 { assert(data_); return *(int8_t*)(data_+20) & 0x4; }91 { assert(data_); return *(int8_t*)(data_+20) & 0x4; } 88 92 inline void setCompressed(bool b) 89 { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }93 { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); } 90 94 91 95 inline void operator=(GamestateHeader& h) 92 { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); }96 { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); } 93 97 private: 94 uint8_t *data_;98 uint8_t* data_; 95 99 96 100 }; … … 110 114 bool collectData(int id, uint8_t mode=0x0); 111 115 bool spreadData( uint8_t mode=0x0); 112 inline int32_t getID() const { return header_ ->getID(); }113 inline bool isDiffed() const { return header_ ->isDiffed(); }114 inline bool isCompressed() const { return header_ ->isCompressed(); }115 inline int32_t getBaseID() const { return header_ ->getBaseID(); }116 inline uint32_t getDataSize() const { return header_ ->getDataSize(); }116 inline int32_t getID() const { return header_.getID(); } 117 inline bool isDiffed() const { return header_.isDiffed(); } 118 inline bool isCompressed() const { return header_.isCompressed(); } 119 inline int32_t getBaseID() const { return header_.getBaseID(); } 120 inline uint32_t getDataSize() const { return header_.getDataSize(); } 117 121 Gamestate* diffVariables(Gamestate *base); 118 122 // Gamestate* diffData(Gamestate *base); … … 134 138 135 139 std::list<obj> dataVector_; 136 GamestateHeader *header_;140 GamestateHeader header_; 137 141 std::vector<uint32_t> sizes_; 138 142 uint32_t nrOfVariables_; -
code/branches/network4/src/libraries/network/synchronisable/Synchronisable.cc
r7497 r7754 321 321 mem += SynchronisableHeader::getSize(); 322 322 std::vector<SynchronisableVariableBase *>::iterator i; 323 for(i=syncList_.begin(); i!=syncList_.end(); i++)323 for(i=syncList_.begin(); i!=syncList_.end(); ++i) 324 324 { 325 325 assert( mem <= data+syncHeader2.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream
Note: See TracChangeset
for help on using the changeset viewer.