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