Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/packet/FunctionIDs.cc @ 3914

Last change on this file since 3914 was 3304, checked in by rgrieder, 15 years ago

Merged netp6 branch back to the trunk.

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