Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/network/FunctionCall.cc @ 10471

Last change on this file since 10471 was 10471, checked in by landauf, 9 years ago

moved static maps from NetworkFunctionStatic and NetworkMemberFunctionBase to NetworkFunctionManager

  • Property svn:eol-style set to native
File size: 7.0 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] orxonox.net>, (C) 2010
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "FunctionCall.h"
30
31#include <cassert>
32#include "util/MultiType.h"
33#include "NetworkFunction.h"
34#include "NetworkFunctionManager.h"
35
36namespace orxonox {
37
38FunctionCall::FunctionCall()
39 : nrOfArguments_(-1), objectID_(OBJECTID_UNKNOWN), size_(0)
40{
41}
42
43FunctionCall::~FunctionCall()
44{
45}
46
47
48bool FunctionCall::execute(){
49  if( this->bIsStatic_ )
50  {
51    NetworkFunctionStatic *fct = static_cast<NetworkFunctionStatic*>(NetworkFunctionManager::getFunction( this->functionID_ ));
52    assert( this->nrOfArguments_==this->arguments_.size() );
53    switch(this->nrOfArguments_)
54    {
55      case 0:
56        fct->call();
57        break;
58      case 1:
59        fct->call(this->arguments_[0]);
60        break;
61      case 2:
62        fct->call(this->arguments_[0], this->arguments_[1]);
63        break;
64      case 3:
65        fct->call(this->arguments_[0], this->arguments_[1], this->arguments_[2]);
66        break;
67      case 4:
68        fct->call(this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3]);
69        break;
70      case 5:
71        fct->call(this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3], this->arguments_[4]);
72        break;
73      default:
74        assert(0);
75    }
76  }
77  else // not a static function, so also handle with the objectID
78  {
79    NetworkMemberFunctionBase *fct = static_cast<NetworkMemberFunctionBase*>(NetworkFunctionManager::getFunction( this->functionID_ ));
80    switch(this->nrOfArguments_)
81    {
82      case 0:
83        if( !fct->call(this->objectID_) )
84          return false;
85        break;
86      case 1:
87        if( !fct->call(this->objectID_, this->arguments_[0]) )
88          return false;
89        break;
90      case 2:
91        if( !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1]) )
92          return false;
93        break;
94      case 3:
95        if( !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1], this->arguments_[2]) )
96          return false;
97        break;
98      case 4:
99        if( !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3]) )
100          return false;
101        break;
102      case 5:
103        if( !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3], this->arguments_[4]) )
104          return false;
105        break;
106      default:
107        assert(0);
108    }
109  }
110  return true;
111}
112
113void FunctionCall::setCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
114
115  // first determine the size that has to be reserved for this call
116  uint32_t callsize = 2*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and for bool isStatic
117  uint32_t nrOfArguments = 0;
118  if(mt1)
119  {
120    nrOfArguments++;
121    callsize += mt1->getNetworkSize();
122    this->arguments_.push_back(*mt1);
123    if(mt2)
124    {
125      nrOfArguments++;
126      callsize += mt2->getNetworkSize();
127      this->arguments_.push_back(*mt2);
128      if(mt3)
129      {
130        nrOfArguments++;
131        callsize += mt3->getNetworkSize();
132        this->arguments_.push_back(*mt3);
133        if(mt4)
134        {
135          nrOfArguments++;
136          callsize += mt4->getNetworkSize();
137          this->arguments_.push_back(*mt4);
138          if(mt5)
139          {
140            nrOfArguments++;
141            callsize += mt5->getNetworkSize();
142            this->arguments_.push_back(*mt5);
143          }
144        }
145      }
146    }
147  }
148  this->nrOfArguments_ = nrOfArguments;
149  this->size_ = callsize;
150  this->bIsStatic_ = true;
151  this->functionID_ = networkID;
152}
153
154void FunctionCall::setCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
155
156  // first determine the size that has to be reserved for this call
157  uint32_t callsize = 3*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and the objectID and bIsStatic
158  uint32_t nrOfArguments = 0;
159  if(mt1)
160  {
161    nrOfArguments++;
162    callsize += mt1->getNetworkSize();
163    this->arguments_.push_back(*mt1);
164    if(mt2)
165    {
166      nrOfArguments++;
167      callsize += mt2->getNetworkSize();
168      this->arguments_.push_back(*mt2);
169      if(mt3)
170      {
171        nrOfArguments++;
172        callsize += mt3->getNetworkSize();
173        this->arguments_.push_back(*mt3);
174        if(mt4)
175        {
176          nrOfArguments++;
177          callsize += mt4->getNetworkSize();
178          this->arguments_.push_back(*mt4);
179          if(mt5)
180          {
181            nrOfArguments++;
182            callsize += mt5->getNetworkSize();
183            this->arguments_.push_back(*mt5);
184          }
185        }
186      }
187    }
188  }
189  this->nrOfArguments_ = nrOfArguments;
190  this->bIsStatic_ = false;
191  this->functionID_ = networkID;
192  this->size_ = callsize;
193  this->objectID_ = objectID;
194}
195
196void FunctionCall::loadData(uint8_t*& mem)
197{
198  this->functionID_ = *(uint32_t*)mem;
199  this->bIsStatic_ = *(uint8_t*)(mem+sizeof(uint32_t));
200  this->nrOfArguments_ = *(uint32_t*)(mem+sizeof(uint32_t)+sizeof(uint8_t));
201  if( this->bIsStatic_ )
202  {
203    mem += 2*sizeof(uint32_t)+sizeof(uint8_t);
204  }
205  else
206  {
207    this->objectID_ = *(uint32_t*)(mem+2*sizeof(uint32_t)+sizeof(uint8_t));
208    mem += 3*sizeof(uint32_t)+sizeof(uint8_t);
209  }
210  for( unsigned int i=0; i<this->nrOfArguments_; ++i )
211  {
212    this->arguments_.push_back(MultiType());
213    this->arguments_.back().importData(mem);
214  }
215}
216
217void FunctionCall::saveData(uint8_t*& mem)
218{
219  // now serialise the mt values and copy the function id and isStatic
220  *(uint32_t*)mem = this->functionID_;
221  *(uint8_t*)(mem+sizeof(uint32_t)) = this->bIsStatic_;
222  *(uint32_t*)(mem+sizeof(uint32_t)+sizeof(uint8_t)) = this->nrOfArguments_;
223  if( this->bIsStatic_ )
224  {
225    mem += 2*sizeof(uint32_t)+sizeof(uint8_t);
226  }
227  else
228  {
229    *(uint32_t*)(mem+2*sizeof(uint32_t)+sizeof(uint8_t)) = this->objectID_;
230    mem += 3*sizeof(uint32_t)+sizeof(uint8_t);
231  }
232  for( std::vector<MultiType>::iterator it = this->arguments_.begin(); it!=this->arguments_.end(); ++it )
233  {
234    it->exportData( mem );
235  }
236}
237
238
239
240} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.