Changeset 9396 in orxonox.OLD for branches/proxy/src/lib/network/network_stream.cc
- Timestamp:
- Jul 23, 2006, 10:07:23 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/proxy/src/lib/network/network_stream.cc
r9386 r9396 70 70 71 71 72 /** 73 * start as a client, connect to a server 74 * @param host: host name (address) 75 * @param port: port number 76 */ 77 NetworkStream::NetworkStream( std::string host, int port ) 72 NetworkStream::NetworkStream( int nodeType) 78 73 { 79 74 this->init(); 80 // init the peers[0] the server to ceonnect to 81 this->peers[0].socket = new UdpSocket( host, port ); 82 this->peers[0].userId = 0; 83 this->peers[0].nodeType = NET_MASTER_SERVER; 84 this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); 85 this->peers[0].ip = this->peers[0].socket->getRemoteAddress(); 86 // and set also the localhost 87 this->pInfo->nodeType = NET_CLIENT; 75 76 this->pInfo->nodeType = nodeType; 77 78 switch( nodeType) 79 { 80 case NET_MASTER_SERVER: 81 // init the shared network data 82 SharedNetworkData::getInstance()->setHostID(0); 83 SharedNetworkData::getInstance()->setNodeType(NET_MASTER_SERVER); 84 break; 85 86 case NET_PROXY_SERVER_ACTIVE: 87 // init the shared network data 88 SharedNetworkData::getInstance()->setHostID(0); 89 SharedNetworkData::getInstance()->setNodeType(NET_PROXY_SERVER_ACTIVE); 90 break; 91 case NET_PROXY_SERVER_PASSIVE: 92 // init the shared network data 93 SharedNetworkData::getInstance()->setHostID(0); 94 SharedNetworkData::getInstance()->setNodeType(NET_PROXY_SERVER_PASSIVE); 95 break; 96 case NET_CLIENT: 97 SharedNetworkData::getInstance()->setNodeType(NET_CLIENT); 98 break; 99 } 100 101 SharedNetworkData::getInstance()->setDefaultSyncStream(this); 102 88 103 // get the local ip address 89 104 IPaddress ip; 90 SDLNet_ResolveHost( &ip, NULL, port);105 SDLNet_ResolveHost( &ip, NULL, 0); 91 106 this->pInfo->ip = ip; 92 107 } 93 108 94 95 /**96 * start as a server97 * @param port: at this port98 */99 NetworkStream::NetworkStream( int port )100 {101 this->init();102 this->serverSocket = new UdpServerSocket(port);103 this->pInfo->nodeType = NET_MASTER_SERVER;104 // get the local ip address105 IPaddress ip;106 SDLNet_ResolveHost( &ip, NULL, port );107 this->pInfo->ip = ip;108 }109 109 110 110 … … 182 182 183 183 /** 184 * establish a connection to a remote master server 185 * @param host: host name 186 * @param port: the port number 187 */ 188 void NetworkStream::connectToMasterServer(std::string host, int port) 189 { 190 int node = this->peers.size(); 191 this->peers[node].socket = new UdpSocket( host, port ); 192 this->peers[node].userId = 0; 193 194 this->peers[node].nodeType = NET_MASTER_SERVER; 195 this->peers[node].connectionMonitor = new ConnectionMonitor( 0 ); 196 this->peers[node].ip = this->peers[node].socket->getRemoteAddress(); 197 } 198 199 200 /** 201 * establish a connection to a remote proxy server 202 * @param host: host name 203 * @param port: the port number 204 */ 205 void NetworkStream::connectToProxyServer(std::string host, int port) 206 { 207 int node = this->peers.size(); 208 this->peers[node].socket = new UdpSocket( host, port ); 209 this->peers[node].userId = 0; 210 211 this->peers[node].nodeType = NET_PROXY_SERVER_ACTIVE; 212 this->peers[node].connectionMonitor = new ConnectionMonitor( 0 ); 213 this->peers[node].ip = this->peers[node].socket->getRemoteAddress(); 214 } 215 216 217 /** 218 * create a server 219 * @param port: interface port for all clients 220 */ 221 void NetworkStream::createServer(int port) 222 { 223 this->serverSocket = new UdpServerSocket(port); 224 } 225 226 227 /** 184 228 * creates a new instance of the network game manager 185 229 */ … … 187 231 { 188 232 this->networkGameManager = NetworkGameManager::getInstance(); 189 // setUniqueID( maxCon+2 ) because we need one id for every handshake 190 // and one for handshake to reject client maxCon+1 233 191 234 this->networkGameManager->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); 192 235 MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); … … 260 303 } 261 304 262 if ( this->pInfo-> nodeType == NET_MASTER_SERVER)305 if ( this->pInfo->isMasterServer()) 263 306 { 264 307 // execute everytthing the master server shoudl do … … 268 311 this->updateConnectionList(); 269 312 } 270 else if( this->pInfo-> nodeType == NET_PROXY_SERVER_ACTIVE)313 else if( this->pInfo->isProxyServer()) 271 314 { 272 315 // execute everything the proxy server should do 316 if ( serverSocket ) 317 serverSocket->update(); 318 319 this->updateConnectionList(); 273 320 } 274 321 else … … 442 489 void NetworkStream::debug() 443 490 { 444 if( this->isMasterServer()) 445 PRINT(0)(" Host ist Server with ID: %i\n", this->pInfo->userId); 446 else 491 if( SharedNetworkData::getInstance()->isMasterServer()) { 492 PRINT(0)(" Host ist Master Server with ID: %i\n", this->pInfo->userId); 493 } 494 else if( SharedNetworkData::getInstance()->isProxyServer()) { 495 PRINT(0)(" Host ist Proxy Server with ID: %i\n", this->pInfo->userId); 496 } 497 else { 447 498 PRINT(0)(" Host ist Client with ID: %i\n", this->pInfo->userId); 499 } 448 500 449 501 PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); … … 493 545 { 494 546 495 if ( this->pInfo-> nodeType == NET_CLIENT)547 if ( this->pInfo->isClient() ) 496 548 { 497 498 549 SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); 499 550 this->pInfo->userId = SharedNetworkData::getInstance()->getHostID(); … … 528 579 if ( it->second.handshake->canDel() ) 529 580 { 530 if ( this->pInfo->nodeType == NET_MASTER_SERVER ) 581 582 if ( this->pInfo->isMasterServer() ) 583 { 584 it->second.nodeType = it->second.handshake->getRemoteNodeType(); 585 it->second.ip = it->second.socket->getRemoteAddress(); 586 587 this->networkMonitor->addNode(&it->second); 588 589 this->handleNewClient( it->second.userId ); 590 591 if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) 592 { 593 PlayerStats::getStats( it->second.userId )->setNickName( it->second.handshake->getPreferedNickName() ); 594 } 595 } 596 else if ( this->pInfo->isProxyServer() ) 531 597 { 532 598 it->second.nodeType = it->second.handshake->getRemoteNodeType(); … … 639 705 continue; 640 706 641 // if we are a server and this is not our handshake642 if ( isMasterServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId )707 // if we are a server (both master and proxy servers) and this is not our handshake 708 if ( ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer() ) && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) 643 709 continue; 644 710 … … 654 720 655 721 // server fakes uniqueid == 0 for handshake 656 if ( this->isMasterServer() && sync.getUniqueID() < SharedNetworkData::getInstance()->getMaxPlayer() - 1 )722 if ( ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer() ) && sync.getUniqueID() < SharedNetworkData::getInstance()->getMaxPlayer() - 1 ) 657 723 n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); 658 724 else … … 703 769 // now compress the data with the zip library 704 770 int compLength = 0; 705 if ( this->isMasterServer())771 if ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer()) 706 772 compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer ); 707 773 else … … 783 849 } 784 850 785 while ( offset + 2 *INTSIZE < length )851 while ( offset + 2 * INTSIZE < length ) 786 852 { 787 853 assert( offset > 0 ); … … 797 863 Synchronizeable * sync = NULL; 798 864 865 // look for the synchronizeable in question 799 866 for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) 800 867 { … … 807 874 } 808 875 876 // this synchronizeable does not yet exist! create it 809 877 if ( sync == NULL ) 810 878 { 811 879 PRINTF(0)("could not find sync with id %d. try to create it\n", uniqueId); 880 881 // if it is an old synchronizeable already removed, ignore it 812 882 if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() ) 813 883 { … … 816 886 } 817 887 818 if ( !peers[peer->second.userId].isMasterServer() ) 888 // if the node we got this unknown sync from is a client we ignore it (since it has no rights to create a new sync) 889 if ( peers[peer->second.userId].isClient() ) 819 890 { 820 891 offset += syncDataLength; … … 833 904 assert( leafClassId != 0 ); 834 905 906 835 907 BaseObject * b = NULL; 836 908 /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ … … 838 910 if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE || leafClassId == CL_NETWORK_GAME_MANAGER ) 839 911 { 840 PRINTF(1)(" Can not create Class with ID %x!\n", (int)leafClassId);912 PRINTF(1)("Don't create Object with ID %x, ignored!\n", (int)leafClassId); 841 913 offset += syncDataLength; 842 914 continue; … … 913 985 // do all game relevant stuff here 914 986 networkGameManager->signalNewPlayer( userId ); 915 916 // register the new client at the network monitor917 // this->networkMonitor->addClient();918 987 } 919 988
Note: See TracChangeset
for help on using the changeset viewer.