Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp2/src/network/packet/ClassID.cc @ 2944

Last change on this file since 2944 was 2937, checked in by scheusso, 16 years ago

commit for testing reasons

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