Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/network/packet/FunctionIDs.cc @ 10468

Last change on this file since 10468 was 10468, checked in by landauf, 9 years ago

moved static content of NetworkFunctionBase to NetworkFunctionManager

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