[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 | |
---|
[8858] | 36 | #include "util/Output.h" |
---|
[9667] | 37 | #include "core/object/ObjectList.h" |
---|
[3214] | 38 | #include "network/NetworkFunction.h" |
---|
[10624] | 39 | #include "network/NetworkFunctionManager.h" |
---|
[3214] | 40 | |
---|
[2937] | 41 | namespace orxonox { |
---|
| 42 | namespace packet { |
---|
| 43 | |
---|
| 44 | |
---|
[3214] | 45 | #define PACKET_FLAGS_FUNCTIONIDS PacketFlag::Reliable |
---|
[2937] | 46 | #define _PACKETID 0 |
---|
| 47 | |
---|
| 48 | |
---|
[7801] | 49 | FunctionIDs::FunctionIDs( ) : Packet() |
---|
| 50 | { |
---|
[6417] | 51 | unsigned int nrOfFunctions=0; |
---|
[2937] | 52 | unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nroffunctions |
---|
| 53 | uint32_t networkID; |
---|
| 54 | flags_ = flags_ | PACKET_FLAGS_FUNCTIONIDS; |
---|
[11071] | 55 | std::queue<std::pair<uint32_t, std::string>> tempQueue; |
---|
[6417] | 56 | |
---|
[2937] | 57 | //calculate total needed size (for all strings and integers) |
---|
[10624] | 58 | const std::set<NetworkFunctionBase*>& set = NetworkFunctionManager::getInstance().getAllFunctions(); |
---|
| 59 | std::set<NetworkFunctionBase*>::const_iterator it; |
---|
| 60 | for (it = set.begin(); it != set.end(); ++it) |
---|
[7801] | 61 | { |
---|
[10624] | 62 | const std::string& functionname = (*it)->getName(); |
---|
| 63 | networkID = (*it)->getNetworkID(); |
---|
[2937] | 64 | // now push the network id and the classname to the stack |
---|
| 65 | tempQueue.push( std::pair<unsigned int, std::string>(networkID, functionname) ); |
---|
| 66 | ++nrOfFunctions; |
---|
| 67 | packetSize += (functionname.size()+1)+sizeof(uint32_t)+sizeof(uint32_t); // reserver size for the functionname string, the functionname length and the networkID |
---|
| 68 | } |
---|
[6417] | 69 | |
---|
[2937] | 70 | this->data_=new uint8_t[ packetSize ]; |
---|
| 71 | //set the appropriate packet id |
---|
| 72 | assert(this->data_); |
---|
[11071] | 73 | *(Type *)(this->data_ + _PACKETID ) = Type::FunctionIDs; |
---|
[6417] | 74 | |
---|
[2937] | 75 | uint8_t *temp=data_+sizeof(uint32_t); |
---|
| 76 | // save the number of all classes |
---|
| 77 | *(uint32_t*)temp = nrOfFunctions; |
---|
| 78 | temp += sizeof(uint32_t); |
---|
[6417] | 79 | |
---|
[2937] | 80 | // now save all classids and classnames |
---|
| 81 | std::pair<uint32_t, std::string> tempPair; |
---|
[7801] | 82 | while( !tempQueue.empty() ) |
---|
| 83 | { |
---|
[2937] | 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; |
---|
| 90 | } |
---|
[6417] | 91 | |
---|
[8858] | 92 | orxout(verbose_more, context::packets) << "FunctionIDs packetSize is " << packetSize << endl; |
---|
[6417] | 93 | |
---|
[2937] | 94 | } |
---|
| 95 | |
---|
| 96 | FunctionIDs::FunctionIDs( uint8_t* data, unsigned int clientID ) |
---|
| 97 | : Packet(data, clientID) |
---|
| 98 | { |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | FunctionIDs::~FunctionIDs() |
---|
| 102 | { |
---|
| 103 | } |
---|
| 104 | |
---|
[7801] | 105 | uint32_t FunctionIDs::getSize() const |
---|
| 106 | { |
---|
[2937] | 107 | assert(this->data_); |
---|
| 108 | uint8_t *temp = data_+sizeof(uint32_t); // packet identification |
---|
| 109 | uint32_t totalsize = sizeof(uint32_t); // data size |
---|
| 110 | uint32_t nrOfFunctions = *(uint32_t*)temp; |
---|
| 111 | temp += sizeof(uint32_t); |
---|
| 112 | totalsize += sizeof(uint32_t); // storage size for nr of all classes |
---|
[6417] | 113 | |
---|
[2937] | 114 | for(unsigned int i=0; i<nrOfFunctions; i++){ |
---|
| 115 | 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 |
---|
| 116 | temp += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); |
---|
| 117 | } |
---|
| 118 | return totalsize; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
[7801] | 122 | bool FunctionIDs::process(orxonox::Host* host) |
---|
| 123 | { |
---|
[2937] | 124 | int nrOfFunctions; |
---|
| 125 | uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid |
---|
| 126 | uint32_t networkID; |
---|
| 127 | uint32_t stringsize; |
---|
| 128 | unsigned char *functionname; |
---|
[6417] | 129 | |
---|
[10624] | 130 | //clear the map of network ids |
---|
| 131 | NetworkFunctionManager::getInstance().clearNetworkIDs(); |
---|
| 132 | |
---|
[8858] | 133 | orxout(verbose, context::packets) << "=== processing functionids: " << endl; |
---|
[2937] | 134 | std::pair<uint32_t, std::string> tempPair; |
---|
| 135 | // read the total number of classes |
---|
| 136 | nrOfFunctions = *(uint32_t*)temp; |
---|
| 137 | temp += sizeof(uint32_t); |
---|
[6417] | 138 | |
---|
[7801] | 139 | for( int i=0; i<nrOfFunctions; i++) |
---|
| 140 | { |
---|
[2937] | 141 | networkID = *(uint32_t*)temp; |
---|
| 142 | stringsize = *(uint32_t*)(temp+sizeof(uint32_t)); |
---|
| 143 | functionname = temp+2*sizeof(uint32_t); |
---|
[8858] | 144 | orxout(internal_info, context::packets) << "processing functionid: " << networkID << " name: " << functionname << endl; |
---|
[10624] | 145 | NetworkFunctionManager::getInstance().getFunctionByName((const char*)functionname)->setNetworkID(networkID); |
---|
[2937] | 146 | temp += 2*sizeof(uint32_t) + stringsize; |
---|
| 147 | } |
---|
| 148 | delete this; |
---|
| 149 | return true; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | } //namespace packet |
---|
| 154 | }//namespace orxonox |
---|