Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/packet/ClassID.cc @ 12067

Last change on this file since 12067 was 12027, checked in by merholzl, 6 years ago

Merged Masterserver, refresh button had to be removed

  • Property svn:eol-style set to native
File size: 4.9 KB
RevLine 
[1711]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
[3214]29#include "ClassID.h"
[1711]30
[3214]31#include <cassert>
32#include <cstdlib>
[1837]33#include <cstring>
[2759]34#include <map>
35#include <queue>
[3214]36#include <string>
[1701]37
[3214]38#include "core/CoreIncludes.h"
39
[2171]40namespace orxonox {
[1701]41namespace packet {
42
[1856]43
[3214]44#define PACKET_FLAGS_CLASSID  PacketFlag::Reliable
[2759]45#define _PACKETID             0
[2669]46
[2759]47
[12027]48ClassID::ClassID() : Packet() {
[2759]49  Identifier *id;
[12027]50  unsigned int nrOfClasses = 0;
51  unsigned int packetSize = 2 * sizeof(uint32_t); //space for the packetID and for the nrofclasses
[2759]52  uint32_t network_id;
[12027]53  this->flags_ |= PACKET_FLAGS_CLASSID;
[11071]54  std::queue<std::pair<uint32_t, std::string>> tempQueue;
[5929]55
[12027]56  // calculate total needed size (for all strings and integers)
57  for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()) {
[11071]58    id = mapEntry.second;
59    if(id == nullptr || !id->hasFactory())
[2759]60      continue;
[6417]61    const std::string& classname = id->getName();
[2759]62    network_id = id->getNetworkID();
63    // now push the network id and the classname to the stack
64    tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) );
65    ++nrOfClasses;
[12027]66    packetSize += (classname.size() + 1) + sizeof(network_id) + sizeof(uint32_t);
[2759]67  }
[5929]68
[12027]69  this->data_ = new uint8_t[ packetSize ];
[2759]70  //set the appropriate packet id
71  assert(this->data_);
[11071]72  *(Type *)(this->data_ + _PACKETID ) = Type::ClassID;
[5929]73
[12027]74  uint8_t *temp = this->data_ + sizeof(uint32_t);
[2759]75  // save the number of all classes
[12027]76  *(uint32_t*) temp = nrOfClasses;
[2759]77  temp += sizeof(uint32_t);
[5929]78
[2759]79  // now save all classids and classnames
80  std::pair<uint32_t, std::string> tempPair;
[12027]81  uint32_t tempsize = 2 * sizeof(uint32_t); // packetid and nrOfClasses
[2759]82  while( !tempQueue.empty() ){
83    tempPair = tempQueue.front();
84    tempQueue.pop();
[12027]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    tempsize += 2 * sizeof(uint32_t) + tempPair.second.size() + 1;
[2759]90  }
[12027]91  assert(tempsize == packetSize);
[5929]92
[8858]93  orxout(verbose_more, context::packets) << "classid packetSize is " << packetSize << endl;
[5929]94
[1701]95}
96
[1907]97ClassID::ClassID( uint8_t* data, unsigned int clientID )
[1711]98  : Packet(data, clientID)
[1701]99{
100}
101
102ClassID::~ClassID()
103{
104}
105
[12027]106uint32_t ClassID::getSize() const {
107  uint8_t *temp = this->data_ + sizeof(uint32_t); // packet identification
[2759]108  uint32_t totalsize = sizeof(uint32_t); // packet identification
[12027]109  uint32_t nrOfClasses = *(uint32_t*) temp;
[2759]110  temp += sizeof(uint32_t);
111  totalsize += sizeof(uint32_t); // storage size for nr of all classes
[5929]112
[12027]113  for(unsigned int i=0; i < nrOfClasses; i++) {
114    totalsize += 2 * sizeof(uint32_t) + *(uint32_t*) (temp + sizeof(uint32_t));
115    temp += 2 * sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
[2759]116  }
117  return totalsize;
[1701]118}
119
[12027]120bool ClassID::process(orxonox::Host* host) {
[2759]121  int nrOfClasses;
[12027]122  uint8_t *temp = this->data_ + sizeof(uint32_t); //skip the packetid
[2759]123  uint32_t networkID;
124  uint32_t stringsize;
125  unsigned char *classname;
[5929]126
127  //clear the map of network ids
[9667]128  IdentifierManager::getInstance().clearNetworkIDs();
[5929]129
[8858]130  orxout(verbose, context::packets) << "=== processing classids: " << endl;
[2759]131  std::pair<uint32_t, std::string> tempPair;
132  Identifier *id;
133  // read the total number of classes
[12027]134  nrOfClasses = *(uint32_t*) temp;
[2759]135  temp += sizeof(uint32_t);
[5929]136
[12027]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) );
[8858]142    orxout(internal_info, context::packets) << "processing classid: " << networkID << " name: " << classname << " id: " << id << endl;
[12027]143    if(id == nullptr) {
[8858]144      orxout(user_error, context::packets) << "Received a bad classname" << endl;
[2759]145      abort();
146    }
147    id->setNetworkID( networkID );
[12027]148    temp += 2 * sizeof(uint32_t) + stringsize;
[2759]149  }
[1711]150  delete this;
[1701]151  return true;
152}
153
[2669]154
[1701]155} //namespace packet
[2171]156}//namespace orxonox
Note: See TracBrowser for help on using the repository browser.