Changeset 6695 in orxonox.OLD for trunk/src/lib/network
- Timestamp:
- Jan 25, 2006, 2:39:53 PM (19 years ago)
- Location:
- trunk/src/lib/network
- Files:
-
- 13 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/Makefile.am
r6139 r6695 6 6 libORXnet_a_SOURCES = synchronizeable.cc \ 7 7 network_manager.cc \ 8 shared_network_data.cc \ 8 9 network_socket.cc \ 9 10 connection_monitor.cc \ … … 21 22 noinst_HEADERS = synchronizeable.h \ 22 23 network_manager.h \ 24 shared_network_data.h \ 23 25 network_socket.h \ 24 26 connection_monitor.h \ -
trunk/src/lib/network/handshake.cc
r6341 r6695 23 23 24 24 Handshake::Handshake( bool server, int clientId, int networkGameManagerId ) 25 : Synchronizeable() 25 26 { 26 27 /* set the class id for the base object */ … … 34 35 this->setOwner(0); 35 36 37 this->setSynchronized(true); 36 38 PRINTF(5)("Handshake created clientId = %d\n", clientId); 37 39 } -
trunk/src/lib/network/netdefs.h
r5822 r6695 17 17 18 18 19 #define MAX_CONNECTIONS 1000 20 21 19 22 typedef unsigned char byte; 20 23 … … 25 28 } NodeType; 26 29 30 31 typedef enum { 32 NET_UID_UNASSIGNED = -1, 33 34 NET_UID_NUMBER 35 } UidType; 36 27 37 #endif /* _NETWORK_MANAGER */ -
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 ) ) -
trunk/src/lib/network/network_game_manager.h
r6634 r6695 15 15 16 16 class TiXmlElement; 17 class PNode; 17 18 18 19 /** 19 20 * protocol definition 20 21 * 21 * CREATE_ENTITY: CLASS_ID, UNIQUE_ID, OWNER22 * REMOVE_ENTITY: UNIQUE_ID22 * CREATE_ENTITY: >> CLASS_ID, UNIQUE_ID, OWNER 23 * REMOVE_ENTITY: >> UNIQUE_ID 23 24 * 24 * CREATE_ENTITY_LIST: NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER]25 * REMOVE_ENTITY_LIST: NUMBER, [UNIQUE_ID][0..NUMBER]25 * CREATE_ENTITY_LIST: >> NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER] 26 * REMOVE_ENTITY_LIST: >> NUMBER, [UNIQUE_ID][0..NUMBER] 26 27 * 27 * REQUEST_CREATE: CLASS_ID 28 * REQUEST_REMOVE: UNIQUE_ID 28 * REQUEST_CREATE: >> CLASS_ID 29 * << [Sync Info] 30 * REQUEST_REMOVE: >> UNIQUE_ID 31 * << [Sync Info] 29 32 * 30 33 * //REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER] … … 32 35 * 33 36 * REQUEST_ENTITY_LIST: //request the whole world :D 34 * REQUEST_SYNC: UNIQUE_ID 37 * REQUEST_SYNC: >> UNIQUE_ID 38 * << [Sync Info] 35 39 * //REQUEST_SYNC_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] 36 40 * 37 * YOU_ARE_ENTITY: UNIQUE_ID41 * YOU_ARE_ENTITY: >> UNIQUE_ID 38 42 * 43 * REQUEST_PNODE_PATH >> UNIQUE_ID_START UNIQUE_ID_STOP 44 * << UNIQUE_ID_1 UNIQUE_ID_2 UNIQUE_ID_3 ... UNIQUE_ID_N 45 * 46 * SEND_PNODE_PATH >> UNIQUE_ID_START UNIQUE_ID_STOP NUMBER [UNIQUE_ID][0..NUMBER] 39 47 */ 40 48 41 typedef enum NetworkGameManagerProtocol{ 42 CREATE_ENTITY = 0, 43 REMOVE_ENTITY, 44 CREATE_ENTITY_LIST, 45 REMOVE_ENTITY_LIST, 46 REQUEST_CREATE, 47 REQUEST_REMOVE, 48 REQUEST_SYNC, 49 YOU_ARE_ENTITY, 50 REQUEST_ENTITY_LIST 49 typedef enum NetworkGameManagerProtocol { 50 NET_CREATE_ENTITY = 0, 51 NET_REMOVE_ENTITY, 52 NET_CREATE_ENTITY_LIST, 53 NET_REMOVE_ENTITY_LIST, 54 NET_REQUEST_CREATE, 55 NET_REQUEST_REMOVE, 56 NET_REQUEST_SYNC, 57 NET_YOU_ARE_ENTITY, 58 NET_REQUEST_ENTITY_LIST, 59 NET_REQUEST_PNODE_PATH, 60 NET_SEND_PNODE_PATH, 61 62 NET_NUMBER 51 63 }; 52 64 … … 83 95 void sendEntityList(int userID); 84 96 97 85 98 private: 86 99 NetworkGameManager(); 87 100 101 102 /* some network signal handlers */ 103 bool handleRequestCreate( int& i, const byte* data, int length, int sender ); 104 bool handleRequestRemove( int& i, const byte* data, int length, int sender ); 105 bool handleCreateEntity( int& i, const byte* data, int length, int sender ); 106 bool handleRemoveEntity( int& i, const byte* data, int length, int sender ); 107 bool handleCreateEntityList( int& i, const byte* data, int length, int sender ); 108 bool handleRemoveEntityList( int& i, const byte* data, int length, int sender ); 109 bool handleYouAreEntity( int& i, const byte* data, int length, int sender ); 110 bool handleRequestSync( int& i, const byte* data, int length, int sender ); 111 bool handleRequestPNodePath(int& i, const byte* data, int length, int sender); 112 bool handleSendPNodePath(int& i, const byte* data, int length, int sender); 113 114 115 /* some network handlers helper functions */ 88 116 void requestCreateEntity(ClassID classID); 89 117 int executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0); 118 BaseObject* doCreateEntity(ClassID classID, int uniqueID, int owner); 90 119 91 120 void requestRemoveEntity(int uniqueID); 92 121 void executeRemoveEntity(int uniqueID); 122 void doRemoveEntity(int uniqueID); 93 123 94 124 void executeRequestSync( int uniqueID, int user ); 125 void doRequestSync(int uniqueID, int userID); 95 126 96 BaseObject* doCreateEntity(ClassID classID, int uniqueID, int owner);97 void doRemoveEntity(int uniqueID);98 void doRequestSync(int uniqueID, int userID);99 127 void doYouAre( int uniqueID ); 128 129 void requestPNodePath(const PNode* node1, const PNode* node2); 130 void executeRequestPNodePath(const PNode* node2, const PNode* node2); 131 void doRequestPNodePath(const PNode* node1, const PNode* node2); 100 132 101 133 bool canCreateEntity(ClassID classID); … … 105 137 void resizeBufferVector(int n); 106 138 107 inlinebool writeToClientBuffer( clientBuffer &cb, byte*data, int length );108 inlinebool writeToClientBuffer( clientBuffer &cb, byte b );109 inlinebool writeToClientBuffer( clientBuffer &cb, int i );110 inlinebool readFromClientBuffer( clientBuffer &cb, byte*data, int length );139 bool writeToClientBuffer( clientBuffer &cb, byte*data, int length ); 140 bool writeToClientBuffer( clientBuffer &cb, byte b ); 141 bool writeToClientBuffer( clientBuffer &cb, int i ); 142 bool readFromClientBuffer( clientBuffer &cb, byte*data, int length ); 111 143 112 //helper functions for writeBytes113 inline bool handleRequestCreate( int& i, const byte* data, int length, int sender );114 inline bool handleRequestRemove( int& i, const byte* data, int length, int sender );115 inline bool handleCreateEntity( int& i, const byte* data, int length, int sender );116 inline bool handleRemoveEntity( int& i, const byte* data, int length, int sender );117 inline bool handleCreateEntityList( int& i, const byte* data, int length, int sender );118 inline bool handleRemoveEntityList( int& i, const byte* data, int length, int sender );119 inline bool handleYouAreEntity( int& i, const byte* data, int length, int sender );120 inline bool handleRequestSync( int& i, const byte* data, int length, int sender );121 144 122 145 private: … … 124 147 //clientBuffer allOutBuffer; 125 148 static NetworkGameManager* singletonRef; 126 127 int newUniqueID;128 149 bool hasRequestedWorld; 129 150 }; -
trunk/src/lib/network/network_manager.cc
r6341 r6695 20 20 #define DEBUG_MODULE_NETWORK 21 21 22 23 #include "network_stream.h"24 22 #include "class_list.h" 25 26 23 #include "debug.h" 27 24 #include "shell_command.h" 28 25 29 26 /* include your own header */ 30 27 #include "network_manager.h" 28 #include "shared_network_data.h" 29 #include "network_stream.h" 31 30 32 31 33 32 /* using namespace std is default, this needs to be here */ 34 33 using namespace std; 34 35 SHELL_COMMAND(debug, NetworkManager, debug); 35 36 36 37 … … 49 50 this->netStreamList = NULL; 50 51 this->syncList = NULL; 51 this->tmpStream = NULL; 52 53 this->hostID = -1; 54 this->bGameServer = false; 52 this->defaultSyncStream = NULL; 53 this->sharedNetworkData = SharedNetworkData::getInstance(); 55 54 56 55 PRINTF(0)("NetworkManager created\n"); … … 98 97 } 99 98 100 this->tmpStream = new NetworkStream(ipAddress); 99 this->defaultSyncStream = new NetworkStream(ipAddress); 100 this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream); 101 this->defaultSyncStream->startHandshake(); 101 102 return 1; 102 103 } … … 109 110 int NetworkManager::createServer(unsigned int port) 110 111 { 111 this->hostID = 0; 112 this->bGameServer = true; 113 this->tmpStream = new NetworkStream(port); 114 this->bGameServer = true; 112 this->sharedNetworkData->setHostID(0); 113 this->sharedNetworkData->setGameServer(true); 114 this->defaultSyncStream = new NetworkStream(port); 115 this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream); 116 this->defaultSyncStream->createNetworkGameManager(); 117 PRINTF(0)("CREATE SERVER\n"); 115 118 SDL_Delay(20); 116 119 return 1; … … 118 121 119 122 120 /**121 * creates a connection from one object to a host122 * @param address: the address of the destination host123 * @param synchronizeable: reference to the sync object124 */125 NetworkStream& NetworkManager::establishConnection(IPaddress& address, Synchronizeable& sync)126 {127 /* creating a new network stream, it will register itself automaticaly to the class list */128 this->tmpStream = new NetworkStream(address);129 this->tmpStream->connectSynchronizeable(sync);130 }131 132 133 /**134 * teardown a connection135 */136 void NetworkManager::shutdownConnection()137 {138 PRINTF(0)("Shutdown connection\n");139 }140 141 142 123 void NetworkManager::connectSynchronizeable(Synchronizeable& sync) 143 124 { 144 this->tmpStream->connectSynchronizeable(sync); 125 if( this->defaultSyncStream) 126 this->defaultSyncStream->connectSynchronizeable(sync); 145 127 } 146 128 … … 157 139 if( static_cast<NetworkStream*>(*stream)->isActive()) 158 140 static_cast<NetworkStream*>(*stream)->processData(); 159 160 141 } 161 162 142 } 163 143 144 145 164 146 /** 165 * Sets the hostID to a specific number 166 * @param id: The new ID 147 * debug output 167 148 */ 168 void NetworkManager::setHostID(int id)149 void NetworkManager::debug() 169 150 { 170 this->hostID = id; 151 PRINT(0)("=================Network::debug()=========\n"); 152 this->defaultSyncStream->debug(); 153 PRINT(0)("===========================================\n"); 171 154 } -
trunk/src/lib/network/network_manager.h
r6341 r6695 6 6 /* you will want to add such a a line at your header file also, since it will 7 7 prevent c++ from including your code twice*/ 8 #ifndef _NETWORK_MAN GER8 #ifndef _NETWORK_MANAGER 9 9 #define _NETWORK_MANAGER 10 10 11 11 /* include this file, it contains some default definitions */ 12 12 #include "netdefs.h" 13 #include "shared_network_data.h" 13 14 14 15 /* include base_object.h since all classes are derived from this one */ … … 38 39 int createServer(unsigned int port); 39 40 40 NetworkStream& establishConnection(IPaddress& address, Synchronizeable& sync);41 void shutdownConnection();42 43 44 void setHostID(int id);45 41 /** Returns the hostID @return The hostID of the object */ 46 inline int getHostID() { return this-> hostID; }47 inline bool isGameServer() { return this-> bGameServer; }42 inline int getHostID() { return this->sharedNetworkData->getHostID(); } 43 inline bool isGameServer() { return this->sharedNetworkData->isGameServer(); } 48 44 49 45 50 46 void connectSynchronizeable(Synchronizeable& sync); 51 47 void synchronize(); 48 49 void debug(); 50 52 51 53 52 private: … … 59 58 const std::list<BaseObject*>* syncList; // list of synchronizeables 60 59 static NetworkManager* singletonRef; //!< Pointer to the only instance of this Class 61 NetworkStream* tmpStream; //!< FIXME: this is only for testing purposes 62 int hostID; //!< The Host-ID of the Manager 63 bool bGameServer; //!< true if it is a server 60 NetworkStream* defaultSyncStream; //!< FIXME: this is only for testing purposes 64 61 62 SharedNetworkData* sharedNetworkData; //!< reference to the shared data 65 63 }; 66 64 -
trunk/src/lib/network/network_socket.cc
r6634 r6695 97 97 /** 98 98 * Default destructor 99 * dont use this from outside: use destroy() instead!! 99 100 */ 100 101 NetworkSocket::~NetworkSocket( ) -
trunk/src/lib/network/network_socket.h
r6139 r6695 81 81 void init(); 82 82 83 //dont make this public use destroy() instead84 ~NetworkSocket();85 86 83 public: 87 84 88 85 NetworkSocket(); 86 ~NetworkSocket(); 89 87 NetworkSocket(IPaddress ip); 90 88 NetworkSocket(TCPsocket sock); -
trunk/src/lib/network/network_stream.cc
r6634 r6695 53 53 } 54 54 55 55 56 NetworkStream::NetworkStream(IPaddress& address) 56 57 { … … 60 61 this->networkProtocol = new NetworkProtocol(); 61 62 this->connectionMonitor = new ConnectionMonitor(); 62 63 Handshake* hs = new Handshake(false);64 hs->setUniqueID( 0 );65 this->handshakes.push_back(hs);66 this->connectSynchronizeable(*hs);67 PRINTF(0)("NetworkStream: %s\n", hs->getName());68 63 this->maxConnections = 1; 69 64 } … … 81 76 this->handshakes.push_back( NULL ); 82 77 this->bActive = true; 83 this->networkGameManager = NetworkGameManager::getInstance();84 // setUniqueID( maxCon+2 ) because we need one id for every handshake85 // and one for handshake to reject client maxCon+186 this->networkGameManager->setUniqueID( this->maxConnections+2 );87 this->connectSynchronizeable( *(this->networkGameManager) );88 89 this->setMaxConnections( 10 );90 78 } 91 79 … … 132 120 133 121 122 void NetworkStream::createNetworkGameManager() 123 { 124 this->networkGameManager = NetworkGameManager::getInstance(); 125 // setUniqueID( maxCon+2 ) because we need one id for every handshake 126 // and one for handshake to reject client maxCon+1 127 this->networkGameManager->setUniqueID( this->maxConnections + 2 ); 128 //this->connectSynchronizeable( *(this->networkGameManager) ); 129 this->setMaxConnections( 10 ); 130 } 131 132 133 void NetworkStream::startHandshake() 134 { 135 Handshake* hs = new Handshake(false); 136 hs->setUniqueID( 0 ); 137 this->handshakes.push_back(hs); 138 //this->connectSynchronizeable(*hs); 139 PRINTF(0)("NetworkStream: %s\n", hs->getName()); 140 } 141 142 134 143 void NetworkStream::connectSynchronizeable(Synchronizeable& sync) 135 144 { … … 140 149 this->bActive = true; 141 150 } 151 142 152 143 153 void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) … … 184 194 if ( type != NET_SERVER ) 185 195 { 186 NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() );196 SharedNetworkData::getInstance()->setHostID( handshakes[i]->getHostId() ); 187 197 myHostId = NetworkManager::getInstance()->getHostID(); 188 198 189 199 this->networkGameManager = NetworkGameManager::getInstance(); 190 200 this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() ); 191 this->connectSynchronizeable( *(this->networkGameManager) );201 //this->connectSynchronizeable( *(this->networkGameManager) ); 192 202 } 193 203 else … … 216 226 /* DOWNSTREAM */ 217 227 228 229 218 230 int dataLength; 219 231 int reciever; … … 221 233 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 222 234 { 223 if ( (*it)!=NULL /*&& (*it)->getOwner() == myHostId*/ )235 if ( (*it)!=NULL && (*it)->beSynchronized() /*&& (*it)->getOwner() == myHostId*/ ) 224 236 { 225 237 do { … … 357 369 PRINTF(0)("New Client: %d\n", clientId); 358 370 359 this->connectSynchronizeable(*handshakes[clientId]);371 //this->connectSynchronizeable(*handshakes[clientId]); 360 372 } 361 373 … … 416 428 417 429 430 431 void NetworkStream::debug() 432 { 433 if( this->isServer()) 434 PRINT(0)(" Host ist Server with ID: %i\n", this->myHostId); 435 else 436 PRINT(0)(" Host ist Client with ID: %i\n", this->myHostId); 437 438 PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); 439 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 440 { 441 if( (*it)->beSynchronized() == true) 442 PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(), 443 (*it)->getUniqueID(), (*it)->beSynchronized()); 444 } 445 PRINT(0)(" Maximal Connections: %i\n", this->maxConnections); 446 447 } 448 449 450 int NetworkStream::getSyncCount() 451 { 452 int n = 0; 453 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 454 if( (*it)->beSynchronized() == true) 455 ++n; 456 457 //return synchronizeables.size(); 458 return n; 459 } 460 461 462 463 464 465 -
trunk/src/lib/network/network_stream.h
r6634 r6695 14 14 #include "server_socket.h" 15 15 #include "handshake.h" 16 17 #define MAX_CONNECTIONS 100018 16 19 17 class Synchronizeable; … … 40 38 void init(); 41 39 40 void createNetworkGameManager(); 41 void startHandshake(); 42 42 43 void connectSynchronizeable(Synchronizeable& sync); 43 44 void disconnectSynchronizeable(Synchronizeable& sync); … … 53 54 inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } 54 55 inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } 55 in line int getSyncCount(){ return synchronizeables.size(); }56 int getSyncCount(); 56 57 57 58 inline bool isUserIdActive( int userID ) { if (userID>=networkSockets.size()) return false; else return networkSockets[userID]!=NULL; } 58 59 60 void debug(); 61 62 59 63 private: 60 NetworkProtocol* networkProtocol; 61 ConnectionMonitor* connectionMonitor; 62 SynchronizeableList synchronizeables; 63 NetworkSocketVector networkSockets; 64 HandshakeVector handshakes; 65 ServerSocket* serverSocket; 66 int type; 67 Header packetHeader; 68 bool bActive; 69 std::list<int> freeSocketSlots; 64 void updateConnectionList(); 70 65 71 int myHostId;72 int maxConnections;73 66 74 NetworkGameManager* networkGameManager; 67 private: 68 NetworkProtocol* networkProtocol; 69 ConnectionMonitor* connectionMonitor; 70 SynchronizeableList synchronizeables; 71 NetworkSocketVector networkSockets; 72 HandshakeVector handshakes; 73 ServerSocket* serverSocket; 74 int type; 75 Header packetHeader; 76 bool bActive; 77 std::list<int> freeSocketSlots; 75 78 76 void updateConnectionList(); 79 int myHostId; 80 int maxConnections; 81 82 NetworkGameManager* networkGameManager; 77 83 }; 78 84 #endif /* _NETWORK_STREAM */ -
trunk/src/lib/network/synchronizeable.cc
r6424 r6695 17 17 #define DEBUG_MODULE_NETWORK 18 18 19 #include "shared_network_data.h" 20 #include "network_stream.h" 21 #include "netdefs.h" 22 23 #include "state.h" 24 25 #include "assert.h" 26 19 27 #include "synchronizeable.h" 20 #include "netdefs.h" 21 #include "network_manager.h" 22 #include "network_stream.h" 28 23 29 24 30 … … 29 35 { 30 36 this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); 31 owner = 0;32 state = 0;33 hostID = NetworkManager::getInstance()->getHostID();37 this->owner = 0; 38 this->state = 0; 39 this->hostID = SharedNetworkData::getInstance()->getHostID(); 34 40 this->setIsServer(this->hostID == 0); 35 uniqueID = -1;41 this->uniqueID = NET_UID_UNASSIGNED; 36 42 this->networkStream = NULL; 37 43 this->setRequestedSync( false ); 38 44 this->setIsOutOfSync( !(this->isServer()) ); 45 this->bSynchronize = false; 46 47 if( State::isOnline()) 48 { 49 NetworkStream* nd = SharedNetworkData::getInstance()->getDefaultSyncStream(); 50 assert(nd != NULL); 51 nd->connectSynchronizeable(*this); 52 this->setUniqueID(SharedNetworkData::getInstance()->getNewUniqueID()); 53 } 39 54 } 40 55 … … 50 65 } 51 66 67 52 68 /** 53 69 * write data to NetworkStream … … 57 73 PRINTF(5)("Synchronizeable::writeBytes was called\n"); 58 74 } 75 59 76 60 77 /** … … 87 104 } 88 105 106 89 107 /** 90 108 * Sets the outofsync flag to a given value … … 100 118 } 101 119 120 102 121 /** 103 122 * Determines if the server flag is set … … 108 127 return (this->state & STATE_SERVER) >0; 109 128 } 129 110 130 111 131 /** … … 118 138 } 119 139 140 120 141 /** 121 142 * Determines if the requestedSync flag is set … … 126 147 return (this->state & STATE_REQUESTEDSYNC) >0; 127 148 } 149 128 150 129 151 /** -
trunk/src/lib/network/synchronizeable.h
r6341 r6695 163 163 164 164 class Synchronizeable : virtual public BaseObject 165 { 165 { 166 166 167 public: 167 168 Synchronizeable(); 168 ~Synchronizeable();169 virtual ~Synchronizeable(); 169 170 170 171 virtual int writeBytes(const byte* data, int length, int sender); … … 179 180 bool isOutOfSync(); 180 181 bool requestedSync(); 182 181 183 inline void setUniqueID( int id ){ uniqueID = id; } 182 inline int getUniqueID() const { return uniqueID; }; 184 inline int getUniqueID() const { return uniqueID; } 185 inline int getHostID() { return this->hostID; } 186 187 inline int getOwner(){ return owner; } 188 inline void setOwner(int owner){ this->owner = owner; } 189 190 /** @returns true if this Synchronizeable has to be synchronized over network */ 191 inline bool beSynchronized() { return this->bSynchronize; } 192 /** @param bSynchronize sets the Synchronizeable to be sunchronized or not */ 193 inline void setSynchronized(bool bSynchronize) { this->bSynchronize = bSynchronize; } 194 183 195 inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } 184 196 inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } }; 185 inline int getHostID() { return this->hostID; }186 187 inline int getOwner(){ return owner; }188 inline void setOwner(int owner){ this->owner = owner; }189 197 190 198 inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; } 199 inline NetworkStream* getNetworkStream() { return this->networkStream; } 200 201 202 protected: 203 NetworkStream* networkStream; 204 int state; 205 191 206 192 207 private: 193 194 208 int uniqueID; 195 196 197 198 //static std::vector<Synchronizeable*> classList; 199 int owner; 200 int hostID; 201 202 std::list<int> synchronizeRequests; 203 204 protected: 205 NetworkStream* networkStream; 206 int state; 207 208 }; 209 int owner; 210 int hostID; 211 bool bSynchronize; 212 213 std::list<int> synchronizeRequests; 214 215 }; 209 216 #endif /* _SYNCHRONIZEABLE_H */
Note: See TracChangeset
for help on using the changeset viewer.