Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/packet/FunctionCalls.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: 9.3 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 <scheusso [at] ee.ethz.ch>, (C) 2008
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "FunctionCalls.h"
30
31#include <cassert>
32#include "util/MultiType.h"
33#include "network/NetworkFunction.h"
34
35namespace orxonox {
36namespace packet {
37 
38#define   PACKET_FLAGS_FUNCTIONCALLS PacketFlag::Reliable
39#define   _PACKETID         0
40const unsigned int FUNCTIONCALLS_MEM_ALLOCATION = 1000;
41   
42FunctionCalls::FunctionCalls()
43 : Packet()
44{
45  flags_ = flags_ | PACKET_FLAGS_FUNCTIONCALLS;
46  currentSize_ = 2*sizeof(uint32_t); // for packetid and nrOfCalls
47  nrOfCalls_ = 0;
48  currentMemBlocks_ = 1;
49  data_=new uint8_t[ FUNCTIONCALLS_MEM_ALLOCATION ];
50  *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::FunctionCalls;
51  *(uint32_t*)(data_+sizeof(uint32_t)) = 0; // set nrOfCalls to 0
52}
53
54FunctionCalls::FunctionCalls( uint8_t* data, unsigned int clientID )
55  : Packet(data, clientID)
56{
57}
58
59FunctionCalls::~FunctionCalls()
60{
61}
62
63
64bool FunctionCalls::process(){
65  assert(isDataENetAllocated());
66  uint8_t* temp = data_+sizeof(uint32_t); //skip packetid
67  this->nrOfCalls_ = *(uint32_t*)temp;
68  temp += sizeof(uint32_t);
69  for( unsigned int i = 0; i<this->nrOfCalls_; i++ )
70  {
71    uint32_t functionID = *(uint32_t*)temp;
72    bool isStatic = *(uint8_t*)(temp+sizeof(uint32_t));
73    if( isStatic )
74    {
75      MultiType mt1, mt2, mt3, mt4, mt5;
76      NetworkFunctionStatic *fct = NetworkFunctionStatic::getFunction( functionID );
77      uint32_t nrOfArguments = *(uint32_t*)(temp+sizeof(uint32_t)+sizeof(uint8_t));
78      temp+=2*sizeof(uint32_t)+sizeof(uint8_t);
79      switch(nrOfArguments)
80      {
81        case 0:
82          fct->call();
83          break;
84        case 1:
85          mt1.importData(temp);
86          fct->call(mt1);
87          break;
88        case 2:
89          mt1.importData(temp);
90          mt2.importData(temp);
91          fct->call(mt1, mt2);
92          break;
93        case 3:
94          mt1.importData(temp);
95          mt2.importData(temp);
96          mt3.importData(temp);
97          fct->call(mt1, mt2, mt3);
98          break;
99        case 4:
100          mt1.importData(temp);
101          mt2.importData(temp);
102          mt3.importData(temp);
103          mt4.importData(temp);
104          fct->call(mt1, mt2, mt3, mt4);
105          break;
106        case 5:
107          mt1.importData(temp);
108          mt2.importData(temp);
109          mt3.importData(temp);
110          mt4.importData(temp);
111          mt5.importData(temp);
112          fct->call(mt1, mt2, mt3, mt4, mt5);
113          break;
114        default:
115          assert(0);
116      }
117    }
118    else // not a static function, so also handle the objectID
119    {
120      MultiType mt1, mt2, mt3, mt4, mt5;
121      NetworkMemberFunctionBase *fct = NetworkMemberFunctionBase::getFunction( functionID );
122      uint32_t nrOfArguments = *(uint32_t*)(temp+sizeof(uint32_t)+sizeof(uint8_t));
123      uint32_t objectID = *(uint32_t*)(temp+2*sizeof(uint32_t)+sizeof(uint8_t));
124      temp+=3*sizeof(uint32_t)+sizeof(uint8_t);
125      switch(nrOfArguments)
126      {
127        case 0:
128          fct->call(objectID);
129          break;
130        case 1:
131          mt1.importData(temp);
132          fct->call(objectID, mt1);
133          break;
134        case 2:
135          mt1.importData(temp);
136          mt2.importData(temp);
137          fct->call(objectID, mt1, mt2);
138          break;
139        case 3:
140          mt1.importData(temp);
141          mt2.importData(temp);
142          mt3.importData(temp);
143          fct->call(objectID, mt1, mt2, mt3);
144          break;
145        case 4:
146          mt1.importData(temp);
147          mt2.importData(temp);
148          mt3.importData(temp);
149          mt4.importData(temp);
150          fct->call(objectID, mt1, mt2, mt3, mt4);
151          break;
152        case 5:
153          mt1.importData(temp);
154          mt2.importData(temp);
155          mt3.importData(temp);
156          mt4.importData(temp);
157          mt5.importData(temp);
158          fct->call(objectID, mt1, mt2, mt3, mt4, mt5);
159          break;
160        default:
161          assert(0);
162          break;
163      }
164    }
165  }
166  delete this;
167  return true;
168}
169
170void FunctionCalls::addCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
171  assert(!isDataENetAllocated());
172 
173  // first determine the size that has to be reserved for this call
174  uint32_t callsize = 2*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and for bool isStatic
175  uint32_t nrOfArguments = 0;
176  if(mt1)
177  {
178    nrOfArguments++;
179    callsize += mt1->getNetworkSize();
180    if(mt2)
181    {
182      nrOfArguments++;
183      callsize += mt2->getNetworkSize();
184      if(mt3)
185      {
186        nrOfArguments++;
187        callsize += mt3->getNetworkSize();
188        if(mt4)
189        {
190          nrOfArguments++;
191          callsize += mt4->getNetworkSize();
192          if(mt5)
193          {
194            nrOfArguments++;
195            callsize += mt5->getNetworkSize();
196          }
197        }
198      }
199    }
200  }
201 
202  // now allocated mem if neccessary
203  if( currentSize_ + callsize > currentMemBlocks_*FUNCTIONCALLS_MEM_ALLOCATION )
204  {
205    currentMemBlocks_ = (currentSize_ + callsize)%FUNCTIONCALLS_MEM_ALLOCATION+1;
206    uint8_t *temp = new uint8_t[currentMemBlocks_*FUNCTIONCALLS_MEM_ALLOCATION];
207    memcpy( temp, data_, currentSize_ );
208    delete[] data_;
209    data_ = temp;
210  }
211 
212  // now serialise the mt values and copy the function id and isStatic
213  uint8_t* temp = data_+currentSize_;
214  *(uint32_t*)(data_+sizeof(uint32_t)) = *(uint32_t*)(data_+sizeof(uint32_t))+1; // increase number of calls
215  *(uint32_t*)temp = networkID;
216  *(uint8_t*)(temp+sizeof(uint32_t)) = true;
217  *(uint32_t*)(temp+sizeof(uint32_t)+sizeof(uint8_t)) = nrOfArguments;
218  temp += 2*sizeof(uint32_t)+sizeof(uint8_t);
219  if(mt1)
220  {
221    mt1->exportData( temp ); //temp gets automatically increased
222    if(mt2)
223    {
224      mt2->exportData( temp ); //temp gets automatically increased
225      if(mt3)
226      {
227        mt3->exportData( temp ); //temp gets automatically increased
228        if(mt4)
229        {
230          mt4->exportData( temp ); //temp gets automatically increased
231          if(mt5)
232          {
233            mt5->exportData( temp ); //temp gets automatically increased
234          }
235        }
236      }
237    }
238  }
239  //currentSize_ += callsize;
240  currentSize_ = temp-data_;
241 
242}
243
244void FunctionCalls::addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
245  assert(!isDataENetAllocated());
246 
247  // first determine the size that has to be reserved for this call
248  uint32_t callsize = 3*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and the objectID
249  uint32_t nrOfArguments = 0;
250  if(mt1)
251  {
252    nrOfArguments++;
253    callsize += mt1->getNetworkSize();
254    if(mt2)
255    {
256      nrOfArguments++;
257      callsize += mt2->getNetworkSize();
258      if(mt3)
259      {
260        nrOfArguments++;
261        callsize += mt3->getNetworkSize();
262        if(mt4)
263        {
264          nrOfArguments++;
265          callsize += mt4->getNetworkSize();
266          if(mt5)
267          {
268            nrOfArguments++;
269            callsize += mt5->getNetworkSize();
270          }
271        }
272      }
273    }
274  }
275 
276  // now allocated mem if neccessary
277  if( currentSize_ + callsize > currentMemBlocks_*FUNCTIONCALLS_MEM_ALLOCATION )
278  {
279    currentMemBlocks_ = (currentSize_ + callsize)%FUNCTIONCALLS_MEM_ALLOCATION+1;
280    uint8_t *temp = new uint8_t[currentMemBlocks_*FUNCTIONCALLS_MEM_ALLOCATION];
281    memcpy( temp, data_, currentSize_ );
282    delete[] data_;
283    data_ = temp;
284  }
285 
286  // now serialise the mt values and copy the function id
287  uint8_t* temp = data_+currentSize_;
288  *(uint32_t*)(data_+sizeof(uint32_t)) = *(uint32_t*)(data_+sizeof(uint32_t))+1; // increase number of calls
289  *(uint32_t*)temp = networkID;
290  *(uint8_t*)(temp+sizeof(uint32_t)) = false;
291  *(uint32_t*)(temp+sizeof(uint32_t)+sizeof(uint8_t)) = nrOfArguments;
292  *(uint32_t*)(temp+2*sizeof(uint32_t)+sizeof(uint8_t)) = objectID;
293  temp += 3*sizeof(uint32_t)+sizeof(uint8_t);
294  if(mt1)
295  {
296    mt1->exportData( temp ); //temp gets automatically increased
297    if(mt2)
298    {
299      mt2->exportData( temp ); //temp gets automatically increased
300      if(mt3)
301      {
302        mt3->exportData( temp ); //temp gets automatically increased
303        if(mt4)
304        {
305          mt4->exportData( temp ); //temp gets automatically increased
306          if(mt5)
307          {
308            mt5->exportData( temp ); //temp gets automatically increased
309          }
310        }
311      }
312    }
313  }
314  currentSize_ += callsize;
315 
316}
317
318
319} //namespace packet
320} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.