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