1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Wuest |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
19 | */ |
---|
20 | #define DEBUG_MODULE_NETWORK |
---|
21 | |
---|
22 | #include "factory.h" |
---|
23 | #include "network_stream.h" |
---|
24 | #include "converter.h" |
---|
25 | |
---|
26 | #include "p_node.h" |
---|
27 | #include "state.h" |
---|
28 | #include "game_world.h" |
---|
29 | #include "world_entity.h" |
---|
30 | #include "playable.h" |
---|
31 | #include "player.h" |
---|
32 | #include "network_manager.h" |
---|
33 | |
---|
34 | |
---|
35 | /* include your own header */ |
---|
36 | #include "network_game_manager.h" |
---|
37 | |
---|
38 | |
---|
39 | /* using namespace std is default, this needs to be here */ |
---|
40 | using namespace std; |
---|
41 | |
---|
42 | NetworkGameManager* NetworkGameManager::singletonRef = NULL; |
---|
43 | |
---|
44 | /*! |
---|
45 | * Standard constructor |
---|
46 | */ |
---|
47 | NetworkGameManager::NetworkGameManager() |
---|
48 | : Synchronizeable() |
---|
49 | { |
---|
50 | PRINTF(0)("START\n"); |
---|
51 | |
---|
52 | /* set the class id for the base object */ |
---|
53 | this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); |
---|
54 | |
---|
55 | hasRequestedWorld = false; |
---|
56 | this->setSynchronized(true); |
---|
57 | } |
---|
58 | |
---|
59 | /*! |
---|
60 | * Standard destructor |
---|
61 | */ |
---|
62 | NetworkGameManager::~NetworkGameManager() |
---|
63 | { |
---|
64 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
65 | { |
---|
66 | if ( outBuffer[i].buffer ) |
---|
67 | delete outBuffer[i].buffer; |
---|
68 | } |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | int NetworkGameManager::writeBytes(const byte* data, int length, int sender) |
---|
74 | { |
---|
75 | int i = 0; |
---|
76 | byte b; |
---|
77 | |
---|
78 | while ( i<length ) |
---|
79 | { |
---|
80 | b = data[i++]; |
---|
81 | |
---|
82 | /**************** Commands only processed by servers ****************/ |
---|
83 | if ( isServer() ) |
---|
84 | { |
---|
85 | if ( b == NET_REQUEST_CREATE ) |
---|
86 | { |
---|
87 | if ( !handleRequestCreate( i, data, length, sender ) ) |
---|
88 | return i; |
---|
89 | continue; |
---|
90 | } |
---|
91 | else if ( b == NET_REQUEST_REMOVE ) |
---|
92 | { |
---|
93 | if ( !handleRequestRemove( i, data, length, sender ) ) |
---|
94 | return i; |
---|
95 | continue; |
---|
96 | } |
---|
97 | else if ( b == NET_REQUEST_PNODE_PATH ) |
---|
98 | { |
---|
99 | if ( !handleRequestPNodePath( i, data, length, sender ) ) |
---|
100 | return i; |
---|
101 | continue; |
---|
102 | } |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | /**************** Commands only processed by clients ****************/ |
---|
107 | if ( b == NET_CREATE_ENTITY ) |
---|
108 | { |
---|
109 | PRINTF(0)("CREATE_ENTITY\n"); |
---|
110 | if ( !handleCreateEntity( i, data, length, sender ) ) |
---|
111 | return i; |
---|
112 | continue; |
---|
113 | } |
---|
114 | else if ( b == NET_REMOVE_ENTITY ) |
---|
115 | { |
---|
116 | if ( !handleRemoveEntity( i, data, length, sender ) ) |
---|
117 | return i; |
---|
118 | continue; |
---|
119 | } |
---|
120 | else if ( b == NET_CREATE_ENTITY_LIST ) |
---|
121 | { |
---|
122 | if ( !handleCreateEntityList( i, data, length, sender ) ) |
---|
123 | return i; |
---|
124 | continue; |
---|
125 | } |
---|
126 | else if ( b == NET_REMOVE_ENTITY_LIST ) |
---|
127 | { |
---|
128 | if ( !handleRemoveEntityList( i, data, length, sender ) ) |
---|
129 | return i; |
---|
130 | continue; |
---|
131 | } |
---|
132 | else if ( b == NET_YOU_ARE_ENTITY ) |
---|
133 | { |
---|
134 | if ( !handleYouAreEntity( i, data, length, sender ) ) |
---|
135 | return i; |
---|
136 | continue; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | /**************** Commands processed by servers and clients ****************/ |
---|
141 | if ( b == NET_REQUEST_ENTITY_LIST ) |
---|
142 | { |
---|
143 | sendEntityList( sender ); |
---|
144 | continue; |
---|
145 | } |
---|
146 | else if ( b == NET_REQUEST_SYNC ) |
---|
147 | { |
---|
148 | if ( !handleRequestSync( i, data, length, sender ) ) |
---|
149 | return i; |
---|
150 | continue; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | PRINTF(1)("Network is asynchronous: couldn't decode the command sent by %i\n", sender); |
---|
155 | PRINTF(1)("Probably this is because the network protocol has different \n"); |
---|
156 | PRINTF(1)("versions or there occured an error in the sending algorithm\n"); |
---|
157 | PRINTF(1)("Data is not in the right format! i=%d\n", i); |
---|
158 | return i; |
---|
159 | } |
---|
160 | |
---|
161 | return i; |
---|
162 | } |
---|
163 | |
---|
164 | int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever) |
---|
165 | { |
---|
166 | if ( !isServer() && !hasRequestedWorld ) |
---|
167 | { |
---|
168 | SYNCHELP_WRITE_BEGIN(); |
---|
169 | byte b = NET_REQUEST_ENTITY_LIST; |
---|
170 | SYNCHELP_WRITE_BYTE( b ); |
---|
171 | hasRequestedWorld = true; |
---|
172 | return SYNCHELP_WRITE_N; |
---|
173 | } |
---|
174 | |
---|
175 | for ( int i = 0; i<outBuffer.size(); i++ ) |
---|
176 | { |
---|
177 | *reciever = i; |
---|
178 | if ( outBuffer[i].length>0 ) |
---|
179 | { |
---|
180 | int nbytes = outBuffer[i].length; |
---|
181 | outBuffer[i].length = 0; |
---|
182 | |
---|
183 | if ( nbytes > maxLength ) |
---|
184 | { |
---|
185 | PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength); |
---|
186 | return 0; |
---|
187 | } |
---|
188 | |
---|
189 | memcpy(data, outBuffer[i].buffer, nbytes); |
---|
190 | return nbytes; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | return 0; |
---|
195 | } |
---|
196 | |
---|
197 | void NetworkGameManager::writeDebug() const |
---|
198 | { |
---|
199 | } |
---|
200 | |
---|
201 | void NetworkGameManager::readDebug() const |
---|
202 | { |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | /*! |
---|
207 | * Checks whether this is connected to a server or a client |
---|
208 | * and afterwards creates the needed entity |
---|
209 | * @param classID: The ID of the class of which an entity should be created |
---|
210 | */ |
---|
211 | int NetworkGameManager::createEntity( ClassID classID, int owner ) |
---|
212 | { |
---|
213 | if ( this->isServer()) |
---|
214 | { |
---|
215 | if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 ) |
---|
216 | { |
---|
217 | PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); |
---|
218 | return -1; |
---|
219 | } |
---|
220 | return this->executeCreateEntity( classID, SharedNetworkData::getInstance()->getNewUniqueID(), owner ); |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | this->requestCreateEntity( classID ); |
---|
225 | return -1; |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | /*! |
---|
231 | * Checks whether this is connected to a server or a client |
---|
232 | * and afterwards creates the needed entity |
---|
233 | * @param classID: The ID of the class of which an entity should be created |
---|
234 | */ |
---|
235 | BaseObject* NetworkGameManager::createEntity(const TiXmlElement* element) |
---|
236 | { |
---|
237 | if ( this->isServer() ) |
---|
238 | { |
---|
239 | if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 ) |
---|
240 | { |
---|
241 | PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); |
---|
242 | return NULL; |
---|
243 | } |
---|
244 | |
---|
245 | BaseObject * b = Factory::fabricate( element ); |
---|
246 | |
---|
247 | if ( !b ) |
---|
248 | { |
---|
249 | PRINTF(1)("Could not fabricate Object with className %s\n", element->Value() ); |
---|
250 | return NULL; |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
255 | { |
---|
256 | Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); |
---|
257 | s->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
258 | s->setOwner( 0 ); |
---|
259 | // all entities created via this function are added automaticaly to the synchronizeable list |
---|
260 | s->setSynchronized(true); |
---|
261 | return b; |
---|
262 | } |
---|
263 | else |
---|
264 | { |
---|
265 | PRINTF(1)("Class %s is not a synchronizeable!\n", b->getClassName() ); |
---|
266 | delete b; |
---|
267 | } |
---|
268 | |
---|
269 | } |
---|
270 | else |
---|
271 | |
---|
272 | { |
---|
273 | PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value()); |
---|
274 | } |
---|
275 | return NULL; |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | /*! |
---|
280 | * Checks whether this is connected to a server or a client |
---|
281 | * and afterwards removes the specified entity |
---|
282 | * @param uniqueID: The ID of the entity object which should be removed |
---|
283 | */ |
---|
284 | void NetworkGameManager::removeEntity(int uniqueID) |
---|
285 | { |
---|
286 | if ( this->isServer() ) |
---|
287 | { |
---|
288 | this->executeRemoveEntity( uniqueID ); |
---|
289 | } |
---|
290 | else |
---|
291 | { |
---|
292 | this->requestRemoveEntity( uniqueID ); |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | /*! |
---|
299 | * Creates the needed entity on the server if possible |
---|
300 | * @param classID: The ID of the class of which an entity should be created |
---|
301 | */ |
---|
302 | void NetworkGameManager::requestCreateEntity(ClassID classID) |
---|
303 | { |
---|
304 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
305 | { |
---|
306 | if ( !this->networkStream->isUserIdActive( i ) ) |
---|
307 | continue; |
---|
308 | |
---|
309 | if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_CREATE ) ) |
---|
310 | return; |
---|
311 | if ( !writeToClientBuffer( outBuffer[i], (int)classID ) ) |
---|
312 | return; |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | /*! |
---|
317 | * Removes the specified entity on the server |
---|
318 | * @param uniqueID: The ID of the entity object which should be removed |
---|
319 | */ |
---|
320 | void NetworkGameManager::requestRemoveEntity(int uniqueID) |
---|
321 | { |
---|
322 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
323 | { |
---|
324 | if ( !this->networkStream->isUserIdActive( i ) ) |
---|
325 | continue; |
---|
326 | |
---|
327 | if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_REMOVE ) ) |
---|
328 | return; |
---|
329 | if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) |
---|
330 | return; |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | /*! |
---|
335 | * Creates the needed entity if possible |
---|
336 | * This function is called if this is a server |
---|
337 | * @param classID: The ID of the class of which an entity should be created |
---|
338 | */ |
---|
339 | int NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner) |
---|
340 | { |
---|
341 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
342 | { |
---|
343 | if ( !this->networkStream->isUserIdActive( i ) ) |
---|
344 | continue; |
---|
345 | |
---|
346 | if ( !writeToClientBuffer( outBuffer[i], (byte)NET_CREATE_ENTITY ) ) |
---|
347 | return -1; |
---|
348 | if ( !writeToClientBuffer( outBuffer[i], (int)classID ) ) |
---|
349 | return -1; |
---|
350 | if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) |
---|
351 | return -1; |
---|
352 | if ( !writeToClientBuffer( outBuffer[i], owner ) ) |
---|
353 | return -1; |
---|
354 | } |
---|
355 | |
---|
356 | PRINTF(0)("ExecuteCreateEntity: server side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner); |
---|
357 | doCreateEntity( classID, uniqueID, owner ); |
---|
358 | |
---|
359 | return uniqueID; |
---|
360 | } |
---|
361 | |
---|
362 | /*! |
---|
363 | * Removes the specified entity |
---|
364 | * This function is called if this is a server |
---|
365 | * @param uniqueID: The ID of the entity object which should be removed |
---|
366 | */ |
---|
367 | void NetworkGameManager::executeRemoveEntity(int uniqueID) |
---|
368 | { |
---|
369 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
370 | { |
---|
371 | if ( !this->networkStream->isUserIdActive( i ) ) |
---|
372 | continue; |
---|
373 | |
---|
374 | if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REMOVE_ENTITY ) ) |
---|
375 | return; |
---|
376 | if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) |
---|
377 | return; |
---|
378 | } |
---|
379 | |
---|
380 | doRemoveEntity(uniqueID); |
---|
381 | } |
---|
382 | |
---|
383 | /*! |
---|
384 | * Checks whether it is possible to create an entity of a given class |
---|
385 | * @return: true if the entity can be created, false otherwise |
---|
386 | */ |
---|
387 | bool NetworkGameManager::canCreateEntity(ClassID classID) |
---|
388 | { |
---|
389 | return true; |
---|
390 | } |
---|
391 | |
---|
392 | /*! |
---|
393 | * Sends the Entities to the new connected client |
---|
394 | * @param userID: The ID of the user |
---|
395 | */ |
---|
396 | void NetworkGameManager::sendEntityList( int userID ) |
---|
397 | { |
---|
398 | if ( !isServer() ) |
---|
399 | return; |
---|
400 | |
---|
401 | if ( userID >= outBuffer.size() ) |
---|
402 | resizeBufferVector( userID ); |
---|
403 | |
---|
404 | SynchronizeableList::const_iterator it, e; |
---|
405 | |
---|
406 | it = this->networkStream->getSyncBegin(); |
---|
407 | e = this->networkStream->getSyncEnd(); |
---|
408 | |
---|
409 | // send the packet header |
---|
410 | if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_CREATE_ENTITY_LIST ) ) |
---|
411 | return; |
---|
412 | |
---|
413 | // send the number of entities: -2 because you must not send network_game_manager and handshake |
---|
414 | if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) ) |
---|
415 | return; |
---|
416 | |
---|
417 | //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 ); |
---|
418 | |
---|
419 | // first send the NullParent |
---|
420 | if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getLeafClassID()) ) |
---|
421 | return; |
---|
422 | if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getUniqueID()) ) |
---|
423 | return; |
---|
424 | if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getOwner()) ) |
---|
425 | return; |
---|
426 | |
---|
427 | // now send the rest of the entities |
---|
428 | while ( it != e ) |
---|
429 | { |
---|
430 | if( (*it)->beSynchronized() && (*it) != PNode::getNullParent()) |
---|
431 | { |
---|
432 | PRINTF(0)("SENDING ENTITY %s classid: %x, uniqueid %d\n", (*it)->getClassName(), (*it)->getLeafClassID(), (*it)->getUniqueID() ); |
---|
433 | if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) ) |
---|
434 | return; |
---|
435 | |
---|
436 | if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) ) |
---|
437 | return; |
---|
438 | |
---|
439 | if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) ) |
---|
440 | return; |
---|
441 | } |
---|
442 | it++; |
---|
443 | } |
---|
444 | |
---|
445 | signalNewPlayer( userID ); |
---|
446 | } |
---|
447 | |
---|
448 | |
---|
449 | |
---|
450 | bool NetworkGameManager::signalNewPlayer(int userId) |
---|
451 | { |
---|
452 | if ( userId >= outBuffer.size() ) |
---|
453 | resizeBufferVector( userId ); |
---|
454 | |
---|
455 | /* create new playable for Player*/ |
---|
456 | PRINTF(0)("Request for creation: %i\n", userId); |
---|
457 | int uniqueId = this->createEntity(CL_SPACE_SHIP, userId); |
---|
458 | PRINTF(0)("Request for creation: userid: %i, uniqueid: %i\n", userId, uniqueId); |
---|
459 | this->sendYouAre(uniqueId, userId); |
---|
460 | |
---|
461 | } |
---|
462 | |
---|
463 | |
---|
464 | /** |
---|
465 | * Creates a buffer for user n |
---|
466 | * @param n The ID of the user |
---|
467 | */ |
---|
468 | void NetworkGameManager::resizeBufferVector( int n ) |
---|
469 | { |
---|
470 | for ( int i = outBuffer.size(); i<=n; i++) |
---|
471 | { |
---|
472 | clientBuffer outBuf; |
---|
473 | |
---|
474 | outBuf.length = 0; |
---|
475 | |
---|
476 | outBuf.maxLength = 5*1024; |
---|
477 | |
---|
478 | outBuf.buffer = new byte[5*1014]; |
---|
479 | |
---|
480 | outBuffer.push_back(outBuf); |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | /** |
---|
485 | * Creates the entity on this host |
---|
486 | * @param classID: ClassID of the entity to create |
---|
487 | * @param uniqueID: Unique ID to assign to the synchronizeable |
---|
488 | * @param owner: owner of this synchronizealbe |
---|
489 | */ |
---|
490 | BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner ) |
---|
491 | { |
---|
492 | PRINTF(0)("Creating Entity via Factory: classid: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner); |
---|
493 | |
---|
494 | BaseObject * b; |
---|
495 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
---|
496 | /* Exception 1: NullParent */ |
---|
497 | if( classID == CL_NULL_PARENT) |
---|
498 | { |
---|
499 | b = (BaseObject*)PNode::getNullParent(); |
---|
500 | } |
---|
501 | else |
---|
502 | b = Factory::fabricate( classID ); |
---|
503 | |
---|
504 | if ( !b ) |
---|
505 | { |
---|
506 | PRINTF(1)("Could not fabricate Object with classID %x\n", classID); |
---|
507 | return NULL; |
---|
508 | } |
---|
509 | |
---|
510 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
511 | { |
---|
512 | Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); |
---|
513 | s->setUniqueID( uniqueID ); |
---|
514 | s->setOwner( owner ); |
---|
515 | s->setSynchronized(true); |
---|
516 | //this->networkStream->connectSynchronizeable( *s ); |
---|
517 | if ( !isServer() ) |
---|
518 | s->setIsOutOfSync( true ); |
---|
519 | PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID()); |
---|
520 | |
---|
521 | //HACK: hack to prevent collision |
---|
522 | if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) ) |
---|
523 | { |
---|
524 | if ( NetworkManager::getInstance()->getHostID()!=0 ) |
---|
525 | { |
---|
526 | static Vector pos = Vector(1000.0, 1000.0, 1000.0); |
---|
527 | PNode *p = dynamic_cast<PNode*>(b); |
---|
528 | p->setAbsCoor(pos); |
---|
529 | //p->updateNode(0); |
---|
530 | pos += Vector(1000.0, 1000.0, 1000.0); |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | return b; |
---|
535 | } |
---|
536 | else |
---|
537 | { |
---|
538 | PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID); |
---|
539 | delete b; |
---|
540 | } |
---|
541 | return NULL; |
---|
542 | } |
---|
543 | |
---|
544 | /** |
---|
545 | * Removes a entity on this host |
---|
546 | * @param uniqueID: unique ID assigned with the entity to remove |
---|
547 | */ |
---|
548 | void NetworkGameManager::doRemoveEntity( int uniqueID ) |
---|
549 | { |
---|
550 | SynchronizeableList::const_iterator it,e; |
---|
551 | it = this->networkStream->getSyncBegin(); |
---|
552 | e = this->networkStream->getSyncEnd(); |
---|
553 | |
---|
554 | while ( it != e ) |
---|
555 | { |
---|
556 | if ( (*it)->getUniqueID() == uniqueID ) |
---|
557 | { |
---|
558 | delete *it; |
---|
559 | break; |
---|
560 | } |
---|
561 | it++; |
---|
562 | } |
---|
563 | } |
---|
564 | |
---|
565 | /** |
---|
566 | * Tell the synchronizeable that a user's synchronizeable is out of sync |
---|
567 | * @param uniqueID: unique ID assigned with the entity which is out of sync |
---|
568 | * @param userID: user ID who's synchronizeable is out of sync |
---|
569 | */ |
---|
570 | void NetworkGameManager::doRequestSync( int uniqueID, int userID ) |
---|
571 | { |
---|
572 | SynchronizeableList::const_iterator it,e; |
---|
573 | it = this->networkStream->getSyncBegin(); |
---|
574 | e = this->networkStream->getSyncEnd(); |
---|
575 | |
---|
576 | while ( it != e ) |
---|
577 | { |
---|
578 | if ( (*it)->getUniqueID() == uniqueID ) |
---|
579 | { |
---|
580 | (*it)->requestSync( userID ); |
---|
581 | break; |
---|
582 | } |
---|
583 | it++; |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | /** |
---|
588 | * Copies length bytes to the clientBuffer with error checking |
---|
589 | * @param clientBuffer: the clientBuffer to write to |
---|
590 | * @param data: buffer to the data |
---|
591 | * @param length: length of data |
---|
592 | * @return false on error true else |
---|
593 | */ |
---|
594 | bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length ) |
---|
595 | { |
---|
596 | if ( length > cb.maxLength-cb.length ) |
---|
597 | { |
---|
598 | PRINTF(1)("No space left in clientBuffer\n"); |
---|
599 | return false; |
---|
600 | } |
---|
601 | |
---|
602 | memcpy( cb.buffer+cb.length, data, length ); |
---|
603 | return true; |
---|
604 | } |
---|
605 | |
---|
606 | /** |
---|
607 | * Reads data from clientBuffer with error checking |
---|
608 | * @param clientBuffer: the clientBuffer to read from |
---|
609 | * @param data: pointer to the buffer |
---|
610 | * @param length: |
---|
611 | * @return |
---|
612 | */ |
---|
613 | bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length ) |
---|
614 | { |
---|
615 | if ( cb.length < length ) |
---|
616 | { |
---|
617 | PRINTF(0)("There is not enough data in clientBuffer\n"); |
---|
618 | return 0; |
---|
619 | } |
---|
620 | |
---|
621 | memcpy( data, cb.buffer+cb.length-length, length ); |
---|
622 | return true; |
---|
623 | } |
---|
624 | |
---|
625 | /** |
---|
626 | * Tells this client that he has to control this entity |
---|
627 | * @param uniqueID: the entity's uniqeID |
---|
628 | */ |
---|
629 | void NetworkGameManager::doYouAre( int uniqueID ) |
---|
630 | { |
---|
631 | |
---|
632 | SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin(); |
---|
633 | |
---|
634 | Playable *p = NULL; |
---|
635 | Synchronizeable *s = NULL; |
---|
636 | |
---|
637 | for ( ; it !=networkStream->getSyncEnd(); it++ ) |
---|
638 | { |
---|
639 | if ( (*it)->getUniqueID()==uniqueID ) |
---|
640 | { |
---|
641 | if ( (*it)->isA( CL_SYNCHRONIZEABLE ) ) |
---|
642 | { |
---|
643 | s = dynamic_cast<Synchronizeable*>(*it); |
---|
644 | } |
---|
645 | if ( (*it)->isA( CL_PLAYABLE ) ) |
---|
646 | { |
---|
647 | p = dynamic_cast<Playable*>(*it); |
---|
648 | break; |
---|
649 | } else |
---|
650 | { |
---|
651 | PRINTF(1)("UniqueID is not a Playable\n"); |
---|
652 | } |
---|
653 | } |
---|
654 | } |
---|
655 | |
---|
656 | Player* player = State::getPlayer(); |
---|
657 | assert(p != NULL); |
---|
658 | assert(s != NULL); |
---|
659 | assert(player != NULL); |
---|
660 | |
---|
661 | s->setIsOutOfSync( true ); |
---|
662 | |
---|
663 | PRINTF(0)("uniqueID = %d\n", s->getUniqueID()); |
---|
664 | |
---|
665 | player->setControllable(p); |
---|
666 | |
---|
667 | |
---|
668 | } |
---|
669 | |
---|
670 | /** |
---|
671 | * Tells a remote client that he has to control this entity |
---|
672 | * @param uniqueID: the entity's uniqeID |
---|
673 | * @param userID: the users ID |
---|
674 | */ |
---|
675 | void NetworkGameManager::sendYouAre( int uniqueID, int userID ) |
---|
676 | { |
---|
677 | if ( !isServer() ) |
---|
678 | return; |
---|
679 | |
---|
680 | if ( userID != 0 ) |
---|
681 | { |
---|
682 | if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_YOU_ARE_ENTITY ) ) |
---|
683 | return; |
---|
684 | |
---|
685 | if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) ) |
---|
686 | return; |
---|
687 | } |
---|
688 | else |
---|
689 | { |
---|
690 | doYouAre(uniqueID); |
---|
691 | } |
---|
692 | } |
---|
693 | |
---|
694 | bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender ) |
---|
695 | { |
---|
696 | if ( INTSIZE > length-i ) |
---|
697 | { |
---|
698 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
699 | return false; |
---|
700 | } |
---|
701 | int classID; |
---|
702 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
703 | |
---|
704 | createEntity( (ClassID)classID ); |
---|
705 | |
---|
706 | return true; |
---|
707 | } |
---|
708 | |
---|
709 | bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender ) |
---|
710 | { |
---|
711 | if ( INTSIZE > length-i ) |
---|
712 | { |
---|
713 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
714 | return false; |
---|
715 | } |
---|
716 | int uniqueID; |
---|
717 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
718 | |
---|
719 | removeEntity( uniqueID ); |
---|
720 | |
---|
721 | return true; |
---|
722 | } |
---|
723 | |
---|
724 | bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender ) |
---|
725 | { |
---|
726 | if ( INTSIZE > length-i ) |
---|
727 | { |
---|
728 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
729 | return false; |
---|
730 | } |
---|
731 | int classID; |
---|
732 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
733 | |
---|
734 | if ( INTSIZE > length-i ) |
---|
735 | { |
---|
736 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
737 | return false; |
---|
738 | } |
---|
739 | int uniqueID; |
---|
740 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
741 | |
---|
742 | if ( INTSIZE > length-i ) |
---|
743 | { |
---|
744 | PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); |
---|
745 | return false; |
---|
746 | } |
---|
747 | int owner; |
---|
748 | i += Converter::byteArrayToInt( &data[i], &owner ); |
---|
749 | |
---|
750 | PRINTF(0)("handleCreateEntity: client side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner); |
---|
751 | doCreateEntity( (ClassID)classID, uniqueID, owner ); |
---|
752 | |
---|
753 | return true; |
---|
754 | } |
---|
755 | |
---|
756 | bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender ) |
---|
757 | { |
---|
758 | if ( INTSIZE > length-i ) |
---|
759 | { |
---|
760 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
761 | return false; |
---|
762 | } |
---|
763 | int uniqueID; |
---|
764 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
765 | |
---|
766 | doRemoveEntity( uniqueID ); |
---|
767 | |
---|
768 | return true; |
---|
769 | } |
---|
770 | |
---|
771 | bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender ) |
---|
772 | { |
---|
773 | if ( INTSIZE > length-i ) |
---|
774 | { |
---|
775 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
776 | return false; |
---|
777 | } |
---|
778 | |
---|
779 | PRINTF(0)("HandleCreateEntityList: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]); |
---|
780 | |
---|
781 | int n; |
---|
782 | i += Converter::byteArrayToInt( &data[i], &n ); |
---|
783 | |
---|
784 | |
---|
785 | PRINTF(0)("HandleCreateEntityList: n = %d\n", n); |
---|
786 | |
---|
787 | int classID, uniqueID, owner; |
---|
788 | |
---|
789 | for ( int j = 0; j<n; j++ ) |
---|
790 | { |
---|
791 | |
---|
792 | if ( INTSIZE > length-i ) |
---|
793 | { |
---|
794 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
795 | return false; |
---|
796 | } |
---|
797 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
798 | |
---|
799 | if ( INTSIZE > length-i ) |
---|
800 | { |
---|
801 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
802 | return false; |
---|
803 | } |
---|
804 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
805 | |
---|
806 | if ( INTSIZE > length-i ) |
---|
807 | { |
---|
808 | PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); |
---|
809 | return false; |
---|
810 | } |
---|
811 | i += Converter::byteArrayToInt( &data[i], &owner ); |
---|
812 | |
---|
813 | if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE ) |
---|
814 | { |
---|
815 | BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner ); |
---|
816 | } |
---|
817 | |
---|
818 | } |
---|
819 | |
---|
820 | return true; |
---|
821 | } |
---|
822 | |
---|
823 | bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender ) |
---|
824 | { |
---|
825 | if ( INTSIZE > length-i ) |
---|
826 | { |
---|
827 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
828 | return false; |
---|
829 | } |
---|
830 | int n; |
---|
831 | i += Converter::byteArrayToInt( &data[i], &n ); |
---|
832 | |
---|
833 | int uniqueID; |
---|
834 | |
---|
835 | for ( int j = 0; j<n; j++ ) |
---|
836 | { |
---|
837 | |
---|
838 | if ( INTSIZE > length-i ) |
---|
839 | { |
---|
840 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
841 | return false; |
---|
842 | } |
---|
843 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
844 | |
---|
845 | doRemoveEntity( uniqueID ); |
---|
846 | } |
---|
847 | |
---|
848 | return true; |
---|
849 | } |
---|
850 | |
---|
851 | bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender ) |
---|
852 | { |
---|
853 | if ( INTSIZE > length-i ) |
---|
854 | { |
---|
855 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
856 | return false; |
---|
857 | } |
---|
858 | |
---|
859 | int uniqueID; |
---|
860 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
861 | |
---|
862 | doYouAre( uniqueID ); |
---|
863 | |
---|
864 | return true; |
---|
865 | } |
---|
866 | |
---|
867 | bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender ) |
---|
868 | { |
---|
869 | if ( INTSIZE > length-i ) |
---|
870 | { |
---|
871 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
872 | return false; |
---|
873 | } |
---|
874 | int uniqueID; |
---|
875 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
876 | |
---|
877 | doRequestSync( uniqueID, sender ); |
---|
878 | |
---|
879 | return true; |
---|
880 | } |
---|
881 | |
---|
882 | |
---|
883 | /** |
---|
884 | * handles the network signal NET_REQUEST_PNODE_PATH |
---|
885 | * @param i byte offset in the buffer |
---|
886 | * @param data data array |
---|
887 | * @param length length of the data arary |
---|
888 | * @param sender the sender id |
---|
889 | * @return true if process terminated sucessfully |
---|
890 | */ |
---|
891 | bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender) |
---|
892 | { |
---|
893 | if( INTSIZE > length-i ) |
---|
894 | { |
---|
895 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
896 | return false; |
---|
897 | } |
---|
898 | PRINTF(0)("HandleRequestPNodePath: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]); |
---|
899 | |
---|
900 | int uniqueID1, uniqueID2; |
---|
901 | if( INTSIZE > length-i ) |
---|
902 | { |
---|
903 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
904 | return false; |
---|
905 | } |
---|
906 | i += Converter::byteArrayToInt( &data[i], &uniqueID1 ); |
---|
907 | |
---|
908 | if( INTSIZE > length-i ) |
---|
909 | { |
---|
910 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
911 | return false; |
---|
912 | } |
---|
913 | i += Converter::byteArrayToInt( &data[i], &uniqueID2 ); |
---|
914 | |
---|
915 | |
---|
916 | PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2); |
---|
917 | |
---|
918 | return true; |
---|
919 | } |
---|
920 | |
---|
921 | |
---|
922 | bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b ) |
---|
923 | { |
---|
924 | if ( cb.maxLength-cb.length < 1 ) |
---|
925 | { |
---|
926 | PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n"); |
---|
927 | return false; |
---|
928 | } |
---|
929 | |
---|
930 | cb.buffer[cb.length++] = b; |
---|
931 | |
---|
932 | return true; |
---|
933 | } |
---|
934 | |
---|
935 | bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i ) |
---|
936 | { |
---|
937 | int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length ); |
---|
938 | cb.length += n; |
---|
939 | |
---|
940 | if ( n <= 0 ) |
---|
941 | { |
---|
942 | PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n"); |
---|
943 | return false; |
---|
944 | } |
---|
945 | |
---|
946 | return true; |
---|
947 | } |
---|
948 | |
---|
949 | void NetworkGameManager::sync( int uniqueID, int owner ) |
---|
950 | { |
---|
951 | /*if ( owner==this->getHostID() ) |
---|
952 | return;*/ |
---|
953 | |
---|
954 | if ( !isServer() ) |
---|
955 | executeRequestSync( uniqueID, 0 ); |
---|
956 | else |
---|
957 | executeRequestSync( uniqueID, owner ); |
---|
958 | } |
---|
959 | |
---|
960 | void NetworkGameManager::executeRequestSync( int uniqueID, int user ) |
---|
961 | { |
---|
962 | if ( user >= outBuffer.size() ) |
---|
963 | resizeBufferVector( user ); |
---|
964 | |
---|
965 | if ( !writeToClientBuffer( outBuffer[user], (byte)NET_REQUEST_SYNC ) ) |
---|
966 | return; |
---|
967 | if ( !writeToClientBuffer( outBuffer[user], uniqueID ) ) |
---|
968 | return; |
---|
969 | } |
---|
970 | |
---|