Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 23, 2006, 10:07:23 PM (19 years ago)
Author:
patrick
Message:

committing my weekends work: 2100 lines :D

  • proxy server now accepts and synchronizes clients like a master server
  • network manager got different network setup interface
  • network stream got different constructure scheme
  • permissions checking and algorithm extended and changed
  • starting ability to connect to multiple network nodes at the same time
  • some very much smaller changes here and there
File:
1 edited

Legend:

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

    r9386 r9396  
    7070
    7171
    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 )
     72NetworkStream::NetworkStream( int nodeType)
    7873{
    7974  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
    88103  // get the local ip address
    89104  IPaddress ip;
    90   SDLNet_ResolveHost( &ip, NULL, port );
     105  SDLNet_ResolveHost( &ip, NULL, 0);
    91106  this->pInfo->ip = ip;
    92107}
    93108
    94 
    95 /**
    96  * start as a server
    97  *  @param port: at this port
    98  */
    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 address
    105   IPaddress ip;
    106   SDLNet_ResolveHost( &ip, NULL, port );
    107   this->pInfo->ip = ip;
    108 }
    109109
    110110
     
    182182
    183183/**
     184 * establish a connection to a remote master server
     185 * @param host: host name
     186 * @param port: the port number
     187 */
     188void 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 */
     205void 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 */
     221void NetworkStream::createServer(int port)
     222{
     223  this->serverSocket = new UdpServerSocket(port);
     224}
     225
     226
     227/**
    184228 * creates a new instance of the network game manager
    185229 */
     
    187231{
    188232  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
    191234  this->networkGameManager->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
    192235  MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
     
    260303  }
    261304
    262   if ( this->pInfo->nodeType == NET_MASTER_SERVER )
     305  if ( this->pInfo->isMasterServer())
    263306  {
    264307    // execute everytthing the master server shoudl do
     
    268311    this->updateConnectionList();
    269312  }
    270   else if( this->pInfo->nodeType == NET_PROXY_SERVER_ACTIVE)
     313  else if( this->pInfo->isProxyServer())
    271314  {
    272315    // execute everything the proxy server should do
     316    if ( serverSocket )
     317      serverSocket->update();
     318
     319    this->updateConnectionList();
    273320  }
    274321  else
     
    442489void NetworkStream::debug()
    443490{
    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 {
    447498    PRINT(0)(" Host ist Client with ID: %i\n", this->pInfo->userId);
     499  }
    448500
    449501  PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size());
     
    493545          {
    494546
    495             if ( this->pInfo->nodeType == NET_CLIENT )
     547            if ( this->pInfo->isClient() )
    496548            {
    497 
    498549              SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() );
    499550              this->pInfo->userId = SharedNetworkData::getInstance()->getHostID();
     
    528579            if ( it->second.handshake->canDel() )
    529580            {
    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() )
    531597              {
    532598                it->second.nodeType = it->second.handshake->getRemoteNodeType();
     
    639705        continue;
    640706
    641       // if we are a server and this is not our handshake
    642       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 )
    643709        continue;
    644710
     
    654720
    655721      // 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 )
    657723        n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset );
    658724      else
     
    703769    // now compress the data with the zip library
    704770    int compLength = 0;
    705     if ( this->isMasterServer() )
     771    if ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer())
    706772      compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer );
    707773    else
     
    783849      }
    784850
    785       while ( offset + 2*INTSIZE < length )
     851      while ( offset + 2 * INTSIZE < length )
    786852      {
    787853        assert( offset > 0 );
     
    797863        Synchronizeable * sync = NULL;
    798864
     865        // look for the synchronizeable in question
    799866        for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
    800867        {
     
    807874        }
    808875
     876        // this synchronizeable does not yet exist! create it
    809877        if ( sync == NULL )
    810878        {
    811879          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
    812882          if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() )
    813883          {
     
    816886          }
    817887
    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() )
    819890          {
    820891            offset += syncDataLength;
     
    833904          assert( leafClassId != 0 );
    834905
     906
    835907          BaseObject * b = NULL;
    836908          /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
     
    838910          if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE || leafClassId == CL_NETWORK_GAME_MANAGER )
    839911          {
    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);
    841913            offset += syncDataLength;
    842914            continue;
     
    913985  // do all game relevant stuff here
    914986  networkGameManager->signalNewPlayer( userId );
    915 
    916   // register the new client at the network monitor
    917 //   this->networkMonitor->addClient();
    918987}
    919988
Note: See TracChangeset for help on using the changeset viewer.