[2937] | 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 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[3214] | 29 | #include "FunctionIDs.h" |
---|
[2937] | 30 | |
---|
| 31 | #include <cassert> |
---|
[3214] | 32 | #include <cstring> |
---|
[2937] | 33 | #include <queue> |
---|
[3214] | 34 | #include <string> |
---|
[2937] | 35 | |
---|
[3214] | 36 | #include "util/Debug.h" |
---|
| 37 | #include "core/ObjectList.h" |
---|
| 38 | #include "network/NetworkFunction.h" |
---|
| 39 | |
---|
[2937] | 40 | namespace orxonox { |
---|
| 41 | namespace packet { |
---|
| 42 | |
---|
| 43 | |
---|
[3214] | 44 | #define PACKET_FLAGS_FUNCTIONIDS PacketFlag::Reliable |
---|
[2937] | 45 | #define _PACKETID 0 |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | FunctionIDs::FunctionIDs( ) : Packet(){ |
---|
[6417] | 49 | unsigned int nrOfFunctions=0; |
---|
[2937] | 50 | unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nroffunctions |
---|
| 51 | uint32_t networkID; |
---|
| 52 | flags_ = flags_ | PACKET_FLAGS_FUNCTIONIDS; |
---|
| 53 | std::queue<std::pair<uint32_t, std::string> > tempQueue; |
---|
[6417] | 54 | |
---|
[2937] | 55 | //calculate total needed size (for all strings and integers) |
---|
| 56 | ObjectList<NetworkFunctionBase>::iterator it; |
---|
| 57 | for(it = ObjectList<NetworkFunctionBase>::begin(); it; ++it){ |
---|
[6417] | 58 | const std::string& functionname = it->getName(); |
---|
[2937] | 59 | networkID = it->getNetworkID(); |
---|
| 60 | // now push the network id and the classname to the stack |
---|
| 61 | tempQueue.push( std::pair<unsigned int, std::string>(networkID, functionname) ); |
---|
| 62 | ++nrOfFunctions; |
---|
| 63 | packetSize += (functionname.size()+1)+sizeof(uint32_t)+sizeof(uint32_t); // reserver size for the functionname string, the functionname length and the networkID |
---|
| 64 | } |
---|
[6417] | 65 | |
---|
[2937] | 66 | this->data_=new uint8_t[ packetSize ]; |
---|
| 67 | //set the appropriate packet id |
---|
| 68 | assert(this->data_); |
---|
[3280] | 69 | *(Type::Value *)(this->data_ + _PACKETID ) = Type::FunctionIDs; |
---|
[6417] | 70 | |
---|
[2937] | 71 | uint8_t *temp=data_+sizeof(uint32_t); |
---|
| 72 | // save the number of all classes |
---|
| 73 | *(uint32_t*)temp = nrOfFunctions; |
---|
| 74 | temp += sizeof(uint32_t); |
---|
[6417] | 75 | |
---|
[2937] | 76 | // now save all classids and classnames |
---|
| 77 | std::pair<uint32_t, std::string> tempPair; |
---|
| 78 | while( !tempQueue.empty() ){ |
---|
| 79 | tempPair = tempQueue.front(); |
---|
| 80 | tempQueue.pop(); |
---|
| 81 | *(uint32_t*)temp = tempPair.first; |
---|
| 82 | *(uint32_t*)(temp+sizeof(uint32_t)) = tempPair.second.size()+1; |
---|
| 83 | memcpy(temp+2*sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size()+1); |
---|
| 84 | temp+=2*sizeof(uint32_t)+tempPair.second.size()+1; |
---|
| 85 | } |
---|
[6417] | 86 | |
---|
[2937] | 87 | COUT(5) << "FunctionIDs packetSize is " << packetSize << endl; |
---|
[6417] | 88 | |
---|
[2937] | 89 | } |
---|
| 90 | |
---|
| 91 | FunctionIDs::FunctionIDs( uint8_t* data, unsigned int clientID ) |
---|
| 92 | : Packet(data, clientID) |
---|
| 93 | { |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | FunctionIDs::~FunctionIDs() |
---|
| 97 | { |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | uint32_t FunctionIDs::getSize() const{ |
---|
| 101 | assert(this->data_); |
---|
| 102 | uint8_t *temp = data_+sizeof(uint32_t); // packet identification |
---|
| 103 | uint32_t totalsize = sizeof(uint32_t); // data size |
---|
| 104 | uint32_t nrOfFunctions = *(uint32_t*)temp; |
---|
| 105 | temp += sizeof(uint32_t); |
---|
| 106 | totalsize += sizeof(uint32_t); // storage size for nr of all classes |
---|
[6417] | 107 | |
---|
[2937] | 108 | for(unsigned int i=0; i<nrOfFunctions; i++){ |
---|
| 109 | totalsize += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); // for each network function add size for id, sizeof(string) and length of string itself to totalsize |
---|
| 110 | temp += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); |
---|
| 111 | } |
---|
| 112 | return totalsize; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | bool FunctionIDs::process(){ |
---|
| 117 | int nrOfFunctions; |
---|
| 118 | uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid |
---|
| 119 | uint32_t networkID; |
---|
| 120 | uint32_t stringsize; |
---|
| 121 | unsigned char *functionname; |
---|
[6417] | 122 | |
---|
[2937] | 123 | COUT(4) << "=== processing functionids: " << endl; |
---|
| 124 | std::pair<uint32_t, std::string> tempPair; |
---|
| 125 | // read the total number of classes |
---|
| 126 | nrOfFunctions = *(uint32_t*)temp; |
---|
| 127 | temp += sizeof(uint32_t); |
---|
[6417] | 128 | |
---|
[2937] | 129 | for( int i=0; i<nrOfFunctions; i++){ |
---|
| 130 | networkID = *(uint32_t*)temp; |
---|
| 131 | stringsize = *(uint32_t*)(temp+sizeof(uint32_t)); |
---|
| 132 | functionname = temp+2*sizeof(uint32_t); |
---|
[3304] | 133 | COUT(3) << "processing functionid: " << networkID << " name: " << functionname << std::endl; |
---|
[2937] | 134 | NetworkFunctionBase::setNetworkID((const char*)functionname, networkID); |
---|
| 135 | temp += 2*sizeof(uint32_t) + stringsize; |
---|
| 136 | } |
---|
| 137 | delete this; |
---|
| 138 | return true; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | } //namespace packet |
---|
| 143 | }//namespace orxonox |
---|