[1711] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Oliver Scheuss, (C) 2008 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[3214] | 29 | #include "ClassID.h" |
---|
[1711] | 30 | |
---|
[3214] | 31 | #include <cassert> |
---|
| 32 | #include <cstdlib> |
---|
[1837] | 33 | #include <cstring> |
---|
[2759] | 34 | #include <map> |
---|
| 35 | #include <queue> |
---|
[3214] | 36 | #include <string> |
---|
[1701] | 37 | |
---|
[3214] | 38 | #include "core/CoreIncludes.h" |
---|
| 39 | |
---|
[2171] | 40 | namespace orxonox { |
---|
[1701] | 41 | namespace packet { |
---|
| 42 | |
---|
[1856] | 43 | |
---|
[3214] | 44 | #define PACKET_FLAGS_CLASSID PacketFlag::Reliable |
---|
[2759] | 45 | #define _PACKETID 0 |
---|
[2669] | 46 | |
---|
[2759] | 47 | |
---|
| 48 | ClassID::ClassID( ) : Packet(){ |
---|
| 49 | Identifier *id; |
---|
[5929] | 50 | unsigned int nrOfClasses=0; |
---|
[2759] | 51 | unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nrofclasses |
---|
| 52 | uint32_t network_id; |
---|
[1701] | 53 | flags_ = flags_ | PACKET_FLAGS_CLASSID; |
---|
[2759] | 54 | std::queue<std::pair<uint32_t, std::string> > tempQueue; |
---|
[5929] | 55 | |
---|
[2759] | 56 | //calculate total needed size (for all strings and integers) |
---|
[5929] | 57 | std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapBegin(); |
---|
| 58 | for(;it != Identifier::getStringIdentifierMapEnd();++it){ |
---|
[6417] | 59 | id = it->second; |
---|
[5929] | 60 | if(id == NULL || !id->hasFactory()) |
---|
[2759] | 61 | continue; |
---|
[6417] | 62 | const std::string& classname = id->getName(); |
---|
[2759] | 63 | network_id = id->getNetworkID(); |
---|
| 64 | // now push the network id and the classname to the stack |
---|
| 65 | tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) ); |
---|
| 66 | ++nrOfClasses; |
---|
[6960] | 67 | packetSize += (classname.size()+1)+sizeof(network_id)+sizeof(uint32_t); |
---|
[2759] | 68 | } |
---|
[5929] | 69 | |
---|
[2759] | 70 | this->data_=new uint8_t[ packetSize ]; |
---|
| 71 | //set the appropriate packet id |
---|
| 72 | assert(this->data_); |
---|
[3280] | 73 | *(Type::Value *)(this->data_ + _PACKETID ) = Type::ClassID; |
---|
[5929] | 74 | |
---|
[2759] | 75 | uint8_t *temp=data_+sizeof(uint32_t); |
---|
| 76 | // save the number of all classes |
---|
| 77 | *(uint32_t*)temp = nrOfClasses; |
---|
| 78 | temp += sizeof(uint32_t); |
---|
[5929] | 79 | |
---|
[2759] | 80 | // now save all classids and classnames |
---|
| 81 | std::pair<uint32_t, std::string> tempPair; |
---|
[6960] | 82 | uint32_t tempsize = 2*sizeof(uint32_t); // packetid and nrOfClasses |
---|
[2759] | 83 | while( !tempQueue.empty() ){ |
---|
| 84 | tempPair = tempQueue.front(); |
---|
| 85 | tempQueue.pop(); |
---|
| 86 | *(uint32_t*)temp = tempPair.first; |
---|
| 87 | *(uint32_t*)(temp+sizeof(uint32_t)) = tempPair.second.size()+1; |
---|
| 88 | memcpy(temp+2*sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size()+1); |
---|
| 89 | temp+=2*sizeof(uint32_t)+tempPair.second.size()+1; |
---|
[6960] | 90 | tempsize+=2*sizeof(uint32_t)+tempPair.second.size()+1; |
---|
[2759] | 91 | } |
---|
[6964] | 92 | assert(tempsize==packetSize); |
---|
[5929] | 93 | |
---|
[3084] | 94 | COUT(5) << "classid packetSize is " << packetSize << endl; |
---|
[5929] | 95 | |
---|
[1701] | 96 | } |
---|
| 97 | |
---|
[1907] | 98 | ClassID::ClassID( uint8_t* data, unsigned int clientID ) |
---|
[1711] | 99 | : Packet(data, clientID) |
---|
[1701] | 100 | { |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | ClassID::~ClassID() |
---|
| 104 | { |
---|
| 105 | } |
---|
| 106 | |
---|
[2759] | 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 |
---|
[5929] | 113 | |
---|
[2759] | 114 | for(unsigned int i=0; i<nrOfClasses; i++){ |
---|
| 115 | totalsize += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); |
---|
[6960] | 116 | temp += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); |
---|
[2759] | 117 | } |
---|
| 118 | return totalsize; |
---|
[1701] | 119 | } |
---|
| 120 | |
---|
[2759] | 121 | |
---|
[1701] | 122 | bool ClassID::process(){ |
---|
[2759] | 123 | int nrOfClasses; |
---|
| 124 | uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid |
---|
| 125 | uint32_t networkID; |
---|
| 126 | uint32_t stringsize; |
---|
| 127 | unsigned char *classname; |
---|
[5929] | 128 | |
---|
| 129 | |
---|
| 130 | //clear the map of network ids |
---|
| 131 | Identifier::clearNetworkIDs(); |
---|
| 132 | |
---|
[2759] | 133 | COUT(4) << "=== processing classids: " << endl; |
---|
| 134 | std::pair<uint32_t, std::string> tempPair; |
---|
| 135 | Identifier *id; |
---|
| 136 | // read the total number of classes |
---|
| 137 | nrOfClasses = *(uint32_t*)temp; |
---|
| 138 | temp += sizeof(uint32_t); |
---|
[5929] | 139 | |
---|
[2759] | 140 | for( int i=0; i<nrOfClasses; i++){ |
---|
| 141 | networkID = *(uint32_t*)temp; |
---|
| 142 | stringsize = *(uint32_t*)(temp+sizeof(uint32_t)); |
---|
| 143 | classname = temp+2*sizeof(uint32_t); |
---|
| 144 | id=ClassByString( std::string((const char*)classname) ); |
---|
[3304] | 145 | COUT(3) << "processing classid: " << networkID << " name: " << classname << " id: " << id << std::endl; |
---|
[2759] | 146 | if(id==NULL){ |
---|
| 147 | COUT(0) << "Recieved a bad classname" << endl; |
---|
| 148 | abort(); |
---|
| 149 | } |
---|
| 150 | id->setNetworkID( networkID ); |
---|
| 151 | temp += 2*sizeof(uint32_t) + stringsize; |
---|
| 152 | } |
---|
[1711] | 153 | delete this; |
---|
[1701] | 154 | return true; |
---|
| 155 | } |
---|
| 156 | |
---|
[2669] | 157 | |
---|
[1701] | 158 | } //namespace packet |
---|
[2171] | 159 | }//namespace orxonox |
---|