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