Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp2/src/network/packet/FunctionIDs.cc @ 2938

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