Changeset 6695 in orxonox.OLD for trunk/src/lib/network/network_game_manager.cc
- Timestamp:
- Jan 25, 2006, 2:39:53 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/network_game_manager.cc
r6634 r6695 46 46 */ 47 47 NetworkGameManager::NetworkGameManager() 48 : Synchronizeable() 48 49 { 49 50 PRINTF(0)("START\n"); … … 52 53 this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); 53 54 54 newUniqueID = MAX_CONNECTIONS + 2;55 56 55 hasRequestedWorld = false; 56 this->setSynchronized(true); 57 57 } 58 58 … … 80 80 b = data[i++]; 81 81 82 /**************** Commands only processed by servers ****************/ 82 83 if ( isServer() ) 83 84 { 84 if ( b == REQUEST_CREATE )85 if ( b == NET_REQUEST_CREATE ) 85 86 { 86 87 if ( !handleRequestCreate( i, data, length, sender ) ) … … 88 89 continue; 89 90 } 90 if ( b ==REQUEST_REMOVE )91 else if ( b == NET_REQUEST_REMOVE ) 91 92 { 92 93 if ( !handleRequestRemove( i, data, length, sender ) ) … … 94 95 continue; 95 96 } 97 else if ( b == NET_REQUEST_PNODE_PATH ) 98 { 99 if ( !handleRequestPNodePath( i, data, length, sender ) ) 100 return i; 101 continue; 102 } 96 103 } 97 104 else 98 105 { 99 if ( b == CREATE_ENTITY ) 106 /**************** Commands only processed by clients ****************/ 107 if ( b == NET_CREATE_ENTITY ) 100 108 { 101 109 PRINTF(0)("CREATE_ENTITY\n"); … … 104 112 continue; 105 113 } 106 if ( b ==REMOVE_ENTITY )114 else if ( b == NET_REMOVE_ENTITY ) 107 115 { 108 116 if ( !handleRemoveEntity( i, data, length, sender ) ) … … 110 118 continue; 111 119 } 112 if ( b ==CREATE_ENTITY_LIST )120 else if ( b == NET_CREATE_ENTITY_LIST ) 113 121 { 114 122 if ( !handleCreateEntityList( i, data, length, sender ) ) … … 116 124 continue; 117 125 } 118 if ( b ==REMOVE_ENTITY_LIST )126 else if ( b == NET_REMOVE_ENTITY_LIST ) 119 127 { 120 128 if ( !handleRemoveEntityList( i, data, length, sender ) ) … … 122 130 continue; 123 131 } 124 if ( b ==YOU_ARE_ENTITY )132 else if ( b == NET_YOU_ARE_ENTITY ) 125 133 { 126 134 if ( !handleYouAreEntity( i, data, length, sender ) ) … … 130 138 } 131 139 132 if ( b == REQUEST_ENTITY_LIST ) 140 /**************** Commands processed by servers and clients ****************/ 141 if ( b == NET_REQUEST_ENTITY_LIST ) 133 142 { 134 143 sendEntityList( sender ); 135 144 continue; 136 145 } 137 138 if ( b == REQUEST_SYNC ) 146 else if ( b == NET_REQUEST_SYNC ) 139 147 { 140 148 if ( !handleRequestSync( i, data, length, sender ) ) … … 143 151 } 144 152 145 //if we get her something with data is wrong 153 154 PRINTF(1)("Network is asynchronous: couldn't decode the command sent by %i\n", sender); 155 PRINTF(1)("Probably this is because the network protocol has different \n"); 156 PRINTF(1)("versions or there occured an error in the sending algorithm\n"); 146 157 PRINTF(1)("Data is not in the right format! i=%d\n", i); 147 158 return i; … … 156 167 { 157 168 SYNCHELP_WRITE_BEGIN(); 158 byte b = REQUEST_ENTITY_LIST;169 byte b = NET_REQUEST_ENTITY_LIST; 159 170 SYNCHELP_WRITE_BYTE( b ); 160 171 hasRequestedWorld = true; … … 200 211 int NetworkGameManager::createEntity( ClassID classID, int owner ) 201 212 { 202 if ( this->isServer() 203 { 204 if ( newUniqueID< 0 )213 if ( this->isServer()) 214 { 215 if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 ) 205 216 { 206 217 PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); 207 218 return -1; 208 219 } 209 210 return this->executeCreateEntity( classID, newUniqueID++, owner ); 220 return this->executeCreateEntity( classID, SharedNetworkData::getInstance()->getNewUniqueID(), owner ); 211 221 } 212 222 else … … 227 237 if ( this->isServer() ) 228 238 { 229 if ( newUniqueID< 0 )239 if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 ) 230 240 { 231 241 PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); … … 245 255 { 246 256 Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); 247 s->setUniqueID( newUniqueID++);257 s->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); 248 258 s->setOwner( 0 ); 249 this->networkStream->connectSynchronizeable( *s ); 259 // all entities created via this function are added automaticaly to the synchronizeable list 260 s->setSynchronized(true); 250 261 return b; 251 262 } … … 258 269 } 259 270 else 271 260 272 { 261 273 PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value()); … … 295 307 continue; 296 308 297 if ( !writeToClientBuffer( outBuffer[i], (byte) REQUEST_CREATE ) )309 if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_CREATE ) ) 298 310 return; 299 311 if ( !writeToClientBuffer( outBuffer[i], (int)classID ) ) … … 313 325 continue; 314 326 315 if ( !writeToClientBuffer( outBuffer[i], (byte) REQUEST_REMOVE ) )327 if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_REMOVE ) ) 316 328 return; 317 329 if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) … … 332 344 continue; 333 345 334 if ( !writeToClientBuffer( outBuffer[i], (byte) CREATE_ENTITY ) )346 if ( !writeToClientBuffer( outBuffer[i], (byte)NET_CREATE_ENTITY ) ) 335 347 return -1; 336 348 if ( !writeToClientBuffer( outBuffer[i], (int)classID ) ) … … 342 354 } 343 355 356 PRINTF(0)("ExecuteCreateEntity: server side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner); 344 357 doCreateEntity( classID, uniqueID, owner ); 345 358 … … 359 372 continue; 360 373 361 if ( !writeToClientBuffer( outBuffer[i], (byte) REMOVE_ENTITY ) )374 if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REMOVE_ENTITY ) ) 362 375 return; 363 376 if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) … … 394 407 e = this->networkStream->getSyncEnd(); 395 408 396 if ( !writeToClientBuffer( outBuffer[userID], (byte)CREATE_ENTITY_LIST ) ) 409 // send the packet header 410 if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_CREATE_ENTITY_LIST ) ) 397 411 return; 398 412 399 // -2 because you must not send network_game_manager and handshake413 // send the number of entities: -2 because you must not send network_game_manager and handshake 400 414 if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) ) 401 415 return; … … 403 417 //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 ); 404 418 405 int n = 0; 406 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 407 428 while ( it != e ) 408 429 { 409 PRINTF(5)("SENDING ENTITY %s id %d\n", (*it)->getClassName(), (*it)->getUniqueID() ); 410 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) ) 411 return; 412 413 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) ) 414 return; 415 416 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) ) 417 return; 418 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 } 419 442 it++; 420 443 } … … 467 490 BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner ) 468 491 { 469 BaseObject * b = Factory::fabricate( classID ); 492 PRINTF(0)("Creating Entity via Factory: classid: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner); 493 494 BaseObject * b; 495 /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ 496 /* Exception 1: NullParent */ 497 if( classID == CL_NULL_PARENT) 498 { 499 b = (BaseObject*)PNode::getNullParent(); 500 } 501 else 502 b = Factory::fabricate( classID ); 470 503 471 504 if ( !b ) … … 480 513 s->setUniqueID( uniqueID ); 481 514 s->setOwner( owner ); 482 this->networkStream->connectSynchronizeable( *s ); 515 s->setSynchronized(true); 516 //this->networkStream->connectSynchronizeable( *s ); 483 517 if ( !isServer() ) 484 518 s->setIsOutOfSync( true ); … … 646 680 if ( userID != 0 ) 647 681 { 648 if ( !writeToClientBuffer( outBuffer[userID], (byte) YOU_ARE_ENTITY ) )682 if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_YOU_ARE_ENTITY ) ) 649 683 return; 650 684 … … 714 748 i += Converter::byteArrayToInt( &data[i], &owner ); 715 749 750 PRINTF(0)("handleCreateEntity: client side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner); 716 751 doCreateEntity( (ClassID)classID, uniqueID, owner ); 717 752 … … 779 814 { 780 815 BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner ); 781 782 /*if ( b != NULL )783 {784 if ( b->isA(CL_WORLD_ENTITY) )785 {786 int n = dynamic_cast<WorldEntity*>(b)->writeState( data, length, sender );787 788 i += n;789 }790 }*/791 816 } 792 817 … … 854 879 return true; 855 880 } 881 882 883 /** 884 * handles the network signal NET_REQUEST_PNODE_PATH 885 * @param i byte offset in the buffer 886 * @param data data array 887 * @param length length of the data arary 888 * @param sender the sender id 889 * @return true if process terminated sucessfully 890 */ 891 bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender) 892 { 893 if( INTSIZE > length-i ) 894 { 895 PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); 896 return false; 897 } 898 PRINTF(0)("HandleRequestPNodePath: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]); 899 900 int uniqueID1, uniqueID2; 901 if( INTSIZE > length-i ) 902 { 903 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 904 return false; 905 } 906 i += Converter::byteArrayToInt( &data[i], &uniqueID1 ); 907 908 if( INTSIZE > length-i ) 909 { 910 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 911 return false; 912 } 913 i += Converter::byteArrayToInt( &data[i], &uniqueID2 ); 914 915 916 PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2); 917 918 return true; 919 } 920 856 921 857 922 bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b ) … … 898 963 resizeBufferVector( user ); 899 964 900 if ( !writeToClientBuffer( outBuffer[user], (byte) REQUEST_SYNC ) )965 if ( !writeToClientBuffer( outBuffer[user], (byte)NET_REQUEST_SYNC ) ) 901 966 return; 902 967 if ( !writeToClientBuffer( outBuffer[user], uniqueID ) )
Note: See TracChangeset
for help on using the changeset viewer.