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 | #include "class_list.h" |
---|
35 | |
---|
36 | /* include your own header */ |
---|
37 | #include "network_game_manager.h" |
---|
38 | |
---|
39 | |
---|
40 | /* using namespace std is default, this needs to be here */ |
---|
41 | using namespace std; |
---|
42 | |
---|
43 | NetworkGameManager* NetworkGameManager::singletonRef = NULL; |
---|
44 | |
---|
45 | /*! |
---|
46 | * Standard constructor |
---|
47 | */ |
---|
48 | NetworkGameManager::NetworkGameManager() |
---|
49 | : Synchronizeable() |
---|
50 | { |
---|
51 | PRINTF(0)("START\n"); |
---|
52 | |
---|
53 | /* set the class id for the base object */ |
---|
54 | this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); |
---|
55 | |
---|
56 | hasRequestedWorld = false; |
---|
57 | this->setSynchronized(true); |
---|
58 | } |
---|
59 | |
---|
60 | /*! |
---|
61 | * Standard destructor |
---|
62 | */ |
---|
63 | NetworkGameManager::~NetworkGameManager() |
---|
64 | { |
---|
65 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
66 | { |
---|
67 | if ( outBuffer[i].buffer ) |
---|
68 | delete outBuffer[i].buffer; |
---|
69 | } |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | int NetworkGameManager::writeBytes(const byte* data, int length, int sender) |
---|
75 | { |
---|
76 | int i = 0; |
---|
77 | byte b; |
---|
78 | |
---|
79 | while ( i<length ) |
---|
80 | { |
---|
81 | b = data[i++]; |
---|
82 | |
---|
83 | /**************** Commands only processed by servers ****************/ |
---|
84 | if ( isServer() ) |
---|
85 | { |
---|
86 | if ( b == NET_REQUEST_CREATE ) |
---|
87 | { |
---|
88 | if ( !handleRequestCreate( i, data, length, sender ) ) |
---|
89 | return i; |
---|
90 | continue; |
---|
91 | } |
---|
92 | else if ( b == NET_REQUEST_REMOVE ) |
---|
93 | { |
---|
94 | if ( !handleRequestRemove( i, data, length, sender ) ) |
---|
95 | return i; |
---|
96 | continue; |
---|
97 | } |
---|
98 | else if ( b == NET_REQUEST_PNODE_PATH ) |
---|
99 | { |
---|
100 | if ( !handleRequestPNodePath( i, data, length, sender ) ) |
---|
101 | return i; |
---|
102 | continue; |
---|
103 | } |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | /**************** Commands only processed by clients ****************/ |
---|
108 | if ( b == NET_CREATE_ENTITY ) |
---|
109 | { |
---|
110 | PRINTF(0)("CREATE_ENTITY\n"); |
---|
111 | if ( !handleCreateEntity( i, data, length, sender ) ) |
---|
112 | return i; |
---|
113 | continue; |
---|
114 | } |
---|
115 | else if ( b == NET_REMOVE_ENTITY ) |
---|
116 | { |
---|
117 | if ( !handleRemoveEntity( i, data, length, sender ) ) |
---|
118 | return i; |
---|
119 | continue; |
---|
120 | } |
---|
121 | else if ( b == NET_CREATE_ENTITY_LIST ) |
---|
122 | { |
---|
123 | if ( !handleCreateEntityList( i, data, length, sender ) ) |
---|
124 | return i; |
---|
125 | continue; |
---|
126 | } |
---|
127 | else if ( b == NET_REMOVE_ENTITY_LIST ) |
---|
128 | { |
---|
129 | if ( !handleRemoveEntityList( i, data, length, sender ) ) |
---|
130 | return i; |
---|
131 | continue; |
---|
132 | } |
---|
133 | else if ( b == NET_YOU_ARE_ENTITY ) |
---|
134 | { |
---|
135 | if ( !handleYouAreEntity( i, data, length, sender ) ) |
---|
136 | return i; |
---|
137 | continue; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | /**************** Commands processed by servers and clients ****************/ |
---|
142 | if ( b == NET_REQUEST_ENTITY_LIST ) |
---|
143 | { |
---|
144 | sendEntityList( sender ); |
---|
145 | continue; |
---|
146 | } |
---|
147 | else if ( b == NET_REQUEST_SYNC ) |
---|
148 | { |
---|
149 | if ( !handleRequestSync( i, data, length, sender ) ) |
---|
150 | return i; |
---|
151 | continue; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | PRINTF(1)("Network is asynchronous: couldn't decode the command sent by %i\n", sender); |
---|
156 | PRINTF(1)("Probably this is because the network protocol has different \n"); |
---|
157 | PRINTF(1)("versions or there occured an error in the sending algorithm\n"); |
---|
158 | PRINTF(1)("Data is not in the right format! i=%d\n", i); |
---|
159 | return i; |
---|
160 | } |
---|
161 | |
---|
162 | return i; |
---|
163 | } |
---|
164 | |
---|
165 | int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever) |
---|
166 | { |
---|
167 | if ( !isServer() && !hasRequestedWorld ) |
---|
168 | { |
---|
169 | assert( maxLength >= 1 ); |
---|
170 | data[0] = NET_REQUEST_ENTITY_LIST; |
---|
171 | hasRequestedWorld = true; |
---|
172 | return 1; |
---|
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 | bool NetworkGameManager::signalLeftPlayer(int userID) |
---|
466 | { |
---|
467 | const std::list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE); |
---|
468 | std::list<BaseObject*>::const_iterator it = playableList->begin(); |
---|
469 | |
---|
470 | for(; it != playableList->end(); it++) |
---|
471 | { |
---|
472 | if( dynamic_cast<Synchronizeable*>(*it)->getOwner() == userID ) |
---|
473 | { |
---|
474 | PRINTF(0)("remove playable from %i\n", userID); |
---|
475 | this->removeEntity(dynamic_cast<Synchronizeable*>(*it)->getUniqueID()); |
---|
476 | return true; |
---|
477 | } |
---|
478 | } |
---|
479 | return false; |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | /** |
---|
484 | * Creates a buffer for user n |
---|
485 | * @param n The ID of the user |
---|
486 | */ |
---|
487 | void NetworkGameManager::resizeBufferVector( int n ) |
---|
488 | { |
---|
489 | for ( int i = outBuffer.size(); i<=n; i++) |
---|
490 | { |
---|
491 | clientBuffer outBuf; |
---|
492 | |
---|
493 | outBuf.length = 0; |
---|
494 | |
---|
495 | outBuf.maxLength = 5*1024; |
---|
496 | |
---|
497 | outBuf.buffer = new byte[5*1014]; |
---|
498 | |
---|
499 | outBuffer.push_back(outBuf); |
---|
500 | } |
---|
501 | } |
---|
502 | |
---|
503 | /** |
---|
504 | * Creates the entity on this host |
---|
505 | * @param classID: ClassID of the entity to create |
---|
506 | * @param uniqueID: Unique ID to assign to the synchronizeable |
---|
507 | * @param owner: owner of this synchronizealbe |
---|
508 | */ |
---|
509 | BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner ) |
---|
510 | { |
---|
511 | PRINTF(0)("Creating Entity via Factory: classid: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner); |
---|
512 | |
---|
513 | BaseObject * b; |
---|
514 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
---|
515 | /* Exception 1: NullParent */ |
---|
516 | if( classID == CL_NULL_PARENT) |
---|
517 | { |
---|
518 | b = (BaseObject*)PNode::getNullParent(); |
---|
519 | } |
---|
520 | else |
---|
521 | b = Factory::fabricate( classID ); |
---|
522 | |
---|
523 | if ( !b ) |
---|
524 | { |
---|
525 | PRINTF(1)("Could not fabricate Object with classID %x\n", classID); |
---|
526 | return NULL; |
---|
527 | } |
---|
528 | |
---|
529 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
530 | { |
---|
531 | Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); |
---|
532 | s->setUniqueID( uniqueID ); |
---|
533 | s->setOwner( owner ); |
---|
534 | s->setSynchronized(true); |
---|
535 | //this->networkStream->connectSynchronizeable( *s ); |
---|
536 | if ( !isServer() ) |
---|
537 | s->setIsOutOfSync( true ); |
---|
538 | PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID()); |
---|
539 | |
---|
540 | //HACK: hack to prevent collision |
---|
541 | if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) ) |
---|
542 | { |
---|
543 | if ( NetworkManager::getInstance()->getHostID()!=0 ) |
---|
544 | { |
---|
545 | static Vector pos = Vector(1000.0, 1000.0, 1000.0); |
---|
546 | PNode *p = dynamic_cast<PNode*>(b); |
---|
547 | p->setAbsCoor(pos); |
---|
548 | p->updateNode(0); |
---|
549 | pos += Vector(1000.0, 1000.0, 1000.0); |
---|
550 | } |
---|
551 | } |
---|
552 | |
---|
553 | return b; |
---|
554 | } |
---|
555 | else |
---|
556 | { |
---|
557 | PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID); |
---|
558 | delete b; |
---|
559 | } |
---|
560 | return NULL; |
---|
561 | } |
---|
562 | |
---|
563 | /** |
---|
564 | * Removes a entity on this host |
---|
565 | * @param uniqueID: unique ID assigned with the entity to remove |
---|
566 | */ |
---|
567 | void NetworkGameManager::doRemoveEntity( int uniqueID ) |
---|
568 | { |
---|
569 | SynchronizeableList::const_iterator it,e; |
---|
570 | it = this->networkStream->getSyncBegin(); |
---|
571 | e = this->networkStream->getSyncEnd(); |
---|
572 | |
---|
573 | while ( it != e ) |
---|
574 | { |
---|
575 | if ( (*it)->getUniqueID() == uniqueID ) |
---|
576 | { |
---|
577 | delete *it; |
---|
578 | break; |
---|
579 | } |
---|
580 | it++; |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | /** |
---|
585 | * Tell the synchronizeable that a user's synchronizeable is out of sync |
---|
586 | * @param uniqueID: unique ID assigned with the entity which is out of sync |
---|
587 | * @param userID: user ID who's synchronizeable is out of sync |
---|
588 | */ |
---|
589 | void NetworkGameManager::doRequestSync( int uniqueID, int userID ) |
---|
590 | { |
---|
591 | SynchronizeableList::const_iterator it,e; |
---|
592 | it = this->networkStream->getSyncBegin(); |
---|
593 | e = this->networkStream->getSyncEnd(); |
---|
594 | |
---|
595 | while ( it != e ) |
---|
596 | { |
---|
597 | if ( (*it)->getUniqueID() == uniqueID ) |
---|
598 | { |
---|
599 | (*it)->requestSync( userID ); |
---|
600 | break; |
---|
601 | } |
---|
602 | it++; |
---|
603 | } |
---|
604 | } |
---|
605 | |
---|
606 | /** |
---|
607 | * Copies length bytes to the clientBuffer with error checking |
---|
608 | * @param clientBuffer: the clientBuffer to write to |
---|
609 | * @param data: buffer to the data |
---|
610 | * @param length: length of data |
---|
611 | * @return false on error true else |
---|
612 | */ |
---|
613 | bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length ) |
---|
614 | { |
---|
615 | if ( length > cb.maxLength-cb.length ) |
---|
616 | { |
---|
617 | PRINTF(1)("No space left in clientBuffer\n"); |
---|
618 | return false; |
---|
619 | } |
---|
620 | |
---|
621 | memcpy( cb.buffer+cb.length, data, length ); |
---|
622 | return true; |
---|
623 | } |
---|
624 | |
---|
625 | /** |
---|
626 | * Reads data from clientBuffer with error checking |
---|
627 | * @param clientBuffer: the clientBuffer to read from |
---|
628 | * @param data: pointer to the buffer |
---|
629 | * @param length: |
---|
630 | * @return |
---|
631 | */ |
---|
632 | bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length ) |
---|
633 | { |
---|
634 | if ( cb.length < length ) |
---|
635 | { |
---|
636 | PRINTF(0)("There is not enough data in clientBuffer\n"); |
---|
637 | return 0; |
---|
638 | } |
---|
639 | |
---|
640 | memcpy( data, cb.buffer+cb.length-length, length ); |
---|
641 | return true; |
---|
642 | } |
---|
643 | |
---|
644 | /** |
---|
645 | * Tells this client that he has to control this entity |
---|
646 | * @param uniqueID: the entity's uniqeID |
---|
647 | */ |
---|
648 | void NetworkGameManager::doYouAre( int uniqueID ) |
---|
649 | { |
---|
650 | |
---|
651 | SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin(); |
---|
652 | |
---|
653 | Playable *p = NULL; |
---|
654 | Synchronizeable *s = NULL; |
---|
655 | |
---|
656 | for ( ; it !=networkStream->getSyncEnd(); it++ ) |
---|
657 | { |
---|
658 | if ( (*it)->getUniqueID()==uniqueID ) |
---|
659 | { |
---|
660 | if ( (*it)->isA( CL_SYNCHRONIZEABLE ) ) |
---|
661 | { |
---|
662 | s = dynamic_cast<Synchronizeable*>(*it); |
---|
663 | } |
---|
664 | if ( (*it)->isA( CL_PLAYABLE ) ) |
---|
665 | { |
---|
666 | p = dynamic_cast<Playable*>(*it); |
---|
667 | break; |
---|
668 | } else |
---|
669 | { |
---|
670 | PRINTF(1)("UniqueID is not a Playable\n"); |
---|
671 | } |
---|
672 | } |
---|
673 | } |
---|
674 | |
---|
675 | Player* player = State::getPlayer(); |
---|
676 | assert(p != NULL); |
---|
677 | assert(s != NULL); |
---|
678 | assert(player != NULL); |
---|
679 | |
---|
680 | s->setIsOutOfSync( true ); |
---|
681 | |
---|
682 | PRINTF(0)("uniqueID = %d\n", s->getUniqueID()); |
---|
683 | |
---|
684 | player->setControllable(p); |
---|
685 | |
---|
686 | |
---|
687 | } |
---|
688 | |
---|
689 | /** |
---|
690 | * Tells a remote client that he has to control this entity |
---|
691 | * @param uniqueID: the entity's uniqeID |
---|
692 | * @param userID: the users ID |
---|
693 | */ |
---|
694 | void NetworkGameManager::sendYouAre( int uniqueID, int userID ) |
---|
695 | { |
---|
696 | if ( !isServer() ) |
---|
697 | return; |
---|
698 | |
---|
699 | if ( userID != 0 ) |
---|
700 | { |
---|
701 | if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_YOU_ARE_ENTITY ) ) |
---|
702 | return; |
---|
703 | |
---|
704 | if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) ) |
---|
705 | return; |
---|
706 | } |
---|
707 | else |
---|
708 | { |
---|
709 | doYouAre(uniqueID); |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender ) |
---|
714 | { |
---|
715 | if ( INTSIZE > length-i ) |
---|
716 | { |
---|
717 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
718 | return false; |
---|
719 | } |
---|
720 | int classID; |
---|
721 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
722 | |
---|
723 | createEntity( (ClassID)classID ); |
---|
724 | |
---|
725 | return true; |
---|
726 | } |
---|
727 | |
---|
728 | bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender ) |
---|
729 | { |
---|
730 | if ( INTSIZE > length-i ) |
---|
731 | { |
---|
732 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
733 | return false; |
---|
734 | } |
---|
735 | int uniqueID; |
---|
736 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
737 | |
---|
738 | removeEntity( uniqueID ); |
---|
739 | |
---|
740 | return true; |
---|
741 | } |
---|
742 | |
---|
743 | bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender ) |
---|
744 | { |
---|
745 | if ( INTSIZE > length-i ) |
---|
746 | { |
---|
747 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
748 | return false; |
---|
749 | } |
---|
750 | int classID; |
---|
751 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
752 | |
---|
753 | if ( INTSIZE > length-i ) |
---|
754 | { |
---|
755 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
756 | return false; |
---|
757 | } |
---|
758 | int uniqueID; |
---|
759 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
760 | |
---|
761 | if ( INTSIZE > length-i ) |
---|
762 | { |
---|
763 | PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); |
---|
764 | return false; |
---|
765 | } |
---|
766 | int owner; |
---|
767 | i += Converter::byteArrayToInt( &data[i], &owner ); |
---|
768 | |
---|
769 | PRINTF(0)("handleCreateEntity: client side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner); |
---|
770 | doCreateEntity( (ClassID)classID, uniqueID, owner ); |
---|
771 | |
---|
772 | return true; |
---|
773 | } |
---|
774 | |
---|
775 | bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender ) |
---|
776 | { |
---|
777 | if ( INTSIZE > length-i ) |
---|
778 | { |
---|
779 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
780 | return false; |
---|
781 | } |
---|
782 | int uniqueID; |
---|
783 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
784 | |
---|
785 | doRemoveEntity( uniqueID ); |
---|
786 | |
---|
787 | return true; |
---|
788 | } |
---|
789 | |
---|
790 | bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender ) |
---|
791 | { |
---|
792 | if ( INTSIZE > length-i ) |
---|
793 | { |
---|
794 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
795 | return false; |
---|
796 | } |
---|
797 | |
---|
798 | PRINTF(0)("HandleCreateEntityList: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]); |
---|
799 | |
---|
800 | int n; |
---|
801 | i += Converter::byteArrayToInt( &data[i], &n ); |
---|
802 | |
---|
803 | |
---|
804 | PRINTF(0)("HandleCreateEntityList: n = %d\n", n); |
---|
805 | |
---|
806 | int classID, uniqueID, owner; |
---|
807 | |
---|
808 | for ( int j = 0; j<n; j++ ) |
---|
809 | { |
---|
810 | |
---|
811 | if ( INTSIZE > length-i ) |
---|
812 | { |
---|
813 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
814 | return false; |
---|
815 | } |
---|
816 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
817 | |
---|
818 | if ( INTSIZE > length-i ) |
---|
819 | { |
---|
820 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
821 | return false; |
---|
822 | } |
---|
823 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
824 | |
---|
825 | if ( INTSIZE > length-i ) |
---|
826 | { |
---|
827 | PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); |
---|
828 | return false; |
---|
829 | } |
---|
830 | i += Converter::byteArrayToInt( &data[i], &owner ); |
---|
831 | |
---|
832 | if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE ) |
---|
833 | { |
---|
834 | BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner ); |
---|
835 | } |
---|
836 | |
---|
837 | } |
---|
838 | |
---|
839 | return true; |
---|
840 | } |
---|
841 | |
---|
842 | bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender ) |
---|
843 | { |
---|
844 | if ( INTSIZE > length-i ) |
---|
845 | { |
---|
846 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
847 | return false; |
---|
848 | } |
---|
849 | int n; |
---|
850 | i += Converter::byteArrayToInt( &data[i], &n ); |
---|
851 | |
---|
852 | int uniqueID; |
---|
853 | |
---|
854 | for ( int j = 0; j<n; j++ ) |
---|
855 | { |
---|
856 | |
---|
857 | if ( INTSIZE > length-i ) |
---|
858 | { |
---|
859 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
860 | return false; |
---|
861 | } |
---|
862 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
863 | |
---|
864 | doRemoveEntity( uniqueID ); |
---|
865 | } |
---|
866 | |
---|
867 | return true; |
---|
868 | } |
---|
869 | |
---|
870 | bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender ) |
---|
871 | { |
---|
872 | if ( INTSIZE > length-i ) |
---|
873 | { |
---|
874 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
875 | return false; |
---|
876 | } |
---|
877 | |
---|
878 | int uniqueID; |
---|
879 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
880 | |
---|
881 | doYouAre( uniqueID ); |
---|
882 | |
---|
883 | return true; |
---|
884 | } |
---|
885 | |
---|
886 | bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender ) |
---|
887 | { |
---|
888 | if ( INTSIZE > length-i ) |
---|
889 | { |
---|
890 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
891 | return false; |
---|
892 | } |
---|
893 | int uniqueID; |
---|
894 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
895 | |
---|
896 | doRequestSync( uniqueID, sender ); |
---|
897 | |
---|
898 | return true; |
---|
899 | } |
---|
900 | |
---|
901 | |
---|
902 | /** |
---|
903 | * handles the network signal NET_REQUEST_PNODE_PATH |
---|
904 | * @param i byte offset in the buffer |
---|
905 | * @param data data array |
---|
906 | * @param length length of the data arary |
---|
907 | * @param sender the sender id |
---|
908 | * @return true if process terminated sucessfully |
---|
909 | */ |
---|
910 | bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender) |
---|
911 | { |
---|
912 | if( INTSIZE > length-i ) |
---|
913 | { |
---|
914 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
915 | return false; |
---|
916 | } |
---|
917 | PRINTF(0)("HandleRequestPNodePath: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]); |
---|
918 | |
---|
919 | int uniqueID1, uniqueID2; |
---|
920 | if( INTSIZE > length-i ) |
---|
921 | { |
---|
922 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
923 | return false; |
---|
924 | } |
---|
925 | i += Converter::byteArrayToInt( &data[i], &uniqueID1 ); |
---|
926 | |
---|
927 | if( INTSIZE > length-i ) |
---|
928 | { |
---|
929 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
930 | return false; |
---|
931 | } |
---|
932 | i += Converter::byteArrayToInt( &data[i], &uniqueID2 ); |
---|
933 | |
---|
934 | |
---|
935 | PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2); |
---|
936 | |
---|
937 | return true; |
---|
938 | } |
---|
939 | |
---|
940 | |
---|
941 | bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b ) |
---|
942 | { |
---|
943 | if ( cb.maxLength-cb.length < 1 ) |
---|
944 | { |
---|
945 | PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n"); |
---|
946 | return false; |
---|
947 | } |
---|
948 | |
---|
949 | cb.buffer[cb.length++] = b; |
---|
950 | |
---|
951 | return true; |
---|
952 | } |
---|
953 | |
---|
954 | bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i ) |
---|
955 | { |
---|
956 | int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length ); |
---|
957 | cb.length += n; |
---|
958 | |
---|
959 | if ( n <= 0 ) |
---|
960 | { |
---|
961 | PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n"); |
---|
962 | return false; |
---|
963 | } |
---|
964 | |
---|
965 | return true; |
---|
966 | } |
---|
967 | |
---|
968 | void NetworkGameManager::sync( int uniqueID, int owner ) |
---|
969 | { |
---|
970 | /*if ( owner==this->getHostID() ) |
---|
971 | return;*/ |
---|
972 | |
---|
973 | if ( !isServer() ) |
---|
974 | executeRequestSync( uniqueID, 0 ); |
---|
975 | else |
---|
976 | executeRequestSync( uniqueID, owner ); |
---|
977 | } |
---|
978 | |
---|
979 | void NetworkGameManager::executeRequestSync( int uniqueID, int user ) |
---|
980 | { |
---|
981 | if ( user >= outBuffer.size() ) |
---|
982 | resizeBufferVector( user ); |
---|
983 | |
---|
984 | if ( !writeToClientBuffer( outBuffer[user], (byte)NET_REQUEST_SYNC ) ) |
---|
985 | return; |
---|
986 | if ( !writeToClientBuffer( outBuffer[user], uniqueID ) ) |
---|
987 | return; |
---|
988 | } |
---|
989 | |
---|