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
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        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);
101    }
102  }
103  return true;
104}
105
106void FunctionCall::setCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
107
108  // first determine the size that has to be reserved for this call
109  uint32_t callsize = 2*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and for bool isStatic
110  uint32_t nrOfArguments = 0;
111  if(mt1)
112  {
113    nrOfArguments++;
114    callsize += mt1->getNetworkSize();
115    this->arguments_.push_back(*mt1);
116    if(mt2)
117    {
118      nrOfArguments++;
119      callsize += mt2->getNetworkSize();
120      this->arguments_.push_back(*mt2);
121      if(mt3)
122      {
123        nrOfArguments++;
124        callsize += mt3->getNetworkSize();
125        this->arguments_.push_back(*mt3);
126        if(mt4)
127        {
128          nrOfArguments++;
129          callsize += mt4->getNetworkSize();
130          this->arguments_.push_back(*mt4);
131          if(mt5)
132          {
133            nrOfArguments++;
134            callsize += mt5->getNetworkSize();
135            this->arguments_.push_back(*mt5);
136          }
137        }
138      }
139    }
140  }
141  this->nrOfArguments_ = nrOfArguments;
142  this->size_ = callsize;
143  this->bIsStatic_ = true;
144  this->functionID_ = networkID;
145}
146
147void FunctionCall::setCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
148
149  // first determine the size that has to be reserved for this call
150  uint32_t callsize = 3*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and the objectID and bIsStatic
151  uint32_t nrOfArguments = 0;
152  if(mt1)
153  {
154    nrOfArguments++;
155    callsize += mt1->getNetworkSize();
156    this->arguments_.push_back(*mt1);
157    if(mt2)
158    {
159      nrOfArguments++;
160      callsize += mt2->getNetworkSize();
161      this->arguments_.push_back(*mt2);
162      if(mt3)
163      {
164        nrOfArguments++;
165        callsize += mt3->getNetworkSize();
166        this->arguments_.push_back(*mt3);
167        if(mt4)
168        {
169          nrOfArguments++;
170          callsize += mt4->getNetworkSize();
171          this->arguments_.push_back(*mt4);
172          if(mt5)
173          {
174            nrOfArguments++;
175            callsize += mt5->getNetworkSize();
176            this->arguments_.push_back(*mt5);
177          }
178        }
179      }
180    }
181  }
182  this->nrOfArguments_ = nrOfArguments;
183  this->bIsStatic_ = false;
184  this->functionID_ = networkID;
185  this->size_ = callsize;
186  this->objectID_ = objectID;
187}
188
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_ )
195  {
196    mem += 2*sizeof(uint32_t)+sizeof(uint8_t);
197  }
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}
209
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_ )
217  {
218    mem += 2*sizeof(uint32_t)+sizeof(uint8_t);
219  }
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  }
228}
229
230
231
232} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.