Changeset 9656 in orxonox.OLD for trunk/src/lib/network/network_stream.cc
- Timestamp:
- Aug 4, 2006, 11:01:28 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/network_stream.cc
r9494 r9656 11 11 ### File Specific: 12 12 main-programmer: Christoph Renner rennerc@ee.ethz.ch 13 co-programmer: Patrick Boenzli boenzlip@orxonox.ethz.ch13 co-programmer: Patrick Boenzli patrick@orxonox.ethz.ch 14 14 15 15 June 2006: finishing work on the network stream for pps presentation (rennerc@ee.ethz.ch) 16 July 2006: some code rearangement and integration of the proxy server mechanism ( boenzlip@ee.ethz.ch)16 July 2006: some code rearangement and integration of the proxy server mechanism (patrick@orxonox.ethz.ch) 17 17 */ 18 18 … … 20 20 #define DEBUG_MODULE_NETWORK 21 21 22 #include "proxy/proxy_control.h" 22 23 23 24 #include "base_object.h" … … 80 81 // init the shared network data 81 82 SharedNetworkData::getInstance()->setHostID(NET_ID_MASTER_SERVER); 83 this->pInfo->userId = NET_ID_MASTER_SERVER; 84 this->pInfo->nodeType = NET_MASTER_SERVER; 85 82 86 break; 83 87 case NET_PROXY_SERVER_ACTIVE: 84 88 // init the shared network data 85 89 SharedNetworkData::getInstance()->setHostID(NET_ID_PROXY_SERVER_01); 90 this->pInfo->nodeType = NET_PROXY_SERVER_ACTIVE; 91 86 92 break; 87 93 case NET_PROXY_SERVER_PASSIVE: 88 94 // init the shared network data 89 95 SharedNetworkData::getInstance()->setHostID(NET_ID_PROXY_SERVER_01); 96 this->pInfo->nodeType = NET_PROXY_SERVER_PASSIVE; 97 90 98 break; 91 99 case NET_CLIENT: 92 100 SharedNetworkData::getInstance()->setHostID(NET_ID_UNASSIGNED); 101 this->pInfo->nodeType = NET_CLIENT; 93 102 break; 94 103 } … … 97 106 98 107 // get the local ip address 99 IPaddress ip; 100 SDLNet_ResolveHost( &ip, NULL, 0); 108 IP ip("localhost", 0); 101 109 this->pInfo->ip = ip; 102 110 } … … 112 120 this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); 113 121 this->clientSocket = NULL; 122 this->clientSoftSocket = NULL; 114 123 this->proxySocket = NULL; 115 124 this->networkGameManager = NULL; … … 117 126 118 127 this->pInfo = new PeerInfo(); 119 this->pInfo->userId = 0;128 this->pInfo->userId = NET_UID_UNASSIGNED; 120 129 this->pInfo->lastAckedState = 0; 121 130 this->pInfo->lastRecvedState = 0; 131 this->pInfo->bLocal = true; 122 132 123 133 this->bRedirect = false; 124 134 125 135 this->currentState = 0; 136 this->redirectionUID = NET_ID_MASTER_SERVER; 126 137 127 138 remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 ); … … 142 153 if ( this->clientSocket ) 143 154 { 144 clientSocket->close(); 145 delete clientSocket; 146 clientSocket = NULL; 155 this->clientSocket->close(); 156 delete this->clientSocket; 157 this->clientSocket = NULL; 158 } 159 if ( this->clientSoftSocket ) 160 { 161 this->clientSoftSocket->close(); 162 delete this->clientSoftSocket; 163 this->clientSoftSocket = NULL; 147 164 } 148 165 if ( this->proxySocket) … … 199 216 this->peers[node].connectionMonitor = new ConnectionMonitor( NET_ID_MASTER_SERVER ); 200 217 this->peers[node].ip = this->peers[node].socket->getRemoteAddress(); 218 this->peers[node].bLocal = true; 201 219 } 202 220 … … 218 236 this->peers[proxyId].connectionMonitor = new ConnectionMonitor( proxyId ); 219 237 this->peers[proxyId].ip = this->peers[proxyId].socket->getRemoteAddress(); 238 this->peers[proxyId].bLocal = true; 220 239 } 221 240 … … 225 244 * @param port: interface port for all clients 226 245 */ 227 void NetworkStream::createServer(int clientPort, int proxyPort )246 void NetworkStream::createServer(int clientPort, int proxyPort, int clientSoftPort) 228 247 { 229 248 PRINTF(0)(" Creating new Server: listening for clients on port %i and for proxies on port %i", clientPort, proxyPort); 230 249 this->clientSocket= new UdpServerSocket(clientPort); 250 this->clientSoftSocket= new UdpServerSocket(clientSoftPort); 231 251 this->proxySocket = new UdpServerSocket(proxyPort); 232 252 } … … 255 275 Handshake* hs = new Handshake(this->pInfo->nodeType); 256 276 // fake the unique id 257 hs->setUniqueID( NET_UID_HANDSHAKE ); 258 assert( peers[userId].handshake == NULL ); 259 peers[userId].handshake = hs; 277 hs->setUniqueID( userId); 278 assert( this->peers[userId].handshake == NULL ); 279 this->peers[userId].handshake = hs; 280 this->peers[userId].bLocal = true; 260 281 261 282 // set the preferred nick name … … 301 322 // create the network monitor after all the init work and before there is any connection handlings 302 323 if( this->networkMonitor == NULL) 324 { 303 325 this->networkMonitor = new NetworkMonitor(this); 326 SharedNetworkData::getInstance()->setNetworkMonitor( this->networkMonitor); 327 } 304 328 305 329 … … 318 342 if ( this->clientSocket ) 319 343 this->clientSocket->update(); 344 if ( this->clientSoftSocket) 345 this->clientSoftSocket->update(); 320 346 if( this->proxySocket) 321 347 this->proxySocket->update(); … … 328 354 if ( this->clientSocket ) 329 355 this->clientSocket->update(); 356 if ( this->clientSoftSocket) 357 this->clientSoftSocket->update(); 330 358 if( this->proxySocket) 331 359 this->proxySocket->update(); … … 348 376 if( this->bRedirect) 349 377 { 350 this->handleReconnect( NET_ID_MASTER_SERVER);378 this->handleReconnect( this->redirectionUID); 351 379 } 352 380 } … … 362 390 this->handleDownstream( tick ); 363 391 this->handleUpstream( tick ); 392 393 // process the local data of the message manager 394 MessageManager::getInstance()->processData(); 395 396 if( this->bSoftRedirect) 397 this->softReconnectToServer(0, IP("localhost", 10001)); 364 398 } 365 399 … … 385 419 if ( tempNetworkSocket ) 386 420 { 387 // determine the network node id 388 if ( freeSocketSlots.size() > 0 ) 421 // get a userId 422 // if ( freeSocketSlots.size() > 0 ) 423 // { 424 // // this should never be called 425 // userId = freeSocketSlots.back(); 426 // freeSocketSlots.pop_back(); 427 // } 428 // else 389 429 { 390 userId = freeSocketSlots.back(); 391 freeSocketSlots.pop_back(); 392 } 393 else 394 { 395 userId = 1; 430 // each server (proxy and master) have an address space for new network nodes of 1000 nodes 431 // the first NET_ID_PROXY_MAX are always reserved for proxy servers 432 userId = SharedNetworkData::getInstance()->getHostID() * 1000 + NET_ID_PROXY_MAX + 1; 396 433 397 434 for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) 398 435 if ( it->first >= userId ) 399 436 userId = it->first + 1; 437 438 // make sure that this server only uses an address space of 1000 439 assert( userId < (SharedNetworkData::getInstance()->getHostID() + 1) * 1000); 400 440 } 401 441 // this creates a new entry in the peers list … … 410 450 } 411 451 452 if( this->clientSoftSocket != NULL) 453 { 454 tempNetworkSocket = this->clientSoftSocket->getNewSocket(); 455 456 // we got new NET_CLIENT connecting 457 if ( tempNetworkSocket ) 458 { 459 460 // this creates a new entry in the peers list 461 peers[userId].socket = tempNetworkSocket; 462 peers[userId].nodeType = NET_CLIENT; 463 464 // handle the newly connected client 465 this->handleSoftConnect(userId); 466 467 PRINTF(0)("New Client softly connected: %d :D\n", userId); 468 } 469 } 470 412 471 413 472 if( this->proxySocket != NULL) … … 419 478 { 420 479 // determine the network node id 421 if ( freeSocketSlots.size() > 0 )422 {423 userId = freeSocketSlots.back();424 freeSocketSlots.pop_back();425 }426 else480 // if ( freeSocketSlots.size() > 0 ) 481 // { 482 // userId = freeSocketSlots.back(); 483 // freeSocketSlots.pop_back(); 484 // } 485 // else 427 486 { 428 487 userId = 1; 429 488 430 for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) 431 if ( it->first >= userId ) 432 userId = it->first + 1; 489 // find an empty slot within the range 490 for( int i = 0; i < NET_ID_PROXY_MAX; i++) 491 { 492 if( this->peers.find( i) == this->peers.end()) 493 { 494 userId = i; 495 break; 496 } 497 } 498 499 // for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) 500 // if ( it->first >= userId ) 501 // userId = it->first + 1; 433 502 } 434 503 … … 443 512 } 444 513 } 514 445 515 446 516 … … 461 531 PRINTF(0)("Client is gone: %d (%s)\n", it->second.userId, reason.c_str()); 462 532 533 463 534 this->handleDisconnect( it->second.userId); 535 536 if( SharedNetworkData::getInstance()->isProxyServerActive()) 537 ProxyControl::getInstance()->signalLeaveClient(it->second.userId); 464 538 465 539 it++; … … 469 543 it++; 470 544 } 471 472 473 545 } 474 546 … … 481 553 { 482 554 // create new handshake and init its variables 483 peers[userId].handshake = new Handshake(this->pInfo->nodeType, userId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); 484 peers[userId].handshake->setUniqueID(userId); 485 486 peers[userId].connectionMonitor = new ConnectionMonitor( userId ); 487 peers[userId].userId = userId; 555 this->peers[userId].handshake = new Handshake(this->pInfo->nodeType, userId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); 556 this->peers[userId].handshake->setUniqueID(userId); 557 558 this->peers[userId].connectionMonitor = new ConnectionMonitor( userId ); 559 this->peers[userId].userId = userId; 560 this->peers[userId].bLocal = true; 488 561 489 562 PRINTF(0)("num sync: %d\n", synchronizeables.size()); … … 494 567 if( pi != NULL) 495 568 { 496 peers[userId].handshake->setProxy1Address( pi->ip);569 this->peers[userId].handshake->setProxy1Address( pi->ip); 497 570 } 498 571 pi = this->networkMonitor->getSecondChoiceProxy(); 499 572 if( pi != NULL) 500 peers[userId].handshake->setProxy2Address( pi->ip);573 this->peers[userId].handshake->setProxy2Address( pi->ip); 501 574 502 575 // check if the connecting client should reconnect to a proxy server 503 576 if( SharedNetworkData::getInstance()->isMasterServer()) 504 peers[userId].handshake->setRedirect(/*this->networkMonitor->isReconnectNextClient()*/false); 577 { 578 if( this->networkMonitor->isReconnectNextClient()) 579 { 580 this->peers[userId].handshake->setRedirect(true); 581 PRINTF(0)("forwarding client to proxy server because this server is saturated\n"); 582 } 583 } 505 584 506 585 // the connecting node of course is a client 507 peers[userId].ip = peers[userId].socket->getRemoteAddress(); 508 } 586 this->peers[userId].ip = this->peers[userId].socket->getRemoteAddress(); 587 } 588 589 590 /** 591 * this handles new soft connections 592 * @param userId: the id of the new user node 593 * 594 * soft connections are connections from clients that are already in the network and don't need a new handshake etc. 595 * the state of all entitites owned by userId are not deleted and stay 596 * soft connections can not be redirected therefore they are negotiated between the to parties 597 */ 598 void NetworkStream::handleSoftConnect( int userId) 599 { 600 // create new handshake and init its variables 601 this->peers[userId].handshake = NULL; 602 this->peers[userId].handshake->setUniqueID(userId); 603 604 this->peers[userId].connectionMonitor = new ConnectionMonitor( userId ); 605 this->peers[userId].userId = userId; 606 this->peers[userId].bLocal = true; 607 608 PRINTF(0)("num sync: %d\n", synchronizeables.size()); 609 610 // the connecting node of course is a client 611 this->peers[userId].ip = this->peers[userId].socket->getRemoteAddress(); 612 } 613 509 614 510 615 … … 524 629 PRINT(0)(" Host ist Client with ID: %i\n", this->pInfo->userId); 525 630 } 631 632 PRINT(0)(" Current number of connections is: %i\n", this->peers.size()); 633 for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) 634 { 635 PRINT(0)(" peers[%i] with uniqueId %i and address: %s\n", it->first, it->second.userId, it->second.ip.ipString().c_str()); 636 } 637 PRINT(0)("\n\n"); 638 526 639 527 640 PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); … … 576 689 // - client <==> master server 577 690 // - proxy server <==> master server 578 if( SharedNetworkData::getInstance()->isClient() || SharedNetworkData::getInstance()->isProxyServerActive() && it->second.isMasterServer()) 691 if( SharedNetworkData::getInstance()->isClient() || 692 SharedNetworkData::getInstance()->isProxyServerActive() && 693 SharedNetworkData::getInstance()->isUserMasterServer(it->second.userId)) 579 694 { 580 PRINTF( 0)("Handshake: i am in client role\n");695 PRINTF(4)("Handshake: i am in client role\n"); 581 696 582 697 SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); 583 698 this->pInfo->userId = SharedNetworkData::getInstance()->getHostID(); 584 699 585 #warning the ip address is not set here because it results in a segfault when connecting to a proxy server => trace this later586 700 // it->second.ip = it->second.socket->getRemoteAddress(); 587 701 588 //it->second.nodeType = it->second.handshake->getRemoteNodeType();702 it->second.nodeType = it->second.handshake->getRemoteNodeType(); 589 703 // it->second.ip = it->second.socket->getRemoteAddress(); 590 704 // add the new server to the nodes list (it can be a NET_MASTER_SERVER or NET_PROXY_SERVER) … … 633 747 else if ( SharedNetworkData::getInstance()->isProxyServerActive() && it->second.isClient() ) 634 748 { 635 PRINTF( 0)("Handshake: i am in server role\n");749 PRINTF(4)("Handshake: Proxy in server role: connecting %i\n", it->second.userId); 636 750 637 751 it->second.ip = it->second.socket->getRemoteAddress(); … … 639 753 this->networkMonitor->addNode(&it->second); 640 754 641 this->handleNewClient( it->second.userId ); 755 // work with the ProxyControl to init the new client 756 ProxyControl::getInstance()->signalNewClient( it->second.userId); 642 757 643 758 if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) … … 667 782 /** 668 783 * this functions handles a reconnect event received from the a NET_MASTER_SERVER or NET_PROXY_SERVER 784 * @param userId 669 785 */ 670 786 void NetworkStream::handleReconnect(int userId) 671 787 { 672 788 this->bRedirect = false; 789 #warning this peer will be created if it does not yet exist: dangerous 673 790 PeerInfo* pInfo = &this->peers[userId]; 791 792 IP proxyIP; 793 if( this->networkMonitor->isForcedReconnection()) 794 proxyIP = this->networkMonitor->getForcedReconnectionIP(); 795 else 796 proxyIP = pInfo->handshake->getProxy1Address(); 674 797 675 798 PRINTF(0)("===============================================\n"); 676 799 PRINTF(0)("Client is redirected to the other proxy servers\n"); 677 800 PRINTF(0)(" user id: %i\n", userId); 678 PRINTF(0)(" connecting to: %s\n", this->networkMonitor->getFirstChoiceProxy()->ip.ipString().c_str());801 PRINTF(0)(" connecting to: %s\n", proxyIP.ipString().c_str()); 679 802 PRINTF(0)("===============================================\n"); 680 803 … … 683 806 pInfo->lastRecvedState = 0; 684 807 685 // temp save the ip address here686 IP proxyIP = pInfo->handshake->getProxy1Address();687 688 808 // disconnect from the current server and reconnect to proxy server 689 809 this->handleDisconnect( userId); 690 810 this->connectToProxyServer(NET_ID_PROXY_SERVER_01, proxyIP.ipString(), 9999); 811 // this->connectToMasterServer(proxyIP.ipString(), 9999); 691 812 #warning the ports are not yet integrated correctly in the ip class 692 813 … … 696 817 697 818 819 820 /** 821 * reconnects to another server, with full handshake 822 * @param address of the new server 823 */ 824 void NetworkStream::reconnectToServer(IP address) 825 { 826 ///TODO make a redirection struct and push it to the network monitor 827 this->networkMonitor->setForcedReconnection(address); 828 829 // reconnect (depending on how we are connected at the moment) 830 if ( peers.find( NET_ID_MASTER_SERVER) != peers.end() ) 831 this->redirectionUID = NET_ID_MASTER_SERVER; 832 else if( peers.find( NET_ID_PROXY_SERVER_01) != peers.end() ) 833 this->redirectionUID = NET_ID_PROXY_SERVER_01; 834 835 this->bRedirect = true; 836 } 837 838 839 840 /** 841 * softly reconnecting to another server 842 * @param serverUserId the id of the client 843 * @param address of the new server 844 */ 845 void NetworkStream::softReconnectToServer(int serverUserId, IP address) 846 { 847 // this->networkMonitor->setForcedReconnection(address); 848 // this->handleReconnect( NET_ID_MASTER_SERVER); 849 850 // create the new udp socket and open the connection to the soft connection port 851 NetworkSocket* newSocket = new UdpSocket(address.ipString(), 10001); 852 853 // delete the synchronization state of this client for all syncs 854 for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) { 855 (*it2)->cleanUpUser( serverUserId ); 856 } 857 858 // temp save the old socket 859 NetworkSocket* oldSocket = this->peers[serverUserId].socket; 860 861 // now integrate the new socket 862 this->peers[serverUserId].socket = newSocket; 863 864 this->bSoftRedirect = false; 865 return; 866 867 // now remove the old socket 868 oldSocket->disconnectServer(); 869 delete oldSocket; 870 871 // replace the old connection monitor 872 if ( this->peers[serverUserId].connectionMonitor ) 873 delete this->peers[serverUserId].connectionMonitor; 874 this->peers[serverUserId].connectionMonitor = new ConnectionMonitor(serverUserId); 875 876 // remove old node from the network monitor 877 this->networkMonitor->removeNode(&this->peers[serverUserId]); 878 879 } 880 881 882 883 /** 884 * prepares a soft connection for a client to connect to 885 * @param userId that will connect to this server 886 */ 887 void NetworkStream::prepareSoftConnection(int userId) 888 { 889 PRINTF(0)("prepare soft connection for userId %i\n"); 890 } 891 892 893 698 894 /** 699 895 * handles the disconnect event … … 702 898 void NetworkStream::handleDisconnect( int userId ) 703 899 { 704 peers[userId].socket->disconnectServer();705 delete peers[userId].socket;706 peers[userId].socket = NULL;707 708 if ( peers[userId].handshake )709 delete peers[userId].handshake;710 peers[userId].handshake = NULL;711 712 if ( peers[userId].connectionMonitor )713 delete peers[userId].connectionMonitor;714 peers[userId].connectionMonitor = NULL;715 716 900 this->peers[userId].socket->disconnectServer(); 901 delete this->peers[userId].socket; 902 this->peers[userId].socket = NULL; 903 904 if ( this->peers[userId].handshake ) 905 delete this->peers[userId].handshake; 906 this->peers[userId].handshake = NULL; 907 908 if ( this->peers[userId].connectionMonitor ) 909 delete this->peers[userId].connectionMonitor; 910 this->peers[userId].connectionMonitor = NULL; 911 912 // delete the synchronization state of this client for all syncs 717 913 for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) { 718 914 (*it2)->cleanUpUser( userId ); … … 724 920 this->freeSocketSlots.push_back( userId ); 725 921 726 peers.erase( userId);727 } 728 922 this->networkMonitor->removeNode(&this->peers[userId]); 923 this->peers.erase( userId); 924 } 729 925 730 926 … … 793 989 assert( offset + INTSIZE <= UDP_PACKET_SIZE ); 794 990 795 // server fakes uniqueid == 0 for handshake 991 // server fakes uniqueid == 0 for handshake synchronizeable 796 992 if ( ( SharedNetworkData::getInstance()->isMasterServer() || 797 993 SharedNetworkData::getInstance()->isProxyServerActive() && peer->second.isClient() ) && 798 sync.getUniqueID() <= SharedNetworkData::getInstance()->getMaxPlayer() + 1) // plus one to handle one client more than the max to redirect it994 ( sync.getUniqueID() >= 1000 || sync.getUniqueID() <= SharedNetworkData::getInstance()->getMaxPlayer() + 1 + NET_ID_PROXY_MAX)) 799 995 n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); 800 996 else … … 930 1126 while ( offset + 2 * INTSIZE < length ) 931 1127 { 1128 // read the unique id of the sync 932 1129 assert( offset > 0 ); 933 1130 assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); 934 1131 offset += INTSIZE; 935 1132 1133 // read the data length 936 1134 assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE ); 937 1135 offset += INTSIZE; … … 945 1143 for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) 946 1144 { 947 // client thinks his handshake has id 0!!!!! 948 if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) 1145 // client thinks his handshake has a special id: hostId * 1000 (host id of this server) 1146 if ( (*it)->getUniqueID() == uniqueId || // so this client exists already go to sync work 1147 ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) // so this is a Handshake! 949 1148 { 950 1149 sync = *it;
Note: See TracChangeset
for help on using the changeset viewer.