Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cleaning up function calls a bit in order to buffer calls for not (yet) existing objects

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