Changeset 6450
- Timestamp:
- Jan 18, 2010, 12:05:57 AM (15 years ago)
- Location:
- code/branches/network2/src/libraries/network
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network2/src/libraries/network/GamestateClient.cc
r6449 r6450 161 161 assert(b); 162 162 } 163 // if(gs->isDiffed()){ 163 if(gs->isDiffed()) 164 { 165 assert(0); 164 166 // packet::Gamestate *base = gamestateMap_[gs->getBaseID()]; 165 // if(!base){ 166 // COUT(3) << "could not find base gamestate id: " << gs->getBaseID() << endl; 167 // if(!base) 168 // { 169 // COUT(0) << "could not find base gamestate id: " << gs->getBaseID() << endl; 170 // assert(0); 167 171 // delete gs; 168 172 // return 0; 169 173 // } 170 // // assert(base); //TODO: fix this171 174 // packet::Gamestate *undiffed = gs->undiff(base); 172 175 // delete gs; 173 176 // gs=undiffed; 174 177 // COUT(5) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl; 175 //}178 } 176 179 if(gs->spreadData(0x2)) 177 180 return gs; 178 181 else 179 182 { 180 COUT(3) << "could not spread gamestate" << endl; 183 COUT(0) << "could not spread gamestate" << endl; 184 assert(0); 181 185 return NULL; 182 186 } -
code/branches/network2/src/libraries/network/GamestateManager.cc
r6449 r6450 190 190 // packet::Gamestate* gs = new packet::Gamestate(*gamestate); 191 191 // packet::Gamestate* gs = gamestate; 192 packet::Gamestate *gs = new packet::Gamestate(*gamestate); 192 packet::Gamestate *gs = new packet::Gamestate(*gamestate); //TODO: is this neccessary ? 193 193 // packet::Gamestate *gs = new packet::Gamestate(); 194 194 // gs->collectData( id_, 0x1 ); … … 201 201 if(base) 202 202 { 203 204 // COUT(3) << "diffing" << std::endl; 205 // packet::Gamestate* gs1 = gs; 206 packet::Gamestate *diffed = gs->diff(base); 207 if( diffed->getDataSize() == 0 ) 203 packet::Gamestate *diffed1 = gs->diffVariables(base); 204 if( diffed1->getDataSize() == 0 ) 208 205 { 209 delete diffed ;206 delete diffed1; 210 207 destgamestate = 0; 211 208 return; 212 209 } 213 else 214 gs = diffed; 215 //packet::Gamestate *gs2 = diffed->undiff(gs); 216 // assert(*gs == *gs2); 217 // packet::Gamestate* gs2 = gs->undiff(client); 218 // gs = new packet::Gamestate(*gs); 219 // assert(*gs1==*gs2); 220 } 221 else{ 210 gs = diffed1; 211 } 212 else 213 { 222 214 gs = new packet::Gamestate(*gs); 223 215 } -
code/branches/network2/src/libraries/network/Host.h
r6073 r6450 36 36 37 37 const unsigned int CLIENTID_SERVER = 0; 38 const unsigned int NETWORK_FREQUENCY = 30;38 const unsigned int NETWORK_FREQUENCY = 25; 39 39 const float NETWORK_PERIOD = 1.0f/NETWORK_FREQUENCY; 40 40 -
code/branches/network2/src/libraries/network/packet/Gamestate.cc
r6449 r6450 181 181 assert(!header_->isCompressed()); 182 182 uint8_t *mem=data_+GamestateHeader::getSize(); 183 bool diffed = header_->isDiffed();184 183 Synchronisable *s; 185 184 … … 194 193 if (!GameMode::isMaster()) 195 194 { 196 Synchronisable::fabricate(mem, diffed,mode);195 Synchronisable::fabricate(mem, mode); 197 196 } 198 197 else 199 198 { 200 mem += objectheader.getDataSize() +SynchronisableHeader::getSize();199 mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() ); 201 200 } 202 201 } … … 365 364 366 365 367 Gamestate *Gamestate::diff(Gamestate *base)366 Gamestate* Gamestate::diffVariables(Gamestate *base) 368 367 { 369 368 assert(this && base); assert(data_ && base->data_); … … 381 380 assert( origLength && baseLength ); 382 381 383 COUT(0) << "newSize: " << origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_ << endl;384 382 uint8_t *nData = new uint8_t[origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_]; // this is the maximum size needed in the worst case 385 383 uint8_t *dest = GAMESTATE_START(nData); … … 448 446 // COUT(4) << "diff " << h.getObjectID() << ":"; 449 447 // Now start to diff the Object 450 SynchronisableHeader h2(dest);448 SynchronisableHeaderLight h2(dest); 451 449 h2 = h; // copy over the objectheader 452 uint32_tvariableID = 0;453 uint32_t newObjectOffset = SynchronisableHeader ::getSize();450 VariableID variableID = 0; 451 uint32_t newObjectOffset = SynchronisableHeaderLight::getSize(); 454 452 // iterate through all variables 455 453 while( objectOffset < h.getDataSize()+SynchronisableHeader::getSize() ) … … 465 463 { 466 464 // COUT(4) << " c" << varSize; 467 *( uint32_t*)(dest+newObjectOffset) = variableID; // copy over the variableID468 newObjectOffset += sizeof( uint32_t);465 *(VariableID*)(dest+newObjectOffset) = variableID; // copy over the variableID 466 newObjectOffset += sizeof(VariableID); 469 467 memcpy( dest+newObjectOffset, origData+origOffset+objectOffset, varSize ); 470 468 newObjectOffset += varSize; … … 485 483 // COUT(4) << endl; 486 484 h2.setDiffed(true); 487 h2.setDataSize(newObjectOffset-SynchronisableHeader ::getSize());485 h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize()); 488 486 assert(objectOffset == h.getDataSize()+SynchronisableHeader::getSize()); 489 487 origOffset += objectOffset; … … 520 518 assert(g->header_); 521 519 *(g->header_) = *header_; 520 g->header_->setBaseID( base->getID() ); 521 g->header_->setDataSize(dest - nData - GamestateHeader::getSize()); 522 g->flags_=flags_; 523 g->packetDirection_ = packetDirection_; 524 assert(!g->isCompressed()); 525 return g; 526 } 527 528 529 Gamestate* Gamestate::diffData(Gamestate *base) 530 { 531 assert(this && base); assert(data_ && base->data_); 532 assert(!header_->isCompressed() && !base->header_->isCompressed()); 533 assert(!header_->isDiffed()); 534 535 uint8_t *basep = GAMESTATE_START(base->data_); 536 uint8_t *gs = GAMESTATE_START(this->data_); 537 uint32_t dest_length = header_->getDataSize(); 538 539 if(dest_length==0) 540 return NULL; 541 542 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; 543 uint8_t *dest = GAMESTATE_START(ndata); 544 545 rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() ); 546 #ifndef NDEBUG 547 uint8_t *dest2 = new uint8_t[dest_length]; 548 rawDiff( dest2, dest, basep, header_->getDataSize(), base->header_->getDataSize() ); 549 assert( memcmp( dest2, gs, dest_length) == 0 ); 550 delete dest2; 551 #endif 552 553 Gamestate *g = new Gamestate(ndata, getClientID()); 554 assert(g->header_); 555 *(g->header_) = *header_; 522 556 g->header_->setDiffed( true ); 523 557 g->header_->setBaseID( base->getID() ); 524 g->header_->setDataSize(dest - nData - GamestateHeader::getSize());525 558 g->flags_=flags_; 526 559 g->packetDirection_ = packetDirection_; … … 528 561 assert(!g->isCompressed()); 529 562 return g; 563 } 564 565 566 Gamestate* Gamestate::undiff(Gamestate *base) 567 { 568 assert(this && base); assert(data_ && base->data_); 569 assert(!header_->isCompressed() && !base->header_->isCompressed()); 570 assert(header_->isDiffed()); 571 572 uint8_t *basep = GAMESTATE_START(base->data_); 573 uint8_t *gs = GAMESTATE_START(this->data_); 574 uint32_t dest_length = header_->getDataSize(); 575 576 if(dest_length==0) 577 return NULL; 578 579 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; 580 uint8_t *dest = ndata + GamestateHeader::getSize(); 581 582 rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() ); 583 584 Gamestate *g = new Gamestate(ndata, getClientID()); 585 assert(g->header_); 586 *(g->header_) = *header_; 587 g->header_->setDiffed( false ); 588 g->flags_=flags_; 589 g->packetDirection_ = packetDirection_; 590 assert(!g->isDiffed()); 591 assert(!g->isCompressed()); 592 return g; 593 } 594 595 596 void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength) 597 { 598 uint64_t* gd = (uint64_t*)data; 599 uint64_t* bd = (uint64_t*)basedata; 600 uint64_t* nd = (uint64_t*)newdata; 601 602 unsigned int i; 603 for( i=0; i<datalength/8; i++ ) 604 { 605 if( i<baselength/8 ) 606 *(nd+i) = *(gd+i) ^ *(bd+i); // xor the data 607 else 608 *(nd+i) = *(gd+i); // just copy over the data 609 } 610 unsigned int j; 611 // now process the rest (when datalength isn't a multiple of 4) 612 for( j = 8*(datalength/8); j<datalength; j++ ) 613 { 614 if( j<baselength ) 615 *(newdata+j) = *(data+j) ^ *(basedata+j); // xor 616 else 617 *(newdata+j) = *(data+j); // just copy 618 } 619 assert(j==datalength); 530 620 } 531 621 -
code/branches/network2/src/libraries/network/packet/Gamestate.h
r6449 r6450 115 115 inline int32_t getBaseID() const { return header_->getBaseID(); } 116 116 inline uint32_t getDataSize() const { return header_->getDataSize(); } 117 Gamestate *diff(Gamestate *base); 117 Gamestate* diffVariables(Gamestate *base); 118 Gamestate* diffData(Gamestate *base); 119 Gamestate *undiff(Gamestate *base); 118 120 Gamestate* doSelection(unsigned int clientID, unsigned int targetSize); 119 121 bool compressData(); -
code/branches/network2/src/libraries/network/packet/Packet.cc
r6417 r6450 69 69 enetPacket_=0; 70 70 bDataENetAllocated_ = false; 71 }72 73 void blub(ENetPacket *packet){74 COUT(4) << "blubb" << std::endl;75 71 } 76 72 -
code/branches/network2/src/libraries/network/synchronisable/Synchronisable.cc
r6449 r6450 132 132 * @return pointer to the newly created synchronisable 133 133 */ 134 Synchronisable *Synchronisable::fabricate(uint8_t*& mem, bool diffed,uint8_t mode)134 Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode) 135 135 { 136 136 SynchronisableHeader header(mem); 137 assert( !header.isDiffed() ); 137 138 138 139 COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl; … … 308 309 if(syncList_.empty()){ 309 310 assert(0); 310 COUT( 4) << "Synchronisable::updateData syncList_ is empty" << std::endl;311 COUT(2) << "Synchronisable::updateData syncList_ is empty" << std::endl; 311 312 return false; 312 313 } … … 314 315 uint8_t* data=mem; 315 316 // start extract header 316 SynchronisableHeader syncHeader(mem); 317 assert(syncHeader.getObjectID()==this->objectID_); 318 assert(syncHeader.getCreatorID()==this->creatorID_); 319 assert(syncHeader.getClassID()==this->classID_); 320 321 mem += SynchronisableHeader::getSize(); 322 // stop extract header 317 SynchronisableHeaderLight syncHeaderLight(mem); 318 assert(syncHeaderLight.getObjectID()==this->getObjectID()); 323 319 324 320 //COUT(5) << "Synchronisable: objectID_ " << syncHeader.getObjectID() << ", classID_ " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << std::endl; 325 if( !syncHeader.isDiffed() ) 326 { 321 if( !syncHeaderLight.isDiffed() ) 322 { 323 SynchronisableHeader syncHeader2(mem); 324 assert( this->getClassID() == syncHeader2.getClassID() ); 325 assert( this->getCreatorID() == syncHeader2.getCreatorID() ); 326 mem += SynchronisableHeader::getSize(); 327 327 std::vector<SynchronisableVariableBase *>::iterator i; 328 328 for(i=syncList_.begin(); i!=syncList_.end(); i++) 329 329 { 330 assert( mem <= data+syncHeader .getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream330 assert( mem <= data+syncHeader2.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream 331 331 (*i)->putData( mem, mode, forceCallback ); 332 332 } 333 assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeader::getSize() ); 333 334 } 334 335 else 335 336 { 336 COUT(0) << "objectID: " << this->objectID_ << endl; 337 while( mem < data+syncHeader.getDataSize()+SynchronisableHeader::getSize() ) 337 mem += SynchronisableHeaderLight::getSize(); 338 // COUT(0) << "objectID: " << this->objectID_ << endl; 339 while( mem < data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() ) 338 340 { 339 uint32_t varID = *(uint32_t*)mem; 340 COUT(0) << "varID: " << varID << endl; 341 if( varID == 22 ) 342 COUT(6) << " blub " << endl; 341 VariableID varID = *(VariableID*)mem; 342 // COUT(0) << "varID: " << varID << endl; 343 343 assert( varID < syncList_.size() ); 344 mem += sizeof( uint32_t);344 mem += sizeof(VariableID); 345 345 syncList_[varID]->putData( mem, mode, forceCallback ); 346 346 } 347 }348 assert(mem == data+syncHeader.getDataSize()+SynchronisableHeader::getSize() );347 assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() ); 348 } 349 349 return true; 350 350 } -
code/branches/network2/src/libraries/network/synchronisable/Synchronisable.h
r6449 r6450 64 64 }; 65 65 } 66 67 typedef uint8_t VariableID; 66 68 67 69 /** … … 77 79 */ 78 80 class _NetworkExport SynchronisableHeader{ 81 friend class SynchronisableHeaderLight; 79 82 private: 80 uint8_t *data_;83 uint8_t* data_; 81 84 public: 82 85 SynchronisableHeader(uint8_t* data) 83 86 { data_ = data; } 84 87 inline static uint32_t getSize() 85 { return 1 6; }86 inline uint 32_t getDataSize() const87 { return (*(uint 32_t*)data_) & 0x7FFFFFFF; } //only use the first 31 bits88 inline void setDataSize(uint 32_t size)89 { *(uint 32_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint32_t*)(data_) & 0x80000000 ); }88 { return 14; } 89 inline uint16_t getDataSize() const 90 { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits 91 inline void setDataSize(uint16_t size) 92 { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); } 90 93 inline bool isDiffed() const 91 { return ( (*(uint 32_t*)data_) & 0x80000000 ) == 0x80000000; }94 { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; } 92 95 inline void setDiffed( bool b) 93 { *(uint 32_t*)(data_) = (b << 31) | (*(uint32_t*)(data_) & 0x7FFFFFFF ); }96 { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); } 94 97 inline uint32_t getObjectID() const 95 { return *(uint32_t*)(data_+ 4); }98 { return *(uint32_t*)(data_+2); } 96 99 inline void setObjectID(uint32_t objectID_) 97 { *(uint32_t*)(data_+ 4) = objectID_; }100 { *(uint32_t*)(data_+2) = objectID_; } 98 101 inline uint32_t getClassID() const 99 { return *(uint32_t*)(data_+ 8); }102 { return *(uint32_t*)(data_+6); } 100 103 inline void setClassID(uint32_t classID_) 101 { *(uint32_t*)(data_+ 8) = classID_; }104 { *(uint32_t*)(data_+6) = classID_; } 102 105 inline uint32_t getCreatorID() const 103 { return *(uint32_t*)(data_+1 2); }106 { return *(uint32_t*)(data_+10); } 104 107 inline void setCreatorID(uint32_t creatorID_) 105 { *(uint32_t*)(data_+1 2) = creatorID_; }108 { *(uint32_t*)(data_+10) = creatorID_; } 106 109 inline void operator=(SynchronisableHeader& h) 107 110 { memcpy(data_, h.data_, getSize()); } … … 119 122 class _NetworkExport SynchronisableHeaderLight{ 120 123 private: 121 uint8_t *data_;124 uint8_t* data_; 122 125 public: 123 SynchronisableHeader (uint8_t* data)126 SynchronisableHeaderLight(uint8_t* data) 124 127 { data_ = data; } 125 128 inline static uint32_t getSize() 126 { return 16; }127 inline uint 32_t getDataSize() const128 { return (*(uint 32_t*)data_) & 0x7FFFFFFF; } //only use the first 31 bits129 inline void setDataSize(uint 32_t size)130 { *(uint 32_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint32_t*)(data_) & 0x80000000 ); }129 { return 6; } 130 inline uint16_t getDataSize() const 131 { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits 132 inline void setDataSize(uint16_t size) 133 { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); } 131 134 inline bool isDiffed() const 132 { return ( (*(uint 32_t*)data_) & 0x80000000 ) == 0x80000000; }135 { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; } 133 136 inline void setDiffed( bool b) 134 { *(uint 32_t*)(data_) = (b << 31) | (*(uint32_t*)(data_) & 0x7FFFFFFF ); }137 { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); } 135 138 inline uint32_t getObjectID() const 136 { return *(uint32_t*)(data_+ 4); }139 { return *(uint32_t*)(data_+2); } 137 140 inline void setObjectID(uint32_t objectID_) 138 { *(uint32_t*)(data_+ 4) = objectID_; }141 { *(uint32_t*)(data_+2) = objectID_; } 139 142 inline void operator=(SynchronisableHeader& h) 140 143 { memcpy(data_, h.data_, getSize()); } … … 153 156 static void setClient(bool b); 154 157 155 static Synchronisable *fabricate(uint8_t*& mem, bool diffed,uint8_t mode=0x0);158 static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0); 156 159 static bool deleteObject(uint32_t objectID_); 157 160 static Synchronisable *getSynchronisable(uint32_t objectID_); … … 168 171 169 172 inline uint32_t getNrOfVariables(){ return this->syncList_.size(); } 170 inline uint32_t getVarSize( uint32_tID )173 inline uint32_t getVarSize( VariableID ID ) 171 174 { return this->syncList_[ID]->getSize(state_); } 172 175
Note: See TracChangeset
for help on using the changeset viewer.