Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 20, 2006, 11:43:27 AM (19 years ago)
Author:
bensch
Message:

orxonox/proxy: merged the proxy.old back again, and it seems to work.

Merged with command
svn merge -r9247:HEAD https://svn.orxonox.net/orxonox/branches/proxy.old .

no conflicts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/proxy/src/lib/network/network_stream.cc

    r9246 r9347  
    1212   main-programmer: Christoph Renner rennerc@ee.ethz.ch
    1313   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)
    1417*/
    1518
     
    2225#include "udp_socket.h"
    2326#include "udp_server_socket.h"
    24 #include "connection_monitor.h"
     27#include "monitor/connection_monitor.h"
     28#include "monitor/network_monitor.h"
    2529#include "synchronizeable.h"
     30#include "ip.h"
    2631#include "network_game_manager.h"
    2732#include "shared_network_data.h"
     
    4247#include <algorithm>
    4348
    44 /* include your own header */
     49
    4550#include "network_stream.h"
    4651
    47 /* probably unnecessary */
     52
    4853using namespace std;
    4954
     
    5257
    5358
     59/**
     60 * empty constructor
     61 */
    5462NetworkStream::NetworkStream()
    5563    : DataStream()
     
    5765  this->init();
    5866  /* initialize the references */
    59   this->type = NET_CLIENT;
    60 }
    61 
    62 
    63 /**
    64  * connect to a server as a client
     67  this->pInfo->nodeType = NET_CLIENT;
     68}
     69
     70
     71/**
     72 * start as a client, connect to a server
    6573 *  @param host: host name (address)
    6674 *  @param port: port number
     
    6876NetworkStream::NetworkStream( std::string host, int port )
    6977{
    70   this->type = NET_CLIENT;
    7178  this->init();
     79  // init the peers[0] the server to ceonnect to
    7280  this->peers[0].socket = new UdpSocket( host, port );
    7381  this->peers[0].userId = 0;
    74   this->peers[0].isServer = true;
     82  this->peers[0].nodeType = NET_MASTER_SERVER;
    7583  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;
    7691}
    7792
     
    8398NetworkStream::NetworkStream( int port )
    8499{
    85   this->type = NET_SERVER;
    86100  this->init();
    87101  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;
    89107}
    90108
     
    97115  /* set the class id for the base object */
    98116  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
    99   this->bActive = false;
    100117  this->serverSocket = NULL;
    101118  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;
    104128
    105129  remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 );
     
    147171  for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ )
    148172    (*it)->setNetworkStream( NULL );
     173
     174  if( this->pInfo)
     175    delete this->pInfo;
     176
     177  if( this->networkMonitor)
     178    delete this->networkMonitor;
    149179}
    150180
     
    165195/**
    166196 * 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
    167199 */
    168200void NetworkStream::startHandshake()
    169201{
    170   Handshake* hs = new Handshake(false);
     202  Handshake* hs = new Handshake(this->pInfo->nodeType);
    171203  hs->setUniqueID( 0 );
    172204  assert( peers[0].handshake == NULL );
    173205  peers[0].handshake = hs;
    174206
     207  // set the preferred nick name
    175208  hs->setPreferedNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Player" ) );
    176209
    177 //   peers[0].handshake->setSynchronized( true );
    178   //this->connectSynchronizeable(*hs);
    179   //this->connectSynchronizeable(*hs);
    180210  PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName());
    181211}
     
    192222  sync.setNetworkStream( this );
    193223
    194   this->bActive = true;
     224//   this->bActive = true;
    195225}
    196226
     
    215245void NetworkStream::processData()
    216246{
     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
    217252  int tick = SDL_GetTicks();
    218253
    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
    223264    if ( serverSocket )
    224265      serverSocket->update();
    225266
    226267    this->updateConnectionList();
     268  }
     269  else if( this->pInfo->nodeType == NET_PROXY_SERVER_ACTIVE)
     270  {
     271    // execute everything the proxy server should do
    227272  }
    228273  else
     
    249294  cleanUpOldSyncList();
    250295  handleHandshakes();
     296
     297  // update the network monitor
     298  this->networkMonitor->process();
    251299
    252300  // order of up/downstream is important!!!!
     
    258306
    259307/**
    260  * if we are a server update the connection list to accept new connections (clients)
    261  * also start the handsake for the new clients
     308 * 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
    262310 */
    263311void NetworkStream::updateConnectionList( )
     
    276324      clientId = freeSocketSlots.back();
    277325      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;
    284326    }
    285327    else
     
    290332        if ( it->first >= clientId )
    291333          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
    315382  }
    316383
     
    373440void NetworkStream::debug()
    374441{
    375   if( this->isServer())
    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);
    377444  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);
    379446
    380447  PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size());
     
    385452               (*it)->getUniqueID(), (*it)->beSynchronized());
    386453  }
    387   PRINT(0)(" Maximal Connections: %i\n", MAX_CONNECTIONS );
     454  PRINT(0)(" Maximal Connections: %i\n", SharedNetworkData::getInstance()->getMaxPlayer() );
    388455
    389456}
     
    414481    if ( it->second.handshake )
    415482    {
     483      // handshake finished
    416484      if ( it->second.handshake->completed() )
    417485      {
     486        //handshake is correct
    418487        if ( it->second.handshake->ok() )
    419488        {
    420489          if ( !it->second.handshake->allowDel() )
    421490          {
    422             if ( type != NET_SERVER )
     491            if ( this->pInfo->nodeType == NET_CLIENT )
    423492            {
    424493              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
    427511              this->networkGameManager = NetworkGameManager::getInstance();
    428512              this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() );
     513              // init the new message manager
    429514              MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() );
    430515            }
     
    437522          else
    438523          {
     524            // handshake finished registring new player
    439525            if ( it->second.handshake->canDel() )
    440526            {
    441               if ( type == NET_SERVER )
     527              if ( this->pInfo->nodeType == NET_MASTER_SERVER )
    442528              {
    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 );
    444535
    445536                if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" )
     
    468559
    469560/**
     561 * this functions handles a reconnect event received from the a NET_MASTER_SERVER or NET_PROXY_SERVER
     562 */
     563void 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/**
    470590 * handle upstream network traffic
    471591 */
     
    513633
    514634      // if we are a server and this is not our handshake
    515       if ( isServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId )
     635      if ( isMasterServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId )
    516636        continue;
    517637
     
    524644
    525645      // server fakes uniqueid == 0 for handshake
    526       if ( this->isServer() && sync.getUniqueID() < MAX_CONNECTIONS - 1 )
     646      if ( this->isMasterServer() && sync.getUniqueID() < SharedNetworkData::getInstance()->getMaxPlayer() - 1 )
    527647        n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset );
    528648      else
     
    573693    // now compress the data with the zip library
    574694    int compLength = 0;
    575     if ( this->isServer() )
     695    if ( this->isMasterServer() )
    576696      compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer );
    577697    else
     
    637757      assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE );
    638758      assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE );
    639       //NETPRINTF(n)("ackedstate: %d\n", ackedState);
    640759      offset = 4*INTSIZE;
    641760
    642761      peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, packetLength, state, ackedState );
    643762
    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
    647765      if ( state <= peer->second.lastRecvedState )
    648766        continue;
     
    688806          }
    689807
    690           if ( !peers[peer->second.userId].isServer )
     808          if ( !peers[peer->second.userId].isMasterServer() )
    691809          {
    692810            offset += syncDataLength;
     
    779897/**
    780898 * is executed when a handshake has finished
    781  * @todo create playable for new user
    782899 */
    783900void NetworkStream::handleNewClient( int userId )
    784901{
     902  // init and assign the message manager
    785903  MessageManager::getInstance()->initUser( userId );
    786 
     904  // do all game relevant stuff here
    787905  networkGameManager->signalNewPlayer( userId );
    788 }
     906
     907  // register the new client at the network monitor
     908//   this->networkMonitor->addClient();
     909}
     910
    789911
    790912/**
Note: See TracChangeset for help on using the changeset viewer.