Changeset 2759 for code/trunk/src/network/packet
- Timestamp:
- Mar 8, 2009, 4:51:27 PM (16 years ago)
- Location:
- code/trunk/src/network/packet
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/packet/ClassID.cc
r2669 r2759 31 31 #include "ClassID.h" 32 32 #include "core/CoreIncludes.h" 33 #include "core/Factory.h" 33 34 #include <cstring> 35 #include <string> 34 36 #include <assert.h> 37 #include <map> 38 #include <queue> 35 39 36 40 namespace orxonox { … … 38 42 39 43 44 #define PACKET_FLAGS_CLASSID ENET_PACKET_FLAG_RELIABLE 45 #define _PACKETID 0 40 46 41 ClassID::ClassID( unsigned int classID, std::string className ) 42 : Packet() 43 { 47 48 ClassID::ClassID( ) : Packet(){ 49 Identifier *id; 50 std::string classname; 51 unsigned int nrOfClasses=0; 52 unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nrofclasses 53 uint32_t network_id; 44 54 flags_ = flags_ | PACKET_FLAGS_CLASSID; 45 classNameLength_=className.length()+1; 46 assert(getSize()); 47 data_=new unsigned char[ getSize() ]; 48 assert(data_); 49 *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::ClassID; 50 *(unsigned int *)(data_ + _CLASSID ) = classID; 51 *(unsigned int *)(data_ + _CLASSNAMELENGTH ) = classNameLength_; 52 memcpy( data_+_CLASSNAME, (void *)className.c_str(), classNameLength_ ); 55 std::queue<std::pair<uint32_t, std::string> > tempQueue; 56 57 //calculate total needed size (for all strings and integers) 58 std::map<std::string, Identifier*>::const_iterator it = Factory::getFactoryMapBegin(); 59 for(;it != Factory::getFactoryMapEnd();++it){ 60 id = (*it).second; 61 if(id == NULL) 62 continue; 63 classname = id->getName(); 64 network_id = id->getNetworkID(); 65 if(network_id==0) 66 COUT(3) << "we got a null class id: " << id->getName() << std::endl; 67 // now push the network id and the classname to the stack 68 tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) ); 69 ++nrOfClasses; 70 packetSize += (classname.size()+1)+sizeof(uint32_t)+sizeof(uint32_t); 71 } 72 73 this->data_=new uint8_t[ packetSize ]; 74 //set the appropriate packet id 75 assert(this->data_); 76 *(ENUM::Type *)(this->data_ + _PACKETID ) = ENUM::ClassID; 77 78 uint8_t *temp=data_+sizeof(uint32_t); 79 // save the number of all classes 80 *(uint32_t*)temp = nrOfClasses; 81 temp += sizeof(uint32_t); 82 83 // now save all classids and classnames 84 std::pair<uint32_t, std::string> tempPair; 85 while( !tempQueue.empty() ){ 86 tempPair = tempQueue.front(); 87 tempQueue.pop(); 88 *(uint32_t*)temp = tempPair.first; 89 *(uint32_t*)(temp+sizeof(uint32_t)) = tempPair.second.size()+1; 90 memcpy(temp+2*sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size()+1); 91 temp+=2*sizeof(uint32_t)+tempPair.second.size()+1; 92 } 93 94 COUT(0) << "classid packetSize is " << packetSize << endl; 95 53 96 } 54 97 … … 56 99 : Packet(data, clientID) 57 100 { 58 memcpy( (void *)&classNameLength_, &data[ _CLASSNAMELENGTH ], sizeof(classNameLength_) );59 101 } 60 102 … … 63 105 } 64 106 65 unsigned int ClassID::getSize() const{ 66 return sizeof(packet::ENUM::Type) + 2*sizeof(uint32_t) + classNameLength_; 107 uint32_t ClassID::getSize() const{ 108 uint8_t *temp = data_+sizeof(uint32_t); // packet identification 109 uint32_t totalsize = sizeof(uint32_t); // packet identification 110 uint32_t nrOfClasses = *(uint32_t*)temp; 111 temp += sizeof(uint32_t); 112 totalsize += sizeof(uint32_t); // storage size for nr of all classes 113 114 for(unsigned int i=0; i<nrOfClasses; i++){ 115 totalsize += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); 116 } 117 return totalsize; 67 118 } 68 119 120 69 121 bool ClassID::process(){ 70 COUT(3) << "processing classid: " << getClassID() << " name: " << getClassName() << std::endl; 71 Identifier *id=ClassByString( std::string(getClassName()) ); 72 if(id==NULL) 73 return false; 74 id->setNetworkID( getClassID() ); 122 int nrOfClasses; 123 uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid 124 uint32_t networkID; 125 uint32_t stringsize; 126 unsigned char *classname; 127 128 129 //clean the map of network ids 130 Factory::cleanNetworkIDs(); 131 132 COUT(4) << "=== processing classids: " << endl; 133 std::pair<uint32_t, std::string> tempPair; 134 Identifier *id; 135 // read the total number of classes 136 nrOfClasses = *(uint32_t*)temp; 137 temp += sizeof(uint32_t); 138 139 for( int i=0; i<nrOfClasses; i++){ 140 networkID = *(uint32_t*)temp; 141 stringsize = *(uint32_t*)(temp+sizeof(uint32_t)); 142 classname = temp+2*sizeof(uint32_t); 143 id=ClassByString( std::string((const char*)classname) ); 144 COUT(0) << "processing classid: " << networkID << " name: " << classname << " id: " << id << std::endl; 145 if(id==NULL){ 146 COUT(0) << "Recieved a bad classname" << endl; 147 abort(); 148 } 149 id->setNetworkID( networkID ); 150 temp += 2*sizeof(uint32_t) + stringsize; 151 } 75 152 delete this; 76 153 return true; … … 78 155 79 156 80 unsigned int ClassID::getClassID(){81 return *(uint32_t *)(data_ + _CLASSID);82 }83 84 157 } //namespace packet 85 158 }//namespace orxonox -
code/trunk/src/network/packet/ClassID.h
r2669 r2759 38 38 namespace packet { 39 39 40 #define PACKET_FLAGS_CLASSID ENET_PACKET_FLAG_RELIABLE41 #define _PACKETID 042 #define _CLASSID _PACKETID + sizeof(ENUM::Type)43 #define _CLASSNAMELENGTH _CLASSID + sizeof(uint32_t)44 #define _CLASSNAME _CLASSNAMELENGTH + sizeof(classNameLength_)45 40 46 41 /** … … 50 45 { 51 46 public: 52 ClassID( unsigned int classID, std::string className);47 ClassID( ); 53 48 ClassID( uint8_t* data, unsigned int clientID ); 54 49 ~ClassID(); 55 50 56 inline unsigned int getSize() const;51 uint32_t getSize() const; 57 52 bool process(); 58 53 59 unsigned int getClassID();60 unsigned int getClassNameLength(){ return classNameLength_; }61 const char *getClassName(){ return (const char*)(data_+_CLASSNAME); }62 54 private: 63 uint32_t classNameLength_;64 55 }; 65 56 -
code/trunk/src/network/packet/Gamestate.cc
r2710 r2759 48 48 #define PACKET_FLAG_GAMESTATE ENET_PACKET_FLAG_RELIABLE 49 49 50 // Gamestate::Gamestate()51 // {52 // flags_ = flags_ | PACKET_FLAG_GAMESTATE;53 // }54 50 55 51 Gamestate::Gamestate() … … 127 123 #endif 128 124 129 //if(it->doSelection(id))130 125 if ( it->doSync( id, mode ) ) 131 126 dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) ); 132 // dataMap_[mem-data_]=(*it); // save the mem location of the synchronisable data133 127 if(!it->getData(mem, id, mode)) 134 128 return false; // mem pointer gets automatically increased because of call by reference … … 158 152 assert(!header_->isDiffed()); 159 153 uint8_t *mem=data_+GamestateHeader::getSize(); 160 // get the start of the Synchronisable list161 //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin();162 154 Synchronisable *s; 163 155 … … 177 169 mem += objectheader.getDataSize(); 178 170 } 179 // COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl;180 // else181 // COUT(0) << "fabricated: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl;182 171 } 183 172 else 184 173 { 185 174 bool b = s->updateData(mem, mode); 186 // if(!b)187 // COUT(0) << "data could not be updated" << endl;188 175 assert(b); 189 176 } … … 262 249 uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()]; 263 250 uint8_t *dest = ndata + GamestateHeader::getSize(); 264 //unsigned char *dest = new unsigned char[buffer];265 251 uint8_t *source = data_ + GamestateHeader::getSize(); 266 252 int retval; … … 294 280 uint32_t compsize = header_->getCompSize(); 295 281 uint32_t bufsize; 296 // assert(compsize<=datasize);297 282 bufsize = datasize; 298 283 assert(bufsize!=0); … … 340 325 assert(!header_->isDiffed()); 341 326 GamestateHeader diffHeader(base->data_); 342 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;343 327 uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); 344 328 uint32_t of=0; // pointers offset … … 397 381 //copy in the zeros 398 382 for(it=dataMap_.begin(); it!=dataMap_.end();){ 399 // if((*it).objSize==0)400 // continue;401 // if(it->second->getSize(HEADER->id)==0) // merged from objecthierarchy2, doesn't work anymore; TODO: change this402 // continue; // merged from objecthierarchy2, doesn't work anymore; TODO: change this403 383 SynchronisableHeader oldobjectheader(origdata); 404 384 SynchronisableHeader newobjectheader(newdata); … … 408 388 continue; 409 389 } 410 // object = Synchronisable::getSynchronisable( (*it).objID );411 // assert(object->objectID == oldobjectheader->objectID);412 390 objectsize = oldobjectheader.getDataSize(); 413 391 objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader … … 447 425 assert(header_->isDiffed()); 448 426 assert(!header_->isCompressed() && !base->header_->isCompressed()); 449 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;450 427 uint8_t *basep = GAMESTATE_START(base->data_); 451 428 uint8_t *gs = GAMESTATE_START(this->data_); -
code/trunk/src/network/packet/Gamestate.h
r2662 r2759 93 93 private: 94 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 95 99 96 };
Note: See TracChangeset
for help on using the changeset viewer.