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 | { |
---|
49 | PRINTF(0)("START\n"); |
---|
50 | |
---|
51 | /* set the class id for the base object */ |
---|
52 | this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); |
---|
53 | |
---|
54 | allOutBuffer.length = 0; |
---|
55 | |
---|
56 | allOutBuffer.maxLength = 10*1024; |
---|
57 | |
---|
58 | allOutBuffer.buffer = new byte[10*1024]; |
---|
59 | |
---|
60 | newUniqueID = MAX_CONNECTIONS + 2; |
---|
61 | |
---|
62 | hasRequestedWorld = false; |
---|
63 | } |
---|
64 | |
---|
65 | /*! |
---|
66 | * Standard destructor |
---|
67 | */ |
---|
68 | NetworkGameManager::~NetworkGameManager() |
---|
69 | { |
---|
70 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
71 | { |
---|
72 | if ( outBuffer[i].buffer ) |
---|
73 | delete outBuffer[i].buffer; |
---|
74 | } |
---|
75 | |
---|
76 | if ( allOutBuffer.buffer ) |
---|
77 | delete allOutBuffer.buffer; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | int NetworkGameManager::writeBytes(const byte* data, int length, int sender) |
---|
82 | { |
---|
83 | int i = 0; |
---|
84 | byte b; |
---|
85 | |
---|
86 | while ( i<length ) |
---|
87 | { |
---|
88 | b = data[i++]; |
---|
89 | |
---|
90 | if ( isServer() ) |
---|
91 | { |
---|
92 | if ( b == REQUEST_CREATE ) |
---|
93 | { |
---|
94 | if ( !handleRequestCreate( i, data, length, sender ) ) |
---|
95 | return i; |
---|
96 | continue; |
---|
97 | } |
---|
98 | if ( b == REQUEST_REMOVE ) |
---|
99 | { |
---|
100 | if ( !handleRequestRemove( i, data, length, sender ) ) |
---|
101 | return i; |
---|
102 | continue; |
---|
103 | } |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | if ( b == CREATE_ENTITY ) |
---|
108 | { |
---|
109 | if ( !handleCreateEntity( i, data, length, sender ) ) |
---|
110 | return i; |
---|
111 | continue; |
---|
112 | } |
---|
113 | if ( b == REMOVE_ENTITY ) |
---|
114 | { |
---|
115 | if ( !handleRemoveEntity( i, data, length, sender ) ) |
---|
116 | return i; |
---|
117 | continue; |
---|
118 | } |
---|
119 | if ( b == CREATE_ENTITY_LIST ) |
---|
120 | { |
---|
121 | if ( !handleCreateEntityList( i, data, length, sender ) ) |
---|
122 | return i; |
---|
123 | continue; |
---|
124 | } |
---|
125 | if ( b == REMOVE_ENTITY_LIST ) |
---|
126 | { |
---|
127 | if ( !handleRemoveEntityList( i, data, length, sender ) ) |
---|
128 | return i; |
---|
129 | continue; |
---|
130 | } |
---|
131 | if ( b == YOU_ARE_ENTITY ) |
---|
132 | { |
---|
133 | if ( !handleYouAreEntity( i, data, length, sender ) ) |
---|
134 | return i; |
---|
135 | continue; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | if ( b == REQUEST_SYNC ) |
---|
140 | { |
---|
141 | if ( !handleRequestSync( i, data, length, sender ) ) |
---|
142 | return i; |
---|
143 | continue; |
---|
144 | } |
---|
145 | |
---|
146 | if ( b == REQUEST_ENTITY_LIST ) |
---|
147 | { |
---|
148 | sendEntityList( sender ); |
---|
149 | continue; |
---|
150 | } |
---|
151 | |
---|
152 | //if we get her something with data is wrong |
---|
153 | PRINTF(1)("Data is not in the right format! i=%d\n", i); |
---|
154 | return i; |
---|
155 | } |
---|
156 | |
---|
157 | return i; |
---|
158 | } |
---|
159 | |
---|
160 | int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever) |
---|
161 | { |
---|
162 | if ( !isServer() && !hasRequestedWorld ) |
---|
163 | { |
---|
164 | SYNCHELP_WRITE_BEGIN(); |
---|
165 | byte b = REQUEST_ENTITY_LIST; |
---|
166 | SYNCHELP_WRITE_BYTE( b ); |
---|
167 | hasRequestedWorld = true; |
---|
168 | return SYNCHELP_WRITE_N; |
---|
169 | } |
---|
170 | |
---|
171 | for ( int i = 0; i<outBuffer.size(); i++ ) |
---|
172 | { |
---|
173 | *reciever = i; |
---|
174 | if ( outBuffer[i].length>0 ) |
---|
175 | { |
---|
176 | int nbytes = outBuffer[i].length; |
---|
177 | outBuffer[i].length = 0; |
---|
178 | |
---|
179 | if ( nbytes > maxLength ) |
---|
180 | { |
---|
181 | PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength); |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | memcpy(data, outBuffer[i].buffer, nbytes); |
---|
186 | return nbytes; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | *reciever = 0; |
---|
191 | int nbytes = allOutBuffer.length; |
---|
192 | allOutBuffer.length = 0; |
---|
193 | |
---|
194 | if ( nbytes <=0 ) |
---|
195 | return 0; |
---|
196 | |
---|
197 | if ( nbytes > maxLength ) |
---|
198 | { |
---|
199 | PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.length\n", nbytes, maxLength); |
---|
200 | return 0; |
---|
201 | } |
---|
202 | |
---|
203 | memcpy( data, allOutBuffer.buffer, nbytes ); |
---|
204 | return nbytes; |
---|
205 | } |
---|
206 | |
---|
207 | void NetworkGameManager::writeDebug() const |
---|
208 | { |
---|
209 | } |
---|
210 | |
---|
211 | void NetworkGameManager::readDebug() const |
---|
212 | { |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | /*! |
---|
217 | * Checks whether this is connected to a server or a client |
---|
218 | * and afterwards creates the needed entity |
---|
219 | * @param classID: The ID of the class of which an entity should be created |
---|
220 | */ |
---|
221 | int NetworkGameManager::createEntity( ClassID classID, int owner ) |
---|
222 | { |
---|
223 | |
---|
224 | if ( this->isServer() ) |
---|
225 | { |
---|
226 | if ( newUniqueID < 0 ) |
---|
227 | { |
---|
228 | PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); |
---|
229 | return -1; |
---|
230 | } |
---|
231 | |
---|
232 | return this->executeCreateEntity( classID, newUniqueID++, owner ); |
---|
233 | } |
---|
234 | else |
---|
235 | { |
---|
236 | this->requestCreateEntity( classID ); |
---|
237 | return -1; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | /*! |
---|
243 | * Checks whether this is connected to a server or a client |
---|
244 | * and afterwards creates the needed entity |
---|
245 | * @param classID: The ID of the class of which an entity should be created |
---|
246 | */ |
---|
247 | BaseObject* NetworkGameManager::createEntity(const TiXmlElement* element) |
---|
248 | { |
---|
249 | if ( this->isServer() ) |
---|
250 | { |
---|
251 | if ( newUniqueID < 0 ) |
---|
252 | { |
---|
253 | PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); |
---|
254 | return NULL; |
---|
255 | } |
---|
256 | newUniqueID++; |
---|
257 | |
---|
258 | BaseObject * b = Factory::fabricate( element ); |
---|
259 | |
---|
260 | if ( !b ) |
---|
261 | { |
---|
262 | PRINTF(1)("Could not fabricate Object with className %s\n", element->Value() ); |
---|
263 | return NULL; |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
268 | { |
---|
269 | Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); |
---|
270 | s->setUniqueID( newUniqueID ); |
---|
271 | s->setOwner( 0 ); |
---|
272 | this->networkStream->connectSynchronizeable( *s ); |
---|
273 | return b; |
---|
274 | } |
---|
275 | else |
---|
276 | { |
---|
277 | PRINTF(1)("Class %s is not a synchronizeable!\n", b->getClassName() ); |
---|
278 | delete b; |
---|
279 | } |
---|
280 | |
---|
281 | } |
---|
282 | else |
---|
283 | { |
---|
284 | PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value()); |
---|
285 | } |
---|
286 | return NULL; |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | /*! |
---|
291 | * Checks whether this is connected to a server or a client |
---|
292 | * and afterwards removes the specified entity |
---|
293 | * @param uniqueID: The ID of the entity object which should be removed |
---|
294 | */ |
---|
295 | void NetworkGameManager::removeEntity(int uniqueID) |
---|
296 | { |
---|
297 | if ( this->isServer() ) |
---|
298 | { |
---|
299 | this->executeRemoveEntity( uniqueID ); |
---|
300 | } |
---|
301 | else |
---|
302 | { |
---|
303 | this->requestRemoveEntity( uniqueID ); |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | /*! |
---|
310 | * Creates the needed entity on the server if possible |
---|
311 | * @param classID: The ID of the class of which an entity should be created |
---|
312 | */ |
---|
313 | void NetworkGameManager::requestCreateEntity(ClassID classID) |
---|
314 | { |
---|
315 | if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_CREATE ) ) |
---|
316 | return; |
---|
317 | if ( !writeToClientBuffer( allOutBuffer, (int)classID ) ) |
---|
318 | return; |
---|
319 | } |
---|
320 | |
---|
321 | /*! |
---|
322 | * Removes the specified entity on the server |
---|
323 | * @param uniqueID: The ID of the entity object which should be removed |
---|
324 | */ |
---|
325 | void NetworkGameManager::requestRemoveEntity(int uniqueID) |
---|
326 | { |
---|
327 | if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_REMOVE ) ) |
---|
328 | return; |
---|
329 | if ( !writeToClientBuffer( allOutBuffer, uniqueID ) ) |
---|
330 | return; |
---|
331 | } |
---|
332 | |
---|
333 | /*! |
---|
334 | * Creates the needed entity if possible |
---|
335 | * This function is called if this is a server |
---|
336 | * @param classID: The ID of the class of which an entity should be created |
---|
337 | */ |
---|
338 | int NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner) |
---|
339 | { |
---|
340 | if ( !writeToClientBuffer( allOutBuffer, (byte)CREATE_ENTITY ) ) |
---|
341 | return -1; |
---|
342 | if ( !writeToClientBuffer( allOutBuffer, (int)classID ) ) |
---|
343 | return -1; |
---|
344 | if ( !writeToClientBuffer( allOutBuffer, uniqueID ) ) |
---|
345 | return -1; |
---|
346 | if ( !writeToClientBuffer( allOutBuffer, owner ) ) |
---|
347 | return -1; |
---|
348 | |
---|
349 | doCreateEntity( classID, uniqueID, owner ); |
---|
350 | |
---|
351 | return uniqueID; |
---|
352 | } |
---|
353 | |
---|
354 | /*! |
---|
355 | * Removes the specified entity |
---|
356 | * This function is called if this is a server |
---|
357 | * @param uniqueID: The ID of the entity object which should be removed |
---|
358 | */ |
---|
359 | void NetworkGameManager::executeRemoveEntity(int uniqueID) |
---|
360 | { |
---|
361 | if ( !writeToClientBuffer( allOutBuffer, (byte)REMOVE_ENTITY ) ) |
---|
362 | return; |
---|
363 | if ( !writeToClientBuffer( allOutBuffer, uniqueID ) ) |
---|
364 | return; |
---|
365 | |
---|
366 | doRemoveEntity(uniqueID); |
---|
367 | } |
---|
368 | |
---|
369 | /*! |
---|
370 | * Checks whether it is possible to create an entity of a given class |
---|
371 | * @return: true if the entity can be created, false otherwise |
---|
372 | */ |
---|
373 | bool NetworkGameManager::canCreateEntity(ClassID classID) |
---|
374 | { |
---|
375 | return true; |
---|
376 | } |
---|
377 | |
---|
378 | /*! |
---|
379 | * Sends the Entities to the new connected client |
---|
380 | * @param userID: The ID of the user |
---|
381 | */ |
---|
382 | void NetworkGameManager::sendEntityList( int userID ) |
---|
383 | { |
---|
384 | if ( !isServer() ) |
---|
385 | return; |
---|
386 | |
---|
387 | if ( userID >= outBuffer.size() ) |
---|
388 | resizeBufferVector( userID ); |
---|
389 | |
---|
390 | SynchronizeableList::const_iterator it, e; |
---|
391 | |
---|
392 | it = this->networkStream->getSyncBegin(); |
---|
393 | e = this->networkStream->getSyncEnd(); |
---|
394 | |
---|
395 | if ( !writeToClientBuffer( outBuffer[userID], (byte)CREATE_ENTITY_LIST ) ) |
---|
396 | return; |
---|
397 | |
---|
398 | // -2 because you must not send network_game_manager and handshake |
---|
399 | if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) ) |
---|
400 | return; |
---|
401 | |
---|
402 | //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 ); |
---|
403 | |
---|
404 | int n = 0; |
---|
405 | |
---|
406 | while ( it != e ) |
---|
407 | { |
---|
408 | |
---|
409 | if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) ) |
---|
410 | return; |
---|
411 | |
---|
412 | if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) ) |
---|
413 | return; |
---|
414 | |
---|
415 | if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) ) |
---|
416 | return; |
---|
417 | |
---|
418 | it++; |
---|
419 | } |
---|
420 | |
---|
421 | signalNewPlayer( userID ); |
---|
422 | } |
---|
423 | |
---|
424 | |
---|
425 | |
---|
426 | bool NetworkGameManager::signalNewPlayer(int userId) |
---|
427 | { |
---|
428 | |
---|
429 | /* create new playable for Player*/ |
---|
430 | PRINTF(0)("Request for creation: %i\n", userId); |
---|
431 | int uniqueId = this->createEntity(CL_SPACE_SHIP, userId); |
---|
432 | PRINTF(0)("Request for creation: userid: %i, uniqueid: %i\n", userId, uniqueId); |
---|
433 | this->sendYouAre(uniqueId, userId); |
---|
434 | |
---|
435 | } |
---|
436 | |
---|
437 | |
---|
438 | /** |
---|
439 | * Creates a buffer for user n |
---|
440 | * @param n The ID of the user |
---|
441 | */ |
---|
442 | void NetworkGameManager::resizeBufferVector( int n ) |
---|
443 | { |
---|
444 | for ( int i = outBuffer.size(); i<=n; i++) |
---|
445 | { |
---|
446 | clientBuffer outBuf; |
---|
447 | |
---|
448 | outBuf.length = 0; |
---|
449 | |
---|
450 | outBuf.maxLength = 5*1024; |
---|
451 | |
---|
452 | outBuf.buffer = new byte[5*1014]; |
---|
453 | |
---|
454 | outBuffer.push_back(outBuf); |
---|
455 | } |
---|
456 | } |
---|
457 | |
---|
458 | /** |
---|
459 | * Creates the entity on this host |
---|
460 | * @param classID: ClassID of the entity to create |
---|
461 | * @param uniqueID: Unique ID to assign to the synchronizeable |
---|
462 | * @param owner: owner of this synchronizealbe |
---|
463 | */ |
---|
464 | BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner ) |
---|
465 | { |
---|
466 | BaseObject * b = Factory::fabricate( classID ); |
---|
467 | |
---|
468 | if ( !b ) |
---|
469 | { |
---|
470 | PRINTF(1)("Could not fabricate Object with classID %x\n", classID); |
---|
471 | return NULL; |
---|
472 | } |
---|
473 | |
---|
474 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
475 | { |
---|
476 | Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); |
---|
477 | s->setUniqueID( uniqueID ); |
---|
478 | s->setOwner( owner ); |
---|
479 | this->networkStream->connectSynchronizeable( *s ); |
---|
480 | if ( !isServer() ) |
---|
481 | s->setIsOutOfSync( true ); |
---|
482 | PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID()); |
---|
483 | |
---|
484 | //HACK: hack to prevent collision |
---|
485 | if ( b->isA(CL_WORLD_ENTITY) ) |
---|
486 | { |
---|
487 | if ( NetworkManager::getInstance()->getHostID()!=0 ) |
---|
488 | { |
---|
489 | static Vector pos = Vector(1000.0, 1000.0, 1000.0); |
---|
490 | PNode *p = dynamic_cast<PNode*>(b); |
---|
491 | p->setRelCoor(pos); |
---|
492 | p->updateNode(0); |
---|
493 | pos += Vector(1000.0, 1000.0, 1000.0); |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | return b; |
---|
498 | } |
---|
499 | else |
---|
500 | { |
---|
501 | PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID); |
---|
502 | delete b; |
---|
503 | } |
---|
504 | return NULL; |
---|
505 | } |
---|
506 | |
---|
507 | /** |
---|
508 | * Removes a entity on this host |
---|
509 | * @param uniqueID: unique ID assigned with the entity to remove |
---|
510 | */ |
---|
511 | void NetworkGameManager::doRemoveEntity( int uniqueID ) |
---|
512 | { |
---|
513 | SynchronizeableList::const_iterator it,e; |
---|
514 | it = this->networkStream->getSyncBegin(); |
---|
515 | e = this->networkStream->getSyncEnd(); |
---|
516 | |
---|
517 | while ( it != e ) |
---|
518 | { |
---|
519 | if ( (*it)->getUniqueID() == uniqueID ) |
---|
520 | { |
---|
521 | delete *it; |
---|
522 | break; |
---|
523 | } |
---|
524 | it++; |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | /** |
---|
529 | * Tell the synchronizeable that a user's synchronizeable is out of sync |
---|
530 | * @param uniqueID: unique ID assigned with the entity which is out of sync |
---|
531 | * @param userID: user ID who's synchronizeable is out of sync |
---|
532 | */ |
---|
533 | void NetworkGameManager::doRequestSync( int uniqueID, int userID ) |
---|
534 | { |
---|
535 | SynchronizeableList::const_iterator it,e; |
---|
536 | it = this->networkStream->getSyncBegin(); |
---|
537 | e = this->networkStream->getSyncEnd(); |
---|
538 | |
---|
539 | while ( it != e ) |
---|
540 | { |
---|
541 | if ( (*it)->getUniqueID() == uniqueID ) |
---|
542 | { |
---|
543 | (*it)->requestSync( userID ); |
---|
544 | break; |
---|
545 | } |
---|
546 | it++; |
---|
547 | } |
---|
548 | } |
---|
549 | |
---|
550 | /** |
---|
551 | * Copies length bytes to the clientBuffer with error checking |
---|
552 | * @param clientBuffer: the clientBuffer to write to |
---|
553 | * @param data: buffer to the data |
---|
554 | * @param length: length of data |
---|
555 | * @return false on error true else |
---|
556 | */ |
---|
557 | bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length ) |
---|
558 | { |
---|
559 | if ( length > cb.maxLength-cb.length ) |
---|
560 | { |
---|
561 | PRINTF(1)("No space left in clientBuffer\n"); |
---|
562 | return false; |
---|
563 | } |
---|
564 | |
---|
565 | memcpy( cb.buffer+cb.length, data, length ); |
---|
566 | return true; |
---|
567 | } |
---|
568 | |
---|
569 | /** |
---|
570 | * Reads data from clientBuffer with error checking |
---|
571 | * @param clientBuffer: the clientBuffer to read from |
---|
572 | * @param data: pointer to the buffer |
---|
573 | * @param length: |
---|
574 | * @return |
---|
575 | */ |
---|
576 | bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length ) |
---|
577 | { |
---|
578 | if ( cb.length < length ) |
---|
579 | { |
---|
580 | PRINTF(0)("There is not enough data in clientBuffer\n"); |
---|
581 | return 0; |
---|
582 | } |
---|
583 | |
---|
584 | memcpy( data, cb.buffer+cb.length-length, length ); |
---|
585 | return true; |
---|
586 | } |
---|
587 | |
---|
588 | /** |
---|
589 | * Tells this client that he has to control this entity |
---|
590 | * @param uniqueID: the entity's uniqeID |
---|
591 | */ |
---|
592 | void NetworkGameManager::doYouAre( int uniqueID ) |
---|
593 | { |
---|
594 | |
---|
595 | SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin(); |
---|
596 | |
---|
597 | Playable *p = NULL; |
---|
598 | |
---|
599 | for ( ; it !=networkStream->getSyncEnd(); it++ ) |
---|
600 | { |
---|
601 | if ( (*it)->getUniqueID()==uniqueID ) |
---|
602 | { |
---|
603 | if ( (*it)->isA( CL_PLAYABLE ) ) |
---|
604 | { |
---|
605 | p = dynamic_cast<Playable*>(*it); |
---|
606 | break; |
---|
607 | } else |
---|
608 | { |
---|
609 | PRINTF(1)("UniqueID is not a Playable\n"); |
---|
610 | } |
---|
611 | } |
---|
612 | } |
---|
613 | |
---|
614 | Player* player = State::getPlayer(); |
---|
615 | assert(p != NULL); |
---|
616 | assert(player != NULL); |
---|
617 | |
---|
618 | player->setControllable(p); |
---|
619 | |
---|
620 | |
---|
621 | } |
---|
622 | |
---|
623 | /** |
---|
624 | * Tells a remote client that he has to control this entity |
---|
625 | * @param uniqueID: the entity's uniqeID |
---|
626 | * @param userID: the users ID |
---|
627 | */ |
---|
628 | void NetworkGameManager::sendYouAre( int uniqueID, int userID ) |
---|
629 | { |
---|
630 | if ( !isServer() ) |
---|
631 | return; |
---|
632 | |
---|
633 | if ( userID != 0 ) |
---|
634 | { |
---|
635 | if ( !writeToClientBuffer( outBuffer[userID], (byte)YOU_ARE_ENTITY ) ) |
---|
636 | return; |
---|
637 | |
---|
638 | if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) ) |
---|
639 | return; |
---|
640 | } |
---|
641 | else |
---|
642 | { |
---|
643 | doYouAre(uniqueID); |
---|
644 | } |
---|
645 | } |
---|
646 | |
---|
647 | bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender ) |
---|
648 | { |
---|
649 | if ( INTSIZE > length-i ) |
---|
650 | { |
---|
651 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
652 | return false; |
---|
653 | } |
---|
654 | int classID; |
---|
655 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
656 | |
---|
657 | createEntity( (ClassID)classID ); |
---|
658 | |
---|
659 | return true; |
---|
660 | } |
---|
661 | |
---|
662 | bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender ) |
---|
663 | { |
---|
664 | if ( INTSIZE > length-i ) |
---|
665 | { |
---|
666 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
667 | return false; |
---|
668 | } |
---|
669 | int uniqueID; |
---|
670 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
671 | |
---|
672 | removeEntity( uniqueID ); |
---|
673 | |
---|
674 | return true; |
---|
675 | } |
---|
676 | |
---|
677 | bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender ) |
---|
678 | { |
---|
679 | if ( INTSIZE > length-i ) |
---|
680 | { |
---|
681 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
682 | return false; |
---|
683 | } |
---|
684 | int classID; |
---|
685 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
686 | |
---|
687 | if ( INTSIZE > length-i ) |
---|
688 | { |
---|
689 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
690 | return false; |
---|
691 | } |
---|
692 | int uniqueID; |
---|
693 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
694 | |
---|
695 | if ( INTSIZE > length-i ) |
---|
696 | { |
---|
697 | PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); |
---|
698 | return false; |
---|
699 | } |
---|
700 | int owner; |
---|
701 | i += Converter::byteArrayToInt( &data[i], &owner ); |
---|
702 | |
---|
703 | doCreateEntity( (ClassID)classID, uniqueID, owner ); |
---|
704 | |
---|
705 | return true; |
---|
706 | } |
---|
707 | |
---|
708 | bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender ) |
---|
709 | { |
---|
710 | if ( INTSIZE > length-i ) |
---|
711 | { |
---|
712 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
713 | return false; |
---|
714 | } |
---|
715 | int uniqueID; |
---|
716 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
717 | |
---|
718 | doRemoveEntity( uniqueID ); |
---|
719 | |
---|
720 | return true; |
---|
721 | } |
---|
722 | |
---|
723 | bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender ) |
---|
724 | { |
---|
725 | if ( INTSIZE > length-i ) |
---|
726 | { |
---|
727 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
728 | return false; |
---|
729 | } |
---|
730 | |
---|
731 | PRINTF(0)("HandleCreateEntityList: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]); |
---|
732 | |
---|
733 | int n; |
---|
734 | i += Converter::byteArrayToInt( &data[i], &n ); |
---|
735 | |
---|
736 | |
---|
737 | PRINTF(0)("HandleCreateEntityList: n = %d\n", n); |
---|
738 | |
---|
739 | int classID, uniqueID, owner; |
---|
740 | |
---|
741 | for ( int j = 0; j<n; j++ ) |
---|
742 | { |
---|
743 | |
---|
744 | if ( INTSIZE > length-i ) |
---|
745 | { |
---|
746 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
747 | return false; |
---|
748 | } |
---|
749 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
750 | |
---|
751 | if ( INTSIZE > length-i ) |
---|
752 | { |
---|
753 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
754 | return false; |
---|
755 | } |
---|
756 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
757 | |
---|
758 | if ( INTSIZE > length-i ) |
---|
759 | { |
---|
760 | PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); |
---|
761 | return false; |
---|
762 | } |
---|
763 | i += Converter::byteArrayToInt( &data[i], &owner ); |
---|
764 | |
---|
765 | if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE ) |
---|
766 | { |
---|
767 | BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner ); |
---|
768 | |
---|
769 | /*if ( b != NULL ) |
---|
770 | { |
---|
771 | if ( b->isA(CL_WORLD_ENTITY) ) |
---|
772 | { |
---|
773 | int n = dynamic_cast<WorldEntity*>(b)->writeState( data, length, sender ); |
---|
774 | |
---|
775 | i += n; |
---|
776 | } |
---|
777 | }*/ |
---|
778 | } |
---|
779 | |
---|
780 | } |
---|
781 | |
---|
782 | return true; |
---|
783 | } |
---|
784 | |
---|
785 | bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender ) |
---|
786 | { |
---|
787 | if ( INTSIZE > length-i ) |
---|
788 | { |
---|
789 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
790 | return false; |
---|
791 | } |
---|
792 | int n; |
---|
793 | i += Converter::byteArrayToInt( &data[i], &n ); |
---|
794 | |
---|
795 | int uniqueID; |
---|
796 | |
---|
797 | for ( int j = 0; j<n; j++ ) |
---|
798 | { |
---|
799 | |
---|
800 | if ( INTSIZE > length-i ) |
---|
801 | { |
---|
802 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
803 | return false; |
---|
804 | } |
---|
805 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
806 | |
---|
807 | doRemoveEntity( uniqueID ); |
---|
808 | } |
---|
809 | |
---|
810 | return true; |
---|
811 | } |
---|
812 | |
---|
813 | bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender ) |
---|
814 | { |
---|
815 | if ( INTSIZE > length-i ) |
---|
816 | { |
---|
817 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
818 | return false; |
---|
819 | } |
---|
820 | |
---|
821 | int uniqueID; |
---|
822 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
823 | |
---|
824 | doYouAre( uniqueID ); |
---|
825 | |
---|
826 | return true; |
---|
827 | } |
---|
828 | |
---|
829 | bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender ) |
---|
830 | { |
---|
831 | if ( INTSIZE > length-i ) |
---|
832 | { |
---|
833 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
834 | return false; |
---|
835 | } |
---|
836 | int uniqueID; |
---|
837 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
838 | |
---|
839 | doRequestSync( uniqueID, sender ); |
---|
840 | |
---|
841 | return true; |
---|
842 | } |
---|
843 | |
---|
844 | bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b ) |
---|
845 | { |
---|
846 | if ( cb.maxLength-cb.length < 1 ) |
---|
847 | { |
---|
848 | PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n"); |
---|
849 | return false; |
---|
850 | } |
---|
851 | |
---|
852 | cb.buffer[cb.length++] = b; |
---|
853 | |
---|
854 | return true; |
---|
855 | } |
---|
856 | |
---|
857 | bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i ) |
---|
858 | { |
---|
859 | int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length ); |
---|
860 | cb.length += n; |
---|
861 | |
---|
862 | if ( n <= 0 ) |
---|
863 | { |
---|
864 | PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n"); |
---|
865 | return false; |
---|
866 | } |
---|
867 | |
---|
868 | return true; |
---|
869 | } |
---|
870 | |
---|
871 | void NetworkGameManager::sync( int uniqueID, int owner ) |
---|
872 | { |
---|
873 | if ( owner==this->getHostID() ) |
---|
874 | return; |
---|
875 | |
---|
876 | if ( !isServer() ) |
---|
877 | executeRequestSync( uniqueID, 0 ); |
---|
878 | else |
---|
879 | executeRequestSync( uniqueID, owner ); |
---|
880 | } |
---|
881 | |
---|
882 | void NetworkGameManager::executeRequestSync( int uniqueID, int user ) |
---|
883 | { |
---|
884 | if ( user >= outBuffer.size() ) |
---|
885 | resizeBufferVector( user ); |
---|
886 | |
---|
887 | if ( !writeToClientBuffer( outBuffer[user], (byte)REQUEST_SYNC ) ) |
---|
888 | return; |
---|
889 | if ( !writeToClientBuffer( outBuffer[user], uniqueID ) ) |
---|
890 | return; |
---|
891 | } |
---|
892 | |
---|