Changeset 7801 for code/trunk/src/libraries/network/packet
- Timestamp:
- Dec 22, 2010, 7:24:24 PM (14 years ago)
- Location:
- code/trunk
- Files:
-
- 21 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/network/packet/Acknowledgement.cc
r6417 r7801 31 31 #include "util/Debug.h" 32 32 #include "network/GamestateHandler.h" 33 #include "network/Host.h" 33 34 34 35 namespace orxonox { … … 39 40 #define _ACKID _PACKETID + sizeof(packet::Type::Value) 40 41 41 Acknowledgement::Acknowledgement( unsigned int id, unsigned int clientID )42 Acknowledgement::Acknowledgement( unsigned int id, unsigned int peerID ) 42 43 : Packet() 43 44 { … … 46 47 *(Type::Value *)(data_ + _PACKETID ) = Type::Acknowledgement; 47 48 *(uint32_t *)(data_ + _ACKID ) = id; 48 clientID_=clientID;49 peerID_=peerID; 49 50 } 50 51 51 Acknowledgement::Acknowledgement( uint8_t *data, unsigned int clientID )52 : Packet(data, clientID)52 Acknowledgement::Acknowledgement( uint8_t *data, unsigned int peerID ) 53 : Packet(data, peerID) 53 54 { 54 55 } … … 62 63 } 63 64 64 bool Acknowledgement::process( ){65 bool Acknowledgement::process(orxonox::Host* host){ 65 66 COUT(5) << "processing ACK with ID: " << getAckID() << endl; 66 bool b = GamestateHandler::ackGamestate(getAckID(), clientID_);67 bool b = host->ackGamestate(getAckID(), peerID_); 67 68 delete this; 68 69 return b; -
code/trunk/src/libraries/network/packet/Acknowledgement.h
r6073 r7801 42 42 { 43 43 public: 44 Acknowledgement( unsigned int id, unsigned int clientID );45 Acknowledgement( uint8_t* data, unsigned int clientID );44 Acknowledgement( unsigned int id, unsigned int peerID ); 45 Acknowledgement( uint8_t* data, unsigned int peerID ); 46 46 ~Acknowledgement(); 47 47 48 48 inline unsigned int getSize() const; 49 bool process();49 virtual bool process(orxonox::Host* host); 50 50 51 51 unsigned int getAckID(); -
code/trunk/src/libraries/network/packet/Chat.cc
r7163 r7801 80 80 } 81 81 82 bool Chat::process( ){83 bool b = Host::incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID));82 bool Chat::process(orxonox::Host* host){ 83 bool b = host->incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID)); 84 84 delete this; 85 85 return b; -
code/trunk/src/libraries/network/packet/Chat.h
r7163 r7801 52 52 53 53 /* process chat message packet and remove it afterwards */ 54 bool process();54 virtual bool process(orxonox::Host* host); 55 55 56 56 /* Get the length of the message (not the full size of the packet) */ -
code/trunk/src/libraries/network/packet/ClassID.cc
r7163 r7801 120 120 121 121 122 bool ClassID::process( ){122 bool ClassID::process(orxonox::Host* host){ 123 123 int nrOfClasses; 124 124 uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid -
code/trunk/src/libraries/network/packet/ClassID.h
r6417 r7801 48 48 49 49 uint32_t getSize() const; 50 bool process();50 virtual bool process(orxonox::Host* host); 51 51 52 52 private: -
code/trunk/src/libraries/network/packet/DeleteObjects.cc
r6417 r7801 57 57 } 58 58 59 bool DeleteObjects::fetchIDs(){ 59 bool DeleteObjects::fetchIDs() 60 { 60 61 unsigned int number = Synchronisable::getNumberOfDeletedObject(); 61 62 if(number==0) … … 79 80 } 80 81 81 unsigned int DeleteObjects::getSize() const{ 82 unsigned int DeleteObjects::getSize() const 83 { 82 84 assert(data_); 83 85 return _OBJECTIDS + *(uint32_t*)(data_+_QUANTITY)*sizeof(uint32_t); 84 86 } 85 87 86 bool DeleteObjects::process(){ 87 for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++){ 88 bool DeleteObjects::process(orxonox::Host* host) 89 { 90 for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++) 91 { 88 92 COUT(4) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << std::endl; 89 93 Synchronisable::deleteObject( *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) ); -
code/trunk/src/libraries/network/packet/DeleteObjects.h
r6073 r7801 32 32 #include "Packet.h" 33 33 34 namespace orxonox { 35 namespace packet { 34 namespace orxonox 35 { 36 namespace packet 37 { 36 38 /** 37 39 @author … … 47 49 48 50 inline unsigned int getSize() const; 49 bool process();51 virtual bool process(orxonox::Host* host); 50 52 51 53 private: -
code/trunk/src/libraries/network/packet/FunctionCalls.cc
r7495 r7801 32 32 #include "network/FunctionCall.h" 33 33 #include "network/FunctionCallManager.h" 34 #include "network/GamestateHandler.h" 34 35 35 36 namespace orxonox { … … 40 41 const unsigned int FUNCTIONCALLS_MEM_ALLOCATION = 1000; 41 42 42 FunctionCalls::FunctionCalls() 43 : Packet()43 FunctionCalls::FunctionCalls(): 44 Packet(), minGamestateID_(GAMESTATEID_INITIAL) 44 45 { 45 46 flags_ = flags_ | PACKET_FLAGS_FUNCTIONCALLS; 46 currentSize_ = 2*sizeof(uint32_t); // for packetid and nrOfCalls47 currentSize_ = 3*sizeof(uint32_t); // for packetid, nrOfCalls and minGamestateID_ 47 48 } 48 49 49 FunctionCalls::FunctionCalls( uint8_t* data, unsigned int clientID ) 50 : Packet(data, clientID)50 FunctionCalls::FunctionCalls( uint8_t* data, unsigned int clientID ): 51 Packet(data, clientID), minGamestateID_(GAMESTATEID_INITIAL) 51 52 { 52 53 } … … 57 58 58 59 59 bool FunctionCalls::process(){ 60 bool FunctionCalls::process(orxonox::Host* host) 61 { 60 62 assert(isDataENetAllocated()); 61 63 62 64 uint8_t* temp = data_+sizeof(uint32_t); //skip packetid 63 65 uint32_t nrOfCalls = *(uint32_t*)temp; 66 temp += sizeof(uint32_t); 67 this->minGamestateID_ = *(uint32_t*)temp; 64 68 temp += sizeof(uint32_t); 65 69 for( unsigned int i = 0; i<nrOfCalls; i++ ) … … 68 72 fctCall.loadData(temp); 69 73 if( !fctCall.execute() ) 70 FunctionCallManager::bufferIncomingFunctionCall( fctCall ); 74 { 75 FunctionCallManager::bufferIncomingFunctionCall( fctCall, minGamestateID_, this->getPeerID() ); 76 } 71 77 } 72 78 … … 75 81 } 76 82 77 void FunctionCalls::addCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){ 83 void FunctionCalls::addCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5) 84 { 78 85 assert(!isDataENetAllocated()); 79 86 … … 83 90 } 84 91 85 void FunctionCalls::addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){ 92 void FunctionCalls::addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5) 93 { 86 94 assert(!isDataENetAllocated()); 87 95 … … 91 99 } 92 100 93 bool FunctionCalls::send( )101 bool FunctionCalls::send(orxonox::Host* host) 94 102 { 103 this->minGamestateID_ = host->getCurrentGamestateID(); 95 104 assert(this->functionCalls_.size()); 96 105 data_=new uint8_t[ currentSize_ ]; 97 106 *(Type::Value *)(data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID 98 *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls to 0 99 uint8_t* temp = data_+2*sizeof(uint32_t); 107 *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls 108 *(uint32_t*)(data_+2*sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_ 109 uint8_t* temp = data_+3*sizeof(uint32_t); 100 110 101 111 while( this->functionCalls_.size() ) … … 107 117 assert( temp==data_+currentSize_ ); 108 118 109 Packet::send( );119 Packet::send(host); 110 120 return true; 111 121 } -
code/trunk/src/libraries/network/packet/FunctionCalls.h
r7490 r7801 54 54 inline unsigned int getSize() const 55 55 { assert(!this->isDataENetAllocated()); return currentSize_; } 56 bool process();56 virtual bool process(orxonox::Host* host); 57 57 58 58 void addCallStatic( uint32_t networkID, const MultiType* mt1=0, const MultiType* mt2=0, const MultiType* mt3=0, const MultiType* mt4=0, const MultiType* mt5=0); 59 59 void addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1=0, const MultiType* mt2=0, const MultiType* mt3=0, const MultiType* mt4=0, const MultiType* mt5=0); 60 virtual bool send( );60 virtual bool send(orxonox::Host* host); 61 61 private: 62 62 std::queue<orxonox::FunctionCall> functionCalls_; 63 63 unsigned int clientID_; 64 uint32_t minGamestateID_; 64 65 uint32_t currentSize_; 65 66 }; -
code/trunk/src/libraries/network/packet/FunctionIDs.cc
r6417 r7801 46 46 47 47 48 FunctionIDs::FunctionIDs( ) : Packet(){ 48 FunctionIDs::FunctionIDs( ) : Packet() 49 { 49 50 unsigned int nrOfFunctions=0; 50 51 unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nroffunctions … … 55 56 //calculate total needed size (for all strings and integers) 56 57 ObjectList<NetworkFunctionBase>::iterator it; 57 for(it = ObjectList<NetworkFunctionBase>::begin(); it; ++it){ 58 for(it = ObjectList<NetworkFunctionBase>::begin(); it; ++it) 59 { 58 60 const std::string& functionname = it->getName(); 59 61 networkID = it->getNetworkID(); … … 76 78 // now save all classids and classnames 77 79 std::pair<uint32_t, std::string> tempPair; 78 while( !tempQueue.empty() ){ 80 while( !tempQueue.empty() ) 81 { 79 82 tempPair = tempQueue.front(); 80 83 tempQueue.pop(); … … 98 101 } 99 102 100 uint32_t FunctionIDs::getSize() const{ 103 uint32_t FunctionIDs::getSize() const 104 { 101 105 assert(this->data_); 102 106 uint8_t *temp = data_+sizeof(uint32_t); // packet identification … … 114 118 115 119 116 bool FunctionIDs::process(){ 120 bool FunctionIDs::process(orxonox::Host* host) 121 { 117 122 int nrOfFunctions; 118 123 uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid … … 127 132 temp += sizeof(uint32_t); 128 133 129 for( int i=0; i<nrOfFunctions; i++){ 134 for( int i=0; i<nrOfFunctions; i++) 135 { 130 136 networkID = *(uint32_t*)temp; 131 137 stringsize = *(uint32_t*)(temp+sizeof(uint32_t)); -
code/trunk/src/libraries/network/packet/FunctionIDs.h
r6417 r7801 47 47 ~FunctionIDs(); 48 48 49 uint32_t getSize() const;50 bool process();49 virtual uint32_t getSize() const; 50 virtual bool process(orxonox::Host* host); 51 51 52 52 private: -
code/trunk/src/libraries/network/packet/Gamestate.cc
r7163 r7801 36 36 #include "network/synchronisable/Synchronisable.h" 37 37 #include "network/GamestateHandler.h" 38 #include "network/Host.h" 38 39 39 40 namespace orxonox { … … 43 44 #define GAMESTATE_START(data) (data + GamestateHeader::getSize()) 44 45 45 #define PACKET_FLAG_GAMESTATE PacketFlag::Reliable 46 // #define PACKET_FLAG_GAMESTATE PacketFlag::Reliable 47 #define PACKET_FLAG_GAMESTATE 0 46 48 47 49 inline bool memzero( uint8_t* data, uint32_t datalength) … … 65 67 66 68 Gamestate::Gamestate(): 67 header_( 0)69 header_() 68 70 { 69 71 flags_ = flags_ | PACKET_FLAG_GAMESTATE; … … 72 74 73 75 Gamestate::Gamestate(uint8_t *data, unsigned int clientID): 74 Packet(data, clientID) 76 Packet(data, clientID), header_(data) 75 77 { 76 78 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 77 header_ = new GamestateHeader(data_); 78 } 79 80 81 Gamestate::Gamestate(uint8_t *data)79 } 80 81 82 Gamestate::Gamestate(uint8_t *data): 83 header_(data) 82 84 { 83 85 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 84 86 data_ = data; 85 header_ = new GamestateHeader(data_);86 87 } 87 88 88 89 89 90 Gamestate::Gamestate(const Gamestate& g) : 90 Packet( *(Packet*)&g), nrOfVariables_(0)91 Packet( *(Packet*)&g ), header_(this->data_), nrOfVariables_(0) 91 92 { 92 93 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 93 header_ = new GamestateHeader(data_);94 94 sizes_ = g.sizes_; 95 95 } … … 98 98 Gamestate::~Gamestate() 99 99 { 100 if( header_ )101 delete header_;102 100 } 103 101 … … 105 103 bool Gamestate::collectData(int id, uint8_t mode) 106 104 { 107 assert(this->header_==0); // make sure the header didn't exist before108 105 uint32_t tempsize=0, currentsize=0; 109 106 assert(data_==0); … … 120 117 } 121 118 122 // create the header object 123 assert( header_ == 0 ); 124 header_ = new GamestateHeader(data_); 119 // tell the gamestate header where to store the data 120 header_.setData(this->data_); 125 121 126 122 //start collect data synchronisable by synchronisable … … 142 138 assert(0); // if we don't use multithreading this part shouldn't be neccessary 143 139 // start allocate additional memory 144 COUT(3) << "G .St.Man: need additional memory" << std::endl;140 COUT(3) << "Gamestate: need additional memory" << std::endl; 145 141 ObjectList<Synchronisable>::iterator temp = it; 146 142 uint32_t addsize=tempsize; … … 161 157 162 158 //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 );159 header_.setDataSize( currentsize ); 160 header_.setID( id ); 161 header_.setBaseID( GAMESTATEID_INITIAL ); 162 header_.setDiffed( false ); 163 header_.setComplete( true ); 164 header_.setCompressed( false ); 169 165 //stop write gamestate header 170 166 171 COUT(5) << "G .ST.Man: Gamestate size: " << currentsize << std::endl;172 COUT(5) << "G .ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;167 COUT(5) << "Gamestate: Gamestate size: " << currentsize << std::endl; 168 COUT(5) << "Gamestate: 'estimated' (and corrected) Gamestate size: " << size << std::endl; 173 169 return true; 174 170 } … … 177 173 bool Gamestate::spreadData(uint8_t mode) 178 174 { 179 COUT( 4) << "processing gamestate with id " << header_->getID() << endl;175 COUT(5) << "processing gamestate with id " << header_.getID() << endl; 180 176 assert(data_); 181 assert(!header_ ->isCompressed());177 assert(!header_.isCompressed()); 182 178 uint8_t *mem=data_+GamestateHeader::getSize(); 183 179 Synchronisable *s; 184 180 185 181 // update the data of the objects we received 186 while(mem < data_+GamestateHeader::getSize()+header_ ->getDataSize())182 while(mem < data_+GamestateHeader::getSize()+header_.getDataSize()) 187 183 { 188 184 SynchronisableHeader objectheader(mem); … … 197 193 else 198 194 { 195 // COUT(4) << "not creating object of classid " << objectheader.getClassID() << endl; 199 196 mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() ); 200 197 } … … 202 199 else 203 200 { 201 // COUT(4) << "updating object of classid " << objectheader.getClassID() << endl; 204 202 bool b = s->updateData(mem, mode); 205 203 assert(b); 206 204 } 207 205 } 206 assert(mem-data_ == GamestateHeader::getSize()+header_.getDataSize()); 207 208 208 // In debug mode, check first, whether there are no duplicate objectIDs 209 209 #ifndef NDEBUG … … 249 249 { 250 250 assert(data_); 251 if(header_ ->isCompressed())252 return header_ ->getCompSize()+GamestateHeader::getSize();251 if(header_.isCompressed()) 252 return header_.getCompSize()+GamestateHeader::getSize(); 253 253 else 254 254 { 255 return header_ ->getDataSize()+GamestateHeader::getSize();255 return header_.getDataSize()+GamestateHeader::getSize(); 256 256 } 257 257 } … … 271 271 272 272 273 bool Gamestate::process( )274 { 275 return GamestateHandler::addGamestate(this, getClientID());273 bool Gamestate::process(orxonox::Host* host) 274 { 275 return host->addGamestate(this, getPeerID()); 276 276 } 277 277 … … 280 280 { 281 281 assert(data_); 282 assert(!header_ ->isCompressed());283 uLongf buffer = (uLongf)(((header_ ->getDataSize() + 12)*1.01)+1);282 assert(!header_.isCompressed()); 283 uLongf buffer = (uLongf)(((header_.getDataSize() + 12)*1.01)+1); 284 284 if(buffer==0) 285 285 return false; … … 289 289 uint8_t *source = data_ + GamestateHeader::getSize(); 290 290 int retval; 291 retval = compress( dest, &buffer, source, (uLong)(header_ ->getDataSize()) );291 retval = compress( dest, &buffer, source, (uLong)(header_.getDataSize()) ); 292 292 switch ( retval ) 293 293 { … … 299 299 300 300 //copy and modify header 301 GamestateHeader *temp = header_; 302 header_ = new GamestateHeader(ndata, temp); 301 GamestateHeader *temp = new GamestateHeader(data_); 302 header_.setData(ndata); 303 header_ = *temp; 303 304 delete temp; 304 305 //delete old data … … 306 307 //save new data 307 308 data_ = ndata; 308 header_ ->setCompSize( buffer );309 header_ ->setCompressed( true );310 COUT( 0) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;309 header_.setCompSize( buffer ); 310 header_.setCompressed( true ); 311 COUT(4) << "gamestate compress datasize: " << header_.getDataSize() << " compsize: " << header_.getCompSize() << std::endl; 311 312 return true; 312 313 } … … 316 317 { 317 318 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();319 assert(header_.isCompressed()); 320 COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_.getID() << ", baseid: " << header_.getBaseID() << ", datasize: " << header_.getDataSize() << ", compsize: " << header_.getCompSize() << std::endl; 321 uint32_t datasize = header_.getDataSize(); 322 uint32_t compsize = header_.getCompSize(); 322 323 uint32_t bufsize; 323 324 bufsize = datasize; … … 338 339 339 340 //copy over the header 340 GamestateHeader *temp = header_; 341 header_ = new GamestateHeader( data_, header_ ); 341 GamestateHeader* temp = new GamestateHeader( data_ ); 342 header_.setData(ndata); 343 header_ = *temp; 342 344 delete temp; 343 345 … … 357 359 //set new pointers 358 360 data_ = ndata; 359 header_ ->setCompressed( false );360 assert(header_ ->getDataSize()==datasize);361 assert(header_ ->getCompSize()==compsize);361 header_.setCompressed( false ); 362 assert(header_.getDataSize()==datasize); 363 assert(header_.getCompSize()==compsize); 362 364 return true; 363 365 } 364 366 365 367 368 inline void /*Gamestate::*/diffObject( uint8_t*& newDataPtr, uint8_t*& origDataPtr, uint8_t*& baseDataPtr, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ) 369 { 370 assert( objectHeader.getDataSize() == SynchronisableHeader(baseDataPtr).getDataSize() ); 371 372 uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData 373 // Check whether the whole object stayed the same 374 if( memcmp( origDataPtr+objectOffset, baseDataPtr+objectOffset, objectHeader.getDataSize()) == 0 ) 375 { 376 // COUT(4) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl; 377 origDataPtr += objectOffset + objectHeader.getDataSize(); // skip the whole object 378 baseDataPtr += objectOffset + objectHeader.getDataSize(); 379 sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); 380 } 381 else 382 { 383 // Now start to diff the Object 384 SynchronisableHeaderLight newObjectHeader(newDataPtr); 385 newObjectHeader = objectHeader; // copy over the objectheader 386 VariableID variableID = 0; 387 uint32_t diffedObjectOffset = SynchronisableHeaderLight::getSize(); 388 // iterate through all variables 389 while( objectOffset < objectHeader.getDataSize()+SynchronisableHeader::getSize() ) 390 { 391 // check whether variable changed and write id and copy over variable to the new stream 392 // otherwise skip variable 393 uint32_t varSize = *sizes; 394 assert( varSize == Synchronisable::getSynchronisable(objectHeader.getObjectID())->getVarSize(variableID) ); 395 if ( varSize != 0 ) 396 { 397 if ( memcmp(origDataPtr+objectOffset, baseDataPtr+objectOffset, varSize) != 0 ) 398 { 399 *(VariableID*)(newDataPtr+diffedObjectOffset) = variableID; // copy over the variableID 400 diffedObjectOffset += sizeof(VariableID); 401 memcpy( newDataPtr+diffedObjectOffset, origDataPtr+objectOffset, varSize ); 402 diffedObjectOffset += varSize; 403 objectOffset += varSize; 404 } 405 else 406 { 407 objectOffset += varSize; 408 } 409 } 410 411 ++variableID; 412 ++sizes; 413 } 414 415 // if there are variables from this object with 0 size left in sizes 416 if( Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() != variableID ) 417 sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() - variableID; 418 419 newObjectHeader.setDiffed(true); 420 newObjectHeader.setDataSize(diffedObjectOffset-SynchronisableHeaderLight::getSize()); 421 assert(objectOffset == objectHeader.getDataSize()+SynchronisableHeader::getSize()); 422 assert(newObjectHeader.getDataSize()>0); 423 424 origDataPtr += objectOffset; 425 baseDataPtr += objectOffset; 426 newDataPtr += diffedObjectOffset; 427 } 428 } 429 430 inline void /*Gamestate::*/copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ) 431 { 432 // COUT(4) << "docopy" << endl; 433 // Just copy over the whole Object 434 memcpy( newData, origData, objectHeader.getDataSize()+SynchronisableHeader::getSize() ); 435 SynchronisableHeader(newData).setDiffed(false); 436 437 newData += objectHeader.getDataSize()+SynchronisableHeader::getSize(); 438 origData += objectHeader.getDataSize()+SynchronisableHeader::getSize(); 439 // SynchronisableHeader baseHeader( baseData ); 440 // baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize(); 441 // COUT(4) << "copy " << h.getObjectID() << endl; 442 // COUT(4) << "copy " << h.getObjectID() << ":"; 443 sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); 444 // for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i ) 445 // { 446 // // COUT(4) << " " << *sizes; 447 // ++sizes; 448 // } 449 // COUT(4) << endl; 450 } 451 452 inline bool findObject(uint8_t*& dataPtr, uint8_t* endPtr, SynchronisableHeader& objectHeader) 453 { 454 // Some assertions to make sure the dataPtr is valid (pointing to a SynchronisableHeader) 455 { 456 SynchronisableHeader htemp2(dataPtr); 457 assert(htemp2.getClassID()<500); 458 assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 459 assert(htemp2.isDiffed()==false); 460 } 461 uint32_t objectID = objectHeader.getObjectID(); 462 while ( dataPtr < endPtr ) 463 { 464 SynchronisableHeader htemp(dataPtr); 465 assert( htemp.getDataSize()!=0 ); 466 if ( htemp.getObjectID() == objectID ) 467 { 468 assert( objectHeader.getClassID() == htemp.getClassID() ); 469 assert( objectHeader.getCreatorID() == htemp.getCreatorID() ); 470 return true; 471 } 472 { 473 if( dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize() < endPtr ) 474 { 475 SynchronisableHeader htemp2(dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize()); 476 assert(htemp2.getClassID()<500); 477 assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 478 assert(htemp2.isDiffed()==false); 479 } 480 } 481 dataPtr += htemp.getDataSize()+SynchronisableHeader::getSize(); 482 483 } 484 assert(dataPtr == endPtr); 485 486 return false; 487 } 488 366 489 Gamestate* Gamestate::diffVariables(Gamestate *base) 367 490 { 368 491 assert(this && base); assert(data_ && base->data_); 369 assert(!header_->isCompressed() && !base->header_->isCompressed()); 370 assert(!header_->isDiffed()); 492 assert(!header_.isCompressed() && !base->header_.isCompressed()); 493 assert(!header_.isDiffed()); 494 assert( header_.getDataSize() && base->header_.getDataSize() ); 371 495 372 496 373 497 // *** first do a raw diff of the two gamestates 374 498 375 uint8_t *baseData = GAMESTATE_START(base->data_);376 uint8_t *origData = GAMESTATE_START(this->data_);377 uint 32_t origLength = header_->getDataSize();378 uint 32_t baseLength = base->header_->getDataSize();379 380 assert( origLength && baseLength);381 382 uint8_t *nData = new uint8_t[origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_]; // this is the maximum size needed in the worst case383 uint 8_t *dest = GAMESTATE_START(nData);384 385 uint 32_t baseOffset = 0; //offset in the diffed stream386 uint32_t origOffset = 0; //offset in the new stream with removed 0's 387 std::vector<uint32_t>::iterator sizes = this->sizes_.begin();388 389 while( orig Offset < origLength)499 uint8_t *baseDataPtr = GAMESTATE_START(base->data_); 500 uint8_t *origDataPtr = GAMESTATE_START(this->data_); 501 uint8_t *origDataEnd = origDataPtr + header_.getDataSize(); 502 uint8_t *baseDataEnd = baseDataPtr + base->header_.getDataSize(); 503 // uint32_t origLength = header_.getDataSize(); 504 // uint32_t baseLength = base->header_.getDataSize(); 505 506 // Allocate new space for diffed gamestate 507 uint32_t newDataSize = header_.getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_; 508 uint8_t *newData = new uint8_t[newDataSize]; // this is the maximum size needed in the worst case 509 uint8_t *destDataPtr = GAMESTATE_START(newData); 510 511 std::vector<uint32_t>::iterator sizesIt = this->sizes_.begin(); 512 513 while( origDataPtr < origDataEnd ) 390 514 { 391 515 //iterate through all objects 392 516 393 SynchronisableHeader h(origData+origOffset);517 SynchronisableHeader origHeader(origDataPtr); 394 518 395 519 // Find (if possible) the current object in the datastream of the old gamestate 396 520 // Start at the current offset position 397 if(baseOffset >= baseLength) 398 baseOffset = 0; 399 uint8_t* temp = baseData + baseOffset; 400 uint32_t objectID = h.getObjectID(); 401 assert(temp < baseData+baseLength); 402 assert(dest < nData + origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_); 403 assert(sizes != this->sizes_.end()); 404 while ( temp < baseData+baseLength ) 405 { 406 SynchronisableHeader htemp(temp); 407 assert( htemp.getDataSize()!=0 ); 408 if ( htemp.getObjectID() == objectID ) 409 { 410 assert( h.getClassID() == htemp.getClassID() ); 411 goto DODIFF; 412 } 413 // { 414 // SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize()); 415 // if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength ) 416 // { 417 // assert(htemp2.getClassID()<500); 418 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 419 // assert(htemp2.isDiffed()==false); 420 // } 421 // } 422 temp += htemp.getDataSize()+SynchronisableHeader::getSize(); 521 if(baseDataPtr == baseDataEnd) 522 baseDataPtr = GAMESTATE_START(base->data_); 523 uint8_t* oldBaseDataPtr = baseDataPtr; 524 525 assert(baseDataPtr < baseDataEnd); 526 assert(destDataPtr < newData + newDataSize); 527 assert(sizesIt != this->sizes_.end()); 528 529 assert(Synchronisable::getSynchronisable(origHeader.getObjectID())); 530 assert(ClassByID(origHeader.getClassID())); 531 assert(origHeader.getDataSize() < 500); 532 533 bool diffedObject = false; 534 if( findObject(baseDataPtr, baseDataEnd, origHeader) ) 535 { 536 SynchronisableHeader baseHeader(baseDataPtr); 537 assert(Synchronisable::getSynchronisable(baseHeader.getObjectID())); 538 assert(ClassByID(baseHeader.getClassID())); 539 assert(baseHeader.getDataSize() < 500); 540 if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() ) 541 { 542 // COUT(4) << "diffing object in order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 543 diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 544 diffedObject = true; 545 } 546 else 547 { 548 // COUT(4) << "copy object because of different data sizes (1): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 549 copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 550 assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); 551 } 423 552 424 553 } 425 // If not found start looking at the beginning 426 assert( temp==baseData+baseLength ); 427 temp = baseData; 428 // { 429 // SynchronisableHeader htemp2(temp); 430 // if( temp < baseData+baseLength ) 431 // { 432 // assert(htemp2.getClassID()<500); 433 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 434 // assert(htemp2.isDiffed()==false); 435 // } 436 // } 437 while ( temp < baseData+baseOffset ) 438 { 439 SynchronisableHeader htemp(temp); 440 if ( htemp.getObjectID() == objectID ) 441 { 442 assert( h.getClassID() == htemp.getClassID() ); 443 goto DODIFF; 444 } 445 // { 446 // SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize()); 447 // if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength ) 448 // { 449 // assert(htemp2.getClassID()<500); 450 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 451 // assert(htemp2.isDiffed()==false); 452 // } 453 // } 454 temp += htemp.getDataSize()+SynchronisableHeader::getSize(); 455 } 456 // Object is new, thus never transmitted -> just copy over 457 goto DOCOPY; 458 459 460 DODIFF: 461 { 462 // COUT(4) << "dodiff" << endl; 463 // if(baseOffset==0) 464 // { 465 // assert(origOffset==0); 466 // } 467 uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData 468 // Check whether the whole object stayed the same 469 if( memcmp( origData+origOffset+objectOffset, temp+objectOffset, h.getDataSize()) == 0 ) 470 { 471 // COUT(4) << "skip object" << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl; 472 origOffset += objectOffset+ h.getDataSize(); // skip the whole object 473 baseOffset = temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData; 474 sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); 554 else 555 { 556 assert( baseDataPtr == baseDataEnd ); 557 baseDataPtr = GAMESTATE_START(base->data_); 558 if( findObject(baseDataPtr, oldBaseDataPtr, origHeader) ) 559 { 560 SynchronisableHeader baseHeader(baseDataPtr); 561 assert(Synchronisable::getSynchronisable(baseHeader.getObjectID())); 562 assert(ClassByID(baseHeader.getClassID())); 563 assert(baseHeader.getDataSize() < 500); 564 if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() ) 565 { 566 // COUT(4) << "diffing object out of order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 567 diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 568 diffedObject = true; 569 } 570 else 571 { 572 // COUT(4) << "copy object because of different data sizes (2): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 573 copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 574 assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); 575 } 475 576 } 476 577 else 477 578 { 478 // if( Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() == "Bot" ) 479 // COUT(0) << "blub" << endl; 480 // COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl; 481 // COUT(4) << "diff " << h.getObjectID() << ":"; 482 // Now start to diff the Object 483 SynchronisableHeaderLight h2(dest); 484 h2 = h; // copy over the objectheader 485 VariableID variableID = 0; 486 uint32_t newObjectOffset = SynchronisableHeaderLight::getSize(); 487 // iterate through all variables 488 while( objectOffset < h.getDataSize()+SynchronisableHeader::getSize() ) 489 { 490 // check whether variable changed and write id and copy over variable to the new stream 491 // otherwise skip variable 492 assert(sizes != this->sizes_.end()); 493 uint32_t varSize = *sizes; 494 assert( varSize == Synchronisable::getSynchronisable(h.getObjectID())->getVarSize(variableID) ); 495 if ( varSize != 0 ) 496 { 497 if ( memcmp(origData+origOffset+objectOffset, temp+objectOffset, varSize) != 0 ) 498 { 499 // COUT(4) << "copy variable" << endl; 500 *(VariableID*)(dest+newObjectOffset) = variableID; // copy over the variableID 501 newObjectOffset += sizeof(VariableID); 502 memcpy( dest+newObjectOffset, origData+origOffset+objectOffset, varSize ); 503 newObjectOffset += varSize; 504 objectOffset += varSize; 505 } 506 else 507 { 508 // COUT(4) << "skip variable" << endl; 509 objectOffset += varSize; 510 } 511 } 512 // else 513 // COUT(4) << "varsize 0" << endl; 514 515 ++variableID; 516 ++sizes; 517 } 518 519 if( Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() != variableID ) 520 sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() - variableID; 521 // COUT(4) << endl; 522 h2.setDiffed(true); 523 h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize()); 524 assert(objectOffset == h.getDataSize()+SynchronisableHeader::getSize()); 525 origOffset += objectOffset; 526 // baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData; 527 //baseOffset += objectOffset; 528 // SynchronisableHeader htemp(temp); 529 // baseOffset += SynchronisableHeader::getSize() + htemp.getDataSize(); 530 // { 531 // SynchronisableHeader htemp2( baseData+(temp-baseData+objectOffset) ); 532 // if( baseData+(temp-baseData+objectOffset) < baseData+baseLength ) 533 // { 534 // assert(htemp2.getClassID()<500); 535 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 536 // assert(htemp2.isDiffed()==false); 537 // } 538 // } 539 baseOffset = temp-baseData + objectOffset; 540 dest += newObjectOffset; 541 } 542 543 continue; 544 } 545 546 DOCOPY: 547 { 548 // COUT(4) << "docopy" << endl; 549 // Just copy over the whole Object 550 memcpy( dest, origData+origOffset, h.getDataSize()+SynchronisableHeader::getSize() ); 551 dest += h.getDataSize()+SynchronisableHeader::getSize(); 552 origOffset += h.getDataSize()+SynchronisableHeader::getSize(); 553 assert( Synchronisable::getSynchronisable(h.getObjectID()) ); 554 // COUT(4) << "copy " << h.getObjectID() << endl; 555 // COUT(4) << "copy " << h.getObjectID() << ":"; 556 //sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); 557 for( unsigned int i = 0; i < Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); ++i ) 558 { 559 // COUT(4) << " " << *sizes; 560 ++sizes; 561 } 562 // COUT(4) << endl; 563 assert(sizes != this->sizes_.end() || origOffset>=origLength); 564 continue; 565 } 566 } 567 568 569 Gamestate *g = new Gamestate(nData, getClientID()); 570 assert(g->header_); 571 *(g->header_) = *header_; 572 g->header_->setBaseID( base->getID() ); 573 g->header_->setDataSize(dest - nData - GamestateHeader::getSize()); 579 // COUT(4) << "copy object: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 580 assert(baseDataPtr == oldBaseDataPtr); 581 copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 582 assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); 583 } 584 } 585 } 586 assert(sizesIt==this->sizes_.end()); 587 588 589 Gamestate *g = new Gamestate(newData, getPeerID()); 590 (g->header_) = header_; 591 g->header_.setBaseID( base->getID() ); 592 g->header_.setDataSize(destDataPtr - newData - GamestateHeader::getSize()); 574 593 g->flags_=flags_; 575 594 g->packetDirection_ = packetDirection_; … … 579 598 580 599 581 Gamestate* Gamestate::diffData(Gamestate *base)600 /*Gamestate* Gamestate::diffData(Gamestate *base) 582 601 { 583 602 assert(this && base); assert(data_ && base->data_); 584 assert(!header_ ->isCompressed() && !base->header_->isCompressed());585 assert(!header_ ->isDiffed());603 assert(!header_.isCompressed() && !base->header_.isCompressed()); 604 assert(!header_.isDiffed()); 586 605 587 606 uint8_t *basep = GAMESTATE_START(base->data_); 588 607 uint8_t *gs = GAMESTATE_START(this->data_); 589 uint32_t dest_length = header_ ->getDataSize();608 uint32_t dest_length = header_.getDataSize(); 590 609 591 610 if(dest_length==0) … … 595 614 uint8_t *dest = GAMESTATE_START(ndata); 596 615 597 rawDiff( dest, gs, basep, header_ ->getDataSize(), base->header_->getDataSize() );616 rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); 598 617 #ifndef NDEBUG 599 618 uint8_t *dest2 = new uint8_t[dest_length]; 600 rawDiff( dest2, dest, basep, header_ ->getDataSize(), base->header_->getDataSize() );619 rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() ); 601 620 assert( memcmp( dest2, gs, dest_length) == 0 ); 602 621 delete dest2; … … 606 625 assert(g->header_); 607 626 *(g->header_) = *header_; 608 g->header_ ->setDiffed( true );609 g->header_ ->setBaseID( base->getID() );627 g->header_.setDiffed( true ); 628 g->header_.setBaseID( base->getID() ); 610 629 g->flags_=flags_; 611 630 g->packetDirection_ = packetDirection_; … … 619 638 { 620 639 assert(this && base); assert(data_ && base->data_); 621 assert(!header_ ->isCompressed() && !base->header_->isCompressed());622 assert(header_ ->isDiffed());640 assert(!header_.isCompressed() && !base->header_.isCompressed()); 641 assert(header_.isDiffed()); 623 642 624 643 uint8_t *basep = GAMESTATE_START(base->data_); 625 644 uint8_t *gs = GAMESTATE_START(this->data_); 626 uint32_t dest_length = header_ ->getDataSize();645 uint32_t dest_length = header_.getDataSize(); 627 646 628 647 if(dest_length==0) … … 632 651 uint8_t *dest = ndata + GamestateHeader::getSize(); 633 652 634 rawDiff( dest, gs, basep, header_ ->getDataSize(), base->header_->getDataSize() );653 rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); 635 654 636 655 Gamestate *g = new Gamestate(ndata, getClientID()); 637 656 assert(g->header_); 638 657 *(g->header_) = *header_; 639 g->header_ ->setDiffed( false );658 g->header_.setDiffed( false ); 640 659 g->flags_=flags_; 641 660 g->packetDirection_ = packetDirection_; … … 670 689 } 671 690 assert(j==datalength); 672 } 673 674 675 Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){691 }*/ 692 693 694 /*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ 676 695 assert(data_); 677 696 std::list<obj>::iterator it; 678 697 679 698 // allocate memory for new data 680 uint8_t *gdata = new uint8_t[header_ ->getDataSize()+GamestateHeader::getSize()];699 uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()]; 681 700 // create a gamestate out of it 682 701 Gamestate *gs = new Gamestate(gdata); … … 694 713 695 714 //call TrafficControl 696 TrafficControl::getInstance()->processObjectList( clientID, header_ ->getID(), dataVector_ );715 TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ ); 697 716 698 717 //copy in the zeros … … 725 744 #ifndef NDEBUG 726 745 uint32_t origsize = destsize; 727 while ( origsize < header_ ->getDataSize() )746 while ( origsize < header_.getDataSize() ) 728 747 { 729 748 SynchronisableHeader oldobjectheader(origdata); … … 732 751 origsize += objectsize; 733 752 } 734 assert(origsize==header_ ->getDataSize());753 assert(origsize==header_.getDataSize()); 735 754 assert(destsize!=0); 736 755 #endif 737 gs->header_ ->setDataSize( destsize );756 gs->header_.setDataSize( destsize ); 738 757 return gs; 739 } 740 741 742 uint32_t Gamestate::calcGamestateSize( int32_t id, uint8_t mode)758 }*/ 759 760 761 uint32_t Gamestate::calcGamestateSize(uint32_t id, uint8_t mode) 743 762 { 744 763 uint32_t size = 0; -
code/trunk/src/libraries/network/packet/Gamestate.h
r7163 r7801 42 42 #include "Packet.h" 43 43 44 namespace orxonox { 44 namespace orxonox 45 { 45 46 46 namespace packet { 47 namespace packet 48 { 49 50 static const uint8_t GAMESTATE_MODE_SERVER = 0x1; 51 static const uint8_t GAMESTATE_MODE_CLIENT = 0x2; 47 52 48 class _NetworkExport GamestateHeader{ 53 class _NetworkExport GamestateHeader 54 { 49 55 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()); } 56 GamestateHeader(){ data_=0; } 57 GamestateHeader(uint8_t* data) 58 { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } 59 /*GamestateHeader(uint8_t* data, GamestateHeader* h) 60 { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }*/ 61 void setData(uint8_t* data) 62 { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } 53 63 static inline uint32_t getSize() 54 { return 21; }64 { return 21; } 55 65 56 inline int32_t getID() const57 { assert(data_); return *(int32_t*)(data_+4); }58 inline void setID( int32_t id)59 { assert(data_); *(int32_t*)(data_+4) = id; }66 inline uint32_t getID() const 67 { assert(data_); return *(uint32_t*)(data_+4); } 68 inline void setID(uint32_t id) 69 { assert(data_); *(uint32_t*)(data_+4) = id; } 60 70 61 inline int32_t getBaseID() const62 { assert(data_); return *(int32_t*)(data_+8); }63 inline void setBaseID( int32_t id)64 { assert(data_); *(int32_t*)(data_+8) = id; }71 inline uint32_t getBaseID() const 72 { assert(data_); return *(uint32_t*)(data_+8); } 73 inline void setBaseID(uint32_t id) 74 { assert(data_); *(uint32_t*)(data_+8) = id; } 65 75 66 76 inline uint32_t getDataSize() const 67 { assert(data_); return *(uint32_t*)(data_+12); }77 { assert(data_); return *(uint32_t*)(data_+12); } 68 78 inline void setDataSize(uint32_t size) 69 { assert(data_); *(uint32_t*)(data_+12) = size; }79 { assert(data_); *(uint32_t*)(data_+12) = size; } 70 80 71 81 inline uint32_t getCompSize() const … … 75 85 76 86 inline bool isDiffed() const 77 { assert(data_); return *(int8_t*)(data_+20) & 0x1; }87 { assert(data_); return *(int8_t*)(data_+20) & 0x1; } 78 88 inline void setDiffed(bool b) 79 { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }89 { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); } 80 90 81 91 inline bool isComplete() const 82 { assert(data_); return *(int8_t*)(data_+20) & 0x2; }92 { assert(data_); return *(int8_t*)(data_+20) & 0x2; } 83 93 inline void setComplete(bool b) 84 { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }94 { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); } 85 95 86 96 inline bool isCompressed() const 87 { assert(data_); return *(int8_t*)(data_+20) & 0x4; }97 { assert(data_); return *(int8_t*)(data_+20) & 0x4; } 88 98 inline void setCompressed(bool b) 89 { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }99 { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); } 90 100 91 101 inline void operator=(GamestateHeader& h) 92 { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); }102 { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); } 93 103 private: 94 uint8_t *data_;104 uint8_t* data_; 95 105 96 106 }; … … 99 109 @author Oliver Scheuss 100 110 */ 101 class _NetworkExport Gamestate: public Packet{ 111 class _NetworkExport Gamestate: public Packet 112 { 102 113 public: 103 114 Gamestate(); … … 110 121 bool collectData(int id, uint8_t mode=0x0); 111 122 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(); }123 inline uint32_t getID() const { return header_.getID(); } 124 inline bool isDiffed() const { return header_.isDiffed(); } 125 inline bool isCompressed() const { return header_.isCompressed(); } 126 inline int32_t getBaseID() const { return header_.getBaseID(); } 127 inline uint32_t getDataSize() const { return header_.getDataSize(); } 117 128 Gamestate* diffVariables(Gamestate *base); 118 Gamestate* diffData(Gamestate *base);119 Gamestate *undiff(Gamestate *base);120 Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);129 // Gamestate* diffData(Gamestate *base); 130 // Gamestate *undiff(Gamestate *base); 131 // Gamestate* doSelection(unsigned int clientID, unsigned int targetSize); 121 132 bool compressData(); 122 133 bool decompressData(); … … 125 136 // Packet functions 126 137 private: 127 void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength);128 inline uint32_t findObject( const SynchronisableHeader& header, uint8_t* mem, uint32_t dataLength, uint32_t startPosition = 0 );138 // void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength); 139 // inline uint32_t findObject( const SynchronisableHeader& header, uint8_t* mem, uint32_t dataLength, uint32_t startPosition = 0 ); 129 140 virtual uint32_t getSize() const; 130 virtual inline bool process(); 131 uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0); 141 virtual bool process(orxonox::Host* host); 142 uint32_t calcGamestateSize(uint32_t id, uint8_t mode=0x0); 143 // inline void diffObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ); 144 // inline void copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ); 132 145 133 146 std::list<obj> dataVector_; 134 GamestateHeader *header_;147 GamestateHeader header_; 135 148 std::vector<uint32_t> sizes_; 136 149 uint32_t nrOfVariables_; -
code/trunk/src/libraries/network/packet/Packet.cc
r7163 r7801 35 35 #include <enet/enet.h> 36 36 #include <boost/static_assert.hpp> 37 #include <boost/thread/mutex.hpp> 37 38 38 39 #include "util/Debug.h" … … 53 54 54 55 // Make sure we assume the right values 55 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable) == static_cast<int>(ENET_PACKET_FLAG_RELIABLE));56 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequence ) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED));57 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate) == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE));56 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable) == static_cast<int>(ENET_PACKET_FLAG_RELIABLE)); 57 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequenced) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED)); 58 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate) == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE)); 58 59 59 60 #define PACKET_FLAG_DEFAULT PacketFlag::NoAllocate … … 61 62 62 63 std::map<size_t, Packet *> Packet::packetMap_; 64 boost::mutex Packet::packetMapMutex_; 63 65 64 66 Packet::Packet() … … 66 68 flags_ = PACKET_FLAG_DEFAULT; 67 69 packetDirection_ = Direction::Outgoing; 68 clientID_=0;70 peerID_=0; 69 71 data_=0; 70 72 enetPacket_=0; … … 72 74 } 73 75 74 Packet::Packet(uint8_t *data, unsigned int clientID)76 Packet::Packet(uint8_t *data, unsigned int peerID) 75 77 { 76 78 flags_ = PACKET_FLAG_DEFAULT; 77 79 packetDirection_ = Direction::Incoming; 78 clientID_=clientID;80 peerID_=peerID; 79 81 data_=data; 80 82 enetPacket_=0; … … 87 89 flags_=p.flags_; 88 90 packetDirection_ = p.packetDirection_; 89 clientID_ = p.clientID_;91 peerID_ = p.peerID_; 90 92 if(p.data_){ 91 93 data_ = new uint8_t[p.getSize()]; … … 123 125 } 124 126 125 bool Packet::send( ){127 bool Packet::send(orxonox::Host* host){ 126 128 if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional ){ 127 129 assert(0); … … 142 144 // Assures we don't create a packet and destroy it right after in another thread 143 145 // without having a reference in the packetMap_ 146 Packet::packetMapMutex_.lock(); 144 147 packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this; 148 Packet::packetMapMutex_.unlock(); 145 149 } 146 150 } … … 164 168 // ENetPacket *temp = enetPacket_; 165 169 // enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet 166 if(!Host::addPacket( enetPacket_, clientID_)) 167 enet_packet_destroy(this->enetPacket_); // if we could not add the packet to the enet queue delete it manually 170 if( this->flags_ & PacketFlag::Reliable ) 171 host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_DEFAULT); 172 else 173 host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_UNRELIABLE); 168 174 return true; 169 175 } … … 172 178 uint8_t *data = packet->data; 173 179 assert(ClientInformation::findClient(&peer->address)->getID() != static_cast<unsigned int>(-2) || !Host::isServer()); 174 unsigned int clientID = ClientInformation::findClient(&peer->address)->getID(); 180 unsigned int peerID = ClientInformation::findClient(&peer->address)->getID(); 181 // HACK 182 if( peerID==static_cast<unsigned int>(-2)) 183 peerID = NETWORK_PEER_ID_SERVER; 175 184 Packet *p = 0; 176 COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl;185 // COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl; 177 186 switch( *(Type::Value *)(data + _PACKETID) ) 178 187 { 179 188 case Type::Acknowledgement: 180 COUT(5) << "ack" << std::endl;181 p = new Acknowledgement( data, clientID );189 // COUT(5) << "ack" << std::endl; 190 p = new Acknowledgement( data, peerID ); 182 191 break; 183 192 case Type::Chat: 184 COUT(5) << "chat" << std::endl;185 p = new Chat( data, clientID );193 // COUT(5) << "chat" << std::endl; 194 p = new Chat( data, peerID ); 186 195 break; 187 196 case Type::ClassID: 188 COUT(5) << "classid" << std::endl;189 p = new ClassID( data, clientID );197 // COUT(5) << "classid" << std::endl; 198 p = new ClassID( data, peerID ); 190 199 break; 191 200 case Type::Gamestate: 192 COUT(5) << "gamestate" << std::endl; 193 // TODO: remove brackets 194 p = new Gamestate( data, clientID ); 201 // COUT(5) << "gamestate" << std::endl; 202 p = new Gamestate( data, peerID ); 195 203 break; 196 204 case Type::Welcome: 197 COUT(5) << "welcome" << std::endl;198 p = new Welcome( data, clientID );205 // COUT(5) << "welcome" << std::endl; 206 p = new Welcome( data, peerID ); 199 207 break; 200 208 case Type::DeleteObjects: 201 COUT(5) << "deleteobjects" << std::endl;202 p = new DeleteObjects( data, clientID );209 // COUT(5) << "deleteobjects" << std::endl; 210 p = new DeleteObjects( data, peerID ); 203 211 break; 204 212 case Type::FunctionCalls: 205 COUT(5) << "functionCalls" << std::endl;206 p = new FunctionCalls( data, clientID );213 // COUT(5) << "functionCalls" << std::endl; 214 p = new FunctionCalls( data, peerID ); 207 215 break; 208 216 case Type::FunctionIDs: 209 COUT(5) << "functionIDs" << std::endl;210 p = new FunctionIDs( data, clientID );217 // COUT(5) << "functionIDs" << std::endl; 218 p = new FunctionIDs( data, peerID ); 211 219 break; 212 220 default: 213 assert(0); //TODO: repair this221 assert(0); 214 222 break; 215 223 } … … 229 237 void Packet::deletePacket(ENetPacket *enetPacket){ 230 238 // Get our Packet from a global map with all Packets created in the send() method of Packet. 239 Packet::packetMapMutex_.lock(); 231 240 std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket)); 232 241 assert(it != packetMap_.end()); … … 235 244 delete it->second; 236 245 packetMap_.erase(it); 237 COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl; 246 Packet::packetMapMutex_.unlock(); 247 // COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl; 238 248 } 239 249 -
code/trunk/src/libraries/network/packet/Packet.h
r7490 r7801 68 68 virtual unsigned char *getData(){ return data_; }; 69 69 virtual unsigned int getSize() const =0; 70 virtual bool process( )=0;70 virtual bool process(orxonox::Host* host)=0; 71 71 inline uint32_t getFlags() 72 72 { return flags_; } 73 inline int getClientID() 74 { return clientID_; } 75 inline void setClientID( int id ) 76 { clientID_ = id; } 73 inline int getPeerID() 74 { return peerID_; } 75 inline void setPeerID( int id ) 76 { peerID_ = id; } 77 inline bool isReliable() 78 { return this->flags_ & PacketFlag::Reliable; } 79 inline uint32_t getRequiredGamestateID() 80 { return this->requiredGamestateID_; } 77 81 78 virtual bool send( );82 virtual bool send(orxonox::Host* host); 79 83 protected: 80 84 Packet(); 81 Packet(uint8_t *data, unsigned int clientID);85 Packet(uint8_t *data, unsigned int peerID); 82 86 // Packet(ENetPacket *packet, ENetPeer *peer); 83 87 inline bool isDataENetAllocated() const … … 85 89 86 90 uint32_t flags_; 87 unsigned int clientID_; 91 unsigned int peerID_; 92 uint32_t requiredGamestateID_; 88 93 Direction::Value packetDirection_; 89 94 /** Pointer to the data. Be careful when deleting it because it might … … 96 101 private: 97 102 static std::map<size_t, Packet *> packetMap_; 103 static boost::mutex packetMapMutex_; 98 104 ENetPacket *enetPacket_; 99 105 }; -
code/trunk/src/libraries/network/packet/ServerInformation.cc
r7461 r7801 21 21 * 22 22 * Author: 23 * Fabian 'x3n' Landau23 * Oliver Scheuss 24 24 * Co-authors: 25 25 * ... … … 58 58 char* ack = new char[strlen(LAN_DISCOVERY_ACK)+1]; 59 59 loadAndIncrease((char*&)ack, temp); 60 assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0); 60 61 /* Fabian, what is this used for? it crashes the masterserver, hence commenting it */ 62 // written by Oli: this is just to make sure that loadAndIncrease really writes the whole ACK string into char* ack 63 // assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0); 64 61 65 // Save Server Name 62 66 loadAndIncrease(this->serverName_, temp); -
code/trunk/src/libraries/network/packet/ServerInformation.h
r7459 r7801 21 21 * 22 22 * Author: 23 * Fabian 'x3n' Landau23 * Oliver Scheuss 24 24 * Co-authors: 25 25 * ... … … 50 50 std::string getServerName() { return this->serverName_; } 51 51 void setServerName(std::string name) { this->serverName_ = name; } 52 void setServerIP( std::string IP ) { this->serverIP_ = IP; } 52 53 uint32_t getServerRTT() { return this->serverRTT_; } 53 54 -
code/trunk/src/libraries/network/packet/Welcome.cc
r5781 r7801 73 73 } 74 74 75 bool Welcome::process( ){75 bool Welcome::process(orxonox::Host* host){ 76 76 uint32_t clientID; 77 77 clientID = *(uint32_t *)(data_ + _CLIENTID ); 78 78 assert(*(uint32_t *)(data_ + _ENDIANTEST ) == 0xFEDC4321); 79 Host::setClientID(clientID);79 host->setClientID(clientID); 80 80 COUT(3) << "Welcome set clientId: " << clientID << endl; 81 81 Synchronisable::setClient(true); -
code/trunk/src/libraries/network/packet/Welcome.h
r6073 r7801 47 47 uint8_t *getData(); 48 48 inline unsigned int getSize() const; 49 bool process();49 virtual bool process(orxonox::Host* host); 50 50 51 51 private:
Note: See TracChangeset
for help on using the changeset viewer.