Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/packet/ClassID.cc @ 3209

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

Cleanup in network plus a few dependency reductions (no enet-function inlines, using enum PacketFlag instead of the enet version)

  • 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, (C) 2008
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "ClassID.h"
30
31#include <cassert>
32#include <map>
33#include <queue>
34#include <string>
35
36#include "core/CoreIncludes.h"
37
38namespace orxonox {
39namespace packet {
40
41
42#define PACKET_FLAGS_CLASSID  PacketFlag::Reliable
43#define _PACKETID             0
44
45
46ClassID::ClassID( ) : Packet(){
47  Identifier *id;
48  std::string classname;
49  unsigned int nrOfClasses=0; 
50  unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nrofclasses
51  uint32_t network_id;
52  flags_ = flags_ | PACKET_FLAGS_CLASSID;
53  std::queue<std::pair<uint32_t, std::string> > tempQueue;
54 
55  //calculate total needed size (for all strings and integers)
56  std::map<std::string, Identifier*>::const_iterator it = Factory::getFactoryMapBegin();
57  for(;it != Factory::getFactoryMapEnd();++it){
58    id = (*it).second;
59    if(id == NULL)
60      continue;
61    classname = id->getName();
62    network_id = id->getNetworkID();
63    if(network_id==0)
64      COUT(3) << "we got a null class id: " << id->getName() << std::endl;
65    // now push the network id and the classname to the stack
66    tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) );
67    ++nrOfClasses;
68    packetSize += (classname.size()+1)+sizeof(uint32_t)+sizeof(uint32_t);
69  }
70 
71  this->data_=new uint8_t[ packetSize ];
72  //set the appropriate packet id
73  assert(this->data_);
74  *(ENUM::Type *)(this->data_ + _PACKETID ) = ENUM::ClassID;
75 
76  uint8_t *temp=data_+sizeof(uint32_t);
77  // save the number of all classes
78  *(uint32_t*)temp = nrOfClasses;
79  temp += sizeof(uint32_t);
80 
81  // now save all classids and classnames
82  std::pair<uint32_t, std::string> tempPair;
83  while( !tempQueue.empty() ){
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  }
91 
92  COUT(5) << "classid packetSize is " << packetSize << endl;
93 
94}
95
96ClassID::ClassID( uint8_t* data, unsigned int clientID )
97  : Packet(data, clientID)
98{
99}
100
101ClassID::~ClassID()
102{
103}
104
105uint32_t ClassID::getSize() const{
106  uint8_t *temp = data_+sizeof(uint32_t); // packet identification
107  uint32_t totalsize = sizeof(uint32_t); // packet identification
108  uint32_t nrOfClasses = *(uint32_t*)temp;
109  temp += sizeof(uint32_t);
110  totalsize += sizeof(uint32_t); // storage size for nr of all classes
111 
112  for(unsigned int i=0; i<nrOfClasses; i++){
113    totalsize += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
114  }
115  return totalsize;
116}
117
118
119bool ClassID::process(){
120  int nrOfClasses;
121  uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid
122  uint32_t networkID;
123  uint32_t stringsize;
124  unsigned char *classname;
125 
126 
127  //clean the map of network ids
128  Factory::cleanNetworkIDs();
129 
130  COUT(4) << "=== processing classids: " << endl;
131  std::pair<uint32_t, std::string> tempPair;
132  Identifier *id;
133  // read the total number of classes
134  nrOfClasses = *(uint32_t*)temp;
135  temp += sizeof(uint32_t);
136 
137  for( int i=0; i<nrOfClasses; i++){
138    networkID = *(uint32_t*)temp;
139    stringsize = *(uint32_t*)(temp+sizeof(uint32_t));
140    classname = temp+2*sizeof(uint32_t);
141    id=ClassByString( std::string((const char*)classname) );
142    COUT(0) << "processing classid: " << networkID << " name: " << classname << " id: " << id << std::endl;
143    if(id==NULL){
144      COUT(0) << "Recieved a bad classname" << endl;
145      abort();
146    }
147    id->setNetworkID( networkID );
148    temp += 2*sizeof(uint32_t) + stringsize;
149  }
150  delete this;
151  return true;
152}
153
154
155} //namespace packet
156}//namespace orxonox
Note: See TracBrowser for help on using the repository browser.