Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/FunctionCall.cc @ 9270

Last change on this file since 9270 was 7503, checked in by scheusso, 14 years ago

don't know where this else got lost

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