Changeset 9347 in orxonox.OLD for branches/proxy/src/lib/network/network_stream.cc
- Timestamp:
- Jul 20, 2006, 11:43:27 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/proxy/src/lib/network/network_stream.cc
r9246 r9347 12 12 main-programmer: Christoph Renner rennerc@ee.ethz.ch 13 13 co-programmer: Patrick Boenzli boenzlip@orxonox.ethz.ch 14 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) 14 17 */ 15 18 … … 22 25 #include "udp_socket.h" 23 26 #include "udp_server_socket.h" 24 #include "connection_monitor.h" 27 #include "monitor/connection_monitor.h" 28 #include "monitor/network_monitor.h" 25 29 #include "synchronizeable.h" 30 #include "ip.h" 26 31 #include "network_game_manager.h" 27 32 #include "shared_network_data.h" … … 42 47 #include <algorithm> 43 48 44 /* include your own header */ 49 45 50 #include "network_stream.h" 46 51 47 /* probably unnecessary */ 52 48 53 using namespace std; 49 54 … … 52 57 53 58 59 /** 60 * empty constructor 61 */ 54 62 NetworkStream::NetworkStream() 55 63 : DataStream() … … 57 65 this->init(); 58 66 /* initialize the references */ 59 this-> type = NET_CLIENT;60 } 61 62 63 /** 64 * connect to a server as a client67 this->pInfo->nodeType = NET_CLIENT; 68 } 69 70 71 /** 72 * start as a client, connect to a server 65 73 * @param host: host name (address) 66 74 * @param port: port number … … 68 76 NetworkStream::NetworkStream( std::string host, int port ) 69 77 { 70 this->type = NET_CLIENT;71 78 this->init(); 79 // init the peers[0] the server to ceonnect to 72 80 this->peers[0].socket = new UdpSocket( host, port ); 73 81 this->peers[0].userId = 0; 74 this->peers[0]. isServer = true;82 this->peers[0].nodeType = NET_MASTER_SERVER; 75 83 this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); 84 this->peers[0].ip = this->peers[0].socket->getRemoteAddress(); 85 // and set also the localhost 86 this->pInfo->nodeType = NET_CLIENT; 87 // get the local ip address 88 IPaddress ip; 89 SDLNet_ResolveHost( &ip, NULL, port ); 90 this->pInfo->ip = ip; 76 91 } 77 92 … … 83 98 NetworkStream::NetworkStream( int port ) 84 99 { 85 this->type = NET_SERVER;86 100 this->init(); 87 101 this->serverSocket = new UdpServerSocket(port); 88 this->bActive = true; 102 this->pInfo->nodeType = NET_MASTER_SERVER; 103 // get the local ip address 104 IPaddress ip; 105 SDLNet_ResolveHost( &ip, NULL, port ); 106 this->pInfo->ip = ip; 89 107 } 90 108 … … 97 115 /* set the class id for the base object */ 98 116 this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); 99 this->bActive = false;100 117 this->serverSocket = NULL; 101 118 this->networkGameManager = NULL; 102 myHostId = 0; 103 currentState = 0; 119 this->networkMonitor = NULL; 120 121 this->pInfo = new PeerInfo(); 122 this->pInfo->userId = 0; 123 this->pInfo->lastAckedState = 0; 124 this->pInfo->lastRecvedState = 0; 125 126 127 this->currentState = 0; 104 128 105 129 remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 ); … … 147 171 for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ ) 148 172 (*it)->setNetworkStream( NULL ); 173 174 if( this->pInfo) 175 delete this->pInfo; 176 177 if( this->networkMonitor) 178 delete this->networkMonitor; 149 179 } 150 180 … … 165 195 /** 166 196 * starts the network handshake 197 * handsakes are always initialized from the client side first. this starts the handshake and therefore is only 198 * executed as client 167 199 */ 168 200 void NetworkStream::startHandshake() 169 201 { 170 Handshake* hs = new Handshake( false);202 Handshake* hs = new Handshake(this->pInfo->nodeType); 171 203 hs->setUniqueID( 0 ); 172 204 assert( peers[0].handshake == NULL ); 173 205 peers[0].handshake = hs; 174 206 207 // set the preferred nick name 175 208 hs->setPreferedNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Player" ) ); 176 209 177 // peers[0].handshake->setSynchronized( true );178 //this->connectSynchronizeable(*hs);179 //this->connectSynchronizeable(*hs);180 210 PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName()); 181 211 } … … 192 222 sync.setNetworkStream( this ); 193 223 194 this->bActive = true;224 // this->bActive = true; 195 225 } 196 226 … … 215 245 void NetworkStream::processData() 216 246 { 247 // create the network monitor after all the init work and before there is any connection handlings 248 if( this->networkMonitor == NULL) 249 this->networkMonitor = new NetworkMonitor(this); 250 251 217 252 int tick = SDL_GetTicks(); 218 253 219 currentState++; 220 221 if ( this->type == NET_SERVER ) 222 { 254 this->currentState++; 255 // there was a wrap around 256 if( this->currentState < 0) 257 { 258 PRINTF(1)("A wrap around in the state variable as occured. The server was running so long? Pls restart server or write a mail to the supporters!\n"); 259 } 260 261 if ( this->pInfo->nodeType == NET_MASTER_SERVER ) 262 { 263 // execute everytthing the master server shoudl do 223 264 if ( serverSocket ) 224 265 serverSocket->update(); 225 266 226 267 this->updateConnectionList(); 268 } 269 else if( this->pInfo->nodeType == NET_PROXY_SERVER_ACTIVE) 270 { 271 // execute everything the proxy server should do 227 272 } 228 273 else … … 249 294 cleanUpOldSyncList(); 250 295 handleHandshakes(); 296 297 // update the network monitor 298 this->networkMonitor->process(); 251 299 252 300 // order of up/downstream is important!!!! … … 258 306 259 307 /** 260 * if we are a server update the connection list to accept new connections (clients)261 * also start the handsake for the new clients308 * if we are a NET_MASTER_SERVER or NET_PROXY_SERVER_ACTIVE update the connection list to accept new 309 * connections (clients) also start the handsake for the new clients 262 310 */ 263 311 void NetworkStream::updateConnectionList( ) … … 276 324 clientId = freeSocketSlots.back(); 277 325 freeSocketSlots.pop_back(); 278 peers[clientId].socket = tempNetworkSocket;279 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() );280 peers[clientId].connectionMonitor = new ConnectionMonitor( clientId );281 peers[clientId].handshake->setUniqueID(clientId);282 peers[clientId].userId = clientId;283 peers[clientId].isServer = false;284 326 } 285 327 else … … 290 332 if ( it->first >= clientId ) 291 333 clientId = it->first + 1; 292 293 peers[clientId].socket = tempNetworkSocket; 294 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); 295 peers[clientId].handshake->setUniqueID(clientId); 296 peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); 297 peers[clientId].userId = clientId; 298 peers[clientId].isServer = false; 299 300 PRINTF(0)("num sync: %d\n", synchronizeables.size()); 301 } 302 303 // check if there are too many clients connected 304 if ( clientId > MAX_CONNECTIONS ) 305 { 306 peers[clientId].handshake->doReject( "too many connections" ); 307 PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); 308 } 309 else 310 { 311 PRINTF(0)("New Client: %d\n", clientId); 312 } 313 314 //this->connectSynchronizeable(*handshakes[clientId]); 334 } 335 336 peers[clientId].socket = tempNetworkSocket; 337 // create new handshake and init its variables 338 peers[clientId].handshake = new Handshake(this->pInfo->nodeType, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); 339 peers[clientId].handshake->setUniqueID(clientId); 340 341 peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); 342 peers[clientId].userId = clientId; 343 344 PRINTF(0)("num sync: %d\n", synchronizeables.size()); 345 346 // get the proxy server informations and write them to the handshake, if any (proxy) 347 assert( this->networkMonitor != NULL); 348 PeerInfo* pi = this->networkMonitor->getFirstChoiceProxy(); 349 if( pi != NULL) 350 { 351 peers[clientId].handshake->setProxy1Address( pi->ip); 352 PRINTF(0)("proxy1 ip set to: %s\n", pi->ip.ipString().c_str()); 353 } 354 355 pi = this->networkMonitor->getSecondChoiceProxy(); 356 if( pi != NULL) 357 peers[clientId].handshake->setProxy2Address( pi->ip); 358 359 // check if the connecting client should reconnect to a proxy server 360 peers[clientId].handshake->setRedirect(this->networkMonitor->isReconnectNextClient()); 361 362 // the connecting node of course is a client 363 peers[clientId].nodeType = NET_CLIENT; 364 peers[clientId].ip = peers[clientId].socket->getRemoteAddress(); 365 366 367 // check if there are too many clients connected (DEPRECATED: new: the masterserver sends a list of proxy servers) 368 // if ( clientId > SharedNetworkData::getInstance()->getMaxPlayer() ) 369 // { 370 // // peers[clientId].handshake->setRedirect(true); 371 // // 372 // // peers[clientId].handshake->doReject( "too many connections" ); 373 // PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); 374 // } 375 // else 376 // { 377 // PRINTF(0)("New Client: %d\n", clientId); 378 // } 379 PRINTF(0)("New Client: %d\n", clientId); 380 381 315 382 } 316 383 … … 373 440 void NetworkStream::debug() 374 441 { 375 if( this->is Server())376 PRINT(0)(" Host ist Server with ID: %i\n", this-> myHostId);442 if( this->isMasterServer()) 443 PRINT(0)(" Host ist Server with ID: %i\n", this->pInfo->userId); 377 444 else 378 PRINT(0)(" Host ist Client with ID: %i\n", this-> myHostId);445 PRINT(0)(" Host ist Client with ID: %i\n", this->pInfo->userId); 379 446 380 447 PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); … … 385 452 (*it)->getUniqueID(), (*it)->beSynchronized()); 386 453 } 387 PRINT(0)(" Maximal Connections: %i\n", MAX_CONNECTIONS);454 PRINT(0)(" Maximal Connections: %i\n", SharedNetworkData::getInstance()->getMaxPlayer() ); 388 455 389 456 } … … 414 481 if ( it->second.handshake ) 415 482 { 483 // handshake finished 416 484 if ( it->second.handshake->completed() ) 417 485 { 486 //handshake is correct 418 487 if ( it->second.handshake->ok() ) 419 488 { 420 489 if ( !it->second.handshake->allowDel() ) 421 490 { 422 if ( t ype != NET_SERVER)491 if ( this->pInfo->nodeType == NET_CLIENT ) 423 492 { 424 493 SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); 425 myHostId = SharedNetworkData::getInstance()->getHostID(); 426 494 this->pInfo->userId = SharedNetworkData::getInstance()->getHostID(); 495 496 it->second.nodeType = it->second.handshake->getRemoteNodeType(); 497 it->second.ip = it->second.socket->getRemoteAddress(); 498 // add the new server to the nodes list (it can be a NET_MASTER_SERVER or NET_PROXY_SERVER) 499 this->networkMonitor->addNode(&it->second); 500 // get proxy 1 address and add it 501 this->networkMonitor->addNode(it->second.handshake->getProxy1Address(), NET_PROXY_SERVER_ACTIVE); 502 PRINTF(0)("proxy1 ip read to: %s\n", it->second.handshake->getProxy1Address().ipString().c_str()); 503 // get proxy 2 address and add it 504 this->networkMonitor->addNode(it->second.handshake->getProxy2Address(), NET_PROXY_SERVER_ACTIVE); 505 506 // now check if the server accepted the connection 507 if( it->second.handshake->redirect()) 508 this->handleReconnect( it->second.userId); 509 510 // create the new network game manager and init it 427 511 this->networkGameManager = NetworkGameManager::getInstance(); 428 512 this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); 513 // init the new message manager 429 514 MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); 430 515 } … … 437 522 else 438 523 { 524 // handshake finished registring new player 439 525 if ( it->second.handshake->canDel() ) 440 526 { 441 if ( t ype == NET_SERVER )527 if ( this->pInfo->nodeType == NET_MASTER_SERVER ) 442 528 { 443 handleNewClient( it->second.userId ); 529 it->second.nodeType = it->second.handshake->getRemoteNodeType(); 530 it->second.ip = it->second.socket->getRemoteAddress(); 531 532 this->networkMonitor->addNode(&it->second); 533 534 this->handleNewClient( it->second.userId ); 444 535 445 536 if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) … … 468 559 469 560 /** 561 * this functions handles a reconnect event received from the a NET_MASTER_SERVER or NET_PROXY_SERVER 562 */ 563 void NetworkStream::handleReconnect(int userId) 564 { 565 PRINTF(0)("===============================================\n"); 566 PRINTF(0)("Client is redirected to the other proxy servers\n"); 567 PRINTF(0)("===============================================\n"); 568 569 PeerInfo* pInfo = &this->peers[userId]; 570 571 // reject the server 572 pInfo->handshake->doReject( "redirected to different server"); 573 574 // flush the old synchronization states, since the numbering could be completely different 575 pInfo->lastAckedState = 0; 576 pInfo->lastRecvedState = 0; 577 // not sure if this works as expected 578 if( pInfo->handshake) 579 delete pInfo->handshake; 580 581 // disconnect from the current server and reconnect to proxy server 582 pInfo->socket->reconnectToServer( pInfo->handshake->getProxy1Address().ipString(), pInfo->handshake->getProxy1Address().port()); 583 584 // and restart the handshake 585 this->startHandshake(); 586 } 587 588 589 /** 470 590 * handle upstream network traffic 471 591 */ … … 513 633 514 634 // if we are a server and this is not our handshake 515 if ( is Server() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId )635 if ( isMasterServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) 516 636 continue; 517 637 … … 524 644 525 645 // server fakes uniqueid == 0 for handshake 526 if ( this->is Server() && sync.getUniqueID() < MAX_CONNECTIONS- 1 )646 if ( this->isMasterServer() && sync.getUniqueID() < SharedNetworkData::getInstance()->getMaxPlayer() - 1 ) 527 647 n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); 528 648 else … … 573 693 // now compress the data with the zip library 574 694 int compLength = 0; 575 if ( this->is Server() )695 if ( this->isMasterServer() ) 576 696 compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer ); 577 697 else … … 637 757 assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); 638 758 assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); 639 //NETPRINTF(n)("ackedstate: %d\n", ackedState);640 759 offset = 4*INTSIZE; 641 760 642 761 peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, packetLength, state, ackedState ); 643 762 644 //NETPRINTF(n)("got packet: %d, %d\n", length, packetLength); 645 646 //if this is an old state drop it 763 764 //if this is an old state drop it 647 765 if ( state <= peer->second.lastRecvedState ) 648 766 continue; … … 688 806 } 689 807 690 if ( !peers[peer->second.userId].is Server)808 if ( !peers[peer->second.userId].isMasterServer() ) 691 809 { 692 810 offset += syncDataLength; … … 779 897 /** 780 898 * is executed when a handshake has finished 781 * @todo create playable for new user782 899 */ 783 900 void NetworkStream::handleNewClient( int userId ) 784 901 { 902 // init and assign the message manager 785 903 MessageManager::getInstance()->initUser( userId ); 786 904 // do all game relevant stuff here 787 905 networkGameManager->signalNewPlayer( userId ); 788 } 906 907 // register the new client at the network monitor 908 // this->networkMonitor->addClient(); 909 } 910 789 911 790 912 /**
Note: See TracChangeset
for help on using the changeset viewer.