Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/rocket2/src/libraries/network/packet/ClassID.cc @ 6952

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

Merged presentation2 branch back to trunk.
Major new features:

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