- Timestamp:
- Mar 29, 2018, 4:04:35 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/Masterserver_FS18/src/libraries/network/packet/ClassID.cc
r11071 r11842 46 46 47 47 48 ClassID::ClassID( ) : Packet(){48 ClassID::ClassID() : Packet() { 49 49 Identifier *id; 50 unsigned int nrOfClasses =0;51 unsigned int packetSize =2*sizeof(uint32_t); //space for the packetID and for the nrofclasses50 unsigned int nrOfClasses = 0; 51 unsigned int packetSize = 2 * sizeof(uint32_t); //space for the packetID and for the nrofclasses 52 52 uint32_t network_id; 53 flags_ = flags_ |PACKET_FLAGS_CLASSID;53 this->flags_ |= PACKET_FLAGS_CLASSID; 54 54 std::queue<std::pair<uint32_t, std::string>> tempQueue; 55 55 56 // calculate total needed size (for all strings and integers)57 for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()) {56 // calculate total needed size (for all strings and integers) 57 for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()) { 58 58 id = mapEntry.second; 59 59 if(id == nullptr || !id->hasFactory()) … … 64 64 tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) ); 65 65 ++nrOfClasses; 66 packetSize += (classname.size() +1)+sizeof(network_id)+sizeof(uint32_t);66 packetSize += (classname.size() + 1) + sizeof(network_id) + sizeof(uint32_t); 67 67 } 68 68 69 this->data_ =new uint8_t[ packetSize ];69 this->data_ = new uint8_t[ packetSize ]; 70 70 //set the appropriate packet id 71 71 assert(this->data_); 72 72 *(Type *)(this->data_ + _PACKETID ) = Type::ClassID; 73 73 74 uint8_t *temp =data_+sizeof(uint32_t);74 uint8_t *temp = this->data_ + sizeof(uint32_t); 75 75 // save the number of all classes 76 *(uint32_t*) temp = nrOfClasses;76 *(uint32_t*) temp = nrOfClasses; 77 77 temp += sizeof(uint32_t); 78 78 79 79 // now save all classids and classnames 80 80 std::pair<uint32_t, std::string> tempPair; 81 uint32_t tempsize = 2 *sizeof(uint32_t); // packetid and nrOfClasses81 uint32_t tempsize = 2 * sizeof(uint32_t); // packetid and nrOfClasses 82 82 while( !tempQueue.empty() ){ 83 83 tempPair = tempQueue.front(); 84 84 tempQueue.pop(); 85 *(uint32_t*) temp = tempPair.first;86 *(uint32_t*) (temp+sizeof(uint32_t)) = tempPair.second.size()+1;87 memcpy(temp +2*sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size()+1);88 temp +=2*sizeof(uint32_t)+tempPair.second.size()+1;89 tempsize +=2*sizeof(uint32_t)+tempPair.second.size()+1;85 *(uint32_t*) temp = tempPair.first; 86 *(uint32_t*) (temp+sizeof(uint32_t)) = tempPair.second.size() + 1; 87 memcpy(temp + 2 * sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size() + 1); 88 temp += 2 * sizeof(uint32_t) + tempPair.second.size() + 1; 89 tempsize += 2 * sizeof(uint32_t) + tempPair.second.size() + 1; 90 90 } 91 assert(tempsize ==packetSize);91 assert(tempsize == packetSize); 92 92 93 93 orxout(verbose_more, context::packets) << "classid packetSize is " << packetSize << endl; … … 104 104 } 105 105 106 uint32_t ClassID::getSize() const {107 uint8_t *temp = data_+sizeof(uint32_t); // packet identification106 uint32_t ClassID::getSize() const { 107 uint8_t *temp = this->data_ + sizeof(uint32_t); // packet identification 108 108 uint32_t totalsize = sizeof(uint32_t); // packet identification 109 uint32_t nrOfClasses = *(uint32_t*) temp;109 uint32_t nrOfClasses = *(uint32_t*) temp; 110 110 temp += sizeof(uint32_t); 111 111 totalsize += sizeof(uint32_t); // storage size for nr of all classes 112 112 113 for(unsigned int i=0; i <nrOfClasses; i++){114 totalsize += 2 *sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));115 temp += 2 *sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));113 for(unsigned int i=0; i < nrOfClasses; i++) { 114 totalsize += 2 * sizeof(uint32_t) + *(uint32_t*) (temp + sizeof(uint32_t)); 115 temp += 2 * sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); 116 116 } 117 117 return totalsize; 118 118 } 119 119 120 121 bool ClassID::process(orxonox::Host* host){ 120 // TODO: This parses the packet and calls ClassByString() 121 // However, the resulting Identifier is discarded... 122 bool ClassID::process(orxonox::Host* host) { 122 123 int nrOfClasses; 123 uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid124 uint8_t *temp = this->data_ + sizeof(uint32_t); //skip the packetid 124 125 uint32_t networkID; 125 126 uint32_t stringsize; 126 127 unsigned char *classname; 127 128 128 129 129 //clear the map of network ids … … 134 134 Identifier *id; 135 135 // read the total number of classes 136 nrOfClasses = *(uint32_t*) temp;136 nrOfClasses = *(uint32_t*) temp; 137 137 temp += sizeof(uint32_t); 138 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) );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 144 orxout(internal_info, context::packets) << "processing classid: " << networkID << " name: " << classname << " id: " << id << endl; 145 if(id ==nullptr){145 if(id == nullptr) { 146 146 orxout(user_error, context::packets) << "Received a bad classname" << endl; 147 147 abort(); 148 148 } 149 149 id->setNetworkID( networkID ); 150 temp += 2 *sizeof(uint32_t) + stringsize;150 temp += 2 * sizeof(uint32_t) + stringsize; 151 151 } 152 152 delete this;
Note: See TracChangeset
for help on using the changeset viewer.