Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 16, 2011, 2:55:40 PM (12 years ago)
Author:
smerkli
Message:

merged masterserverfix corretly now.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/network/MasterServer.cc

    r8858 r8937  
    2929#include "MasterServer.h"
    3030#include "util/ScopedSingletonManager.h"
     31#include "core/command/ConsoleCommand.h"
    3132#include "core/CoreIncludes.h"
    3233#include "core/CorePrereqs.h"
     34#include "util/Output.h"
    3335
    3436namespace orxonox
    3537{
     38  /*** MACROS ***/
     39  /* commands for the terminal interface */
     40  SetConsoleCommand( "ms-listservers", &MasterServer::listServers );
     41  SetConsoleCommand( "ms-delserver", &MasterServer::delServer );
     42  //SetConsoleCommand( "ms-serverinfo", &MasterServer::serverInfo );
     43
     44  /* forward declaration so the linker doesn't complain */
     45  MasterServer *MasterServer::instance = NULL;
     46
     47
     48
     49
     50  /* command: list servers */
     51  void
     52  MasterServer::listServers( void )
     53  {
     54    /* get an iterator */
     55    std::list<ServerListElem>::iterator i;
     56
     57    /* print list header */
     58    orxout(user_info) << "List of connected servers" << std::endl;
     59
     60    /* loop through list elements */
     61    for( i = MasterServer::getInstance()->mainlist.serverlist.begin();
     62      i != MasterServer::getInstance()->mainlist.serverlist.end(); ++i )
     63    {
     64      orxout(user_info) << "  " << (*i).ServerInfo.getServerIP() << std::endl;
     65    }
     66
     67    /* display end of list */
     68    orxout(user_info) << MasterServer::getInstance()->mainlist.serverlist.size() <<
     69      " servers connected." << std::endl;
     70  }
     71
     72  void
     73  MasterServer::delServer( std::string todeladdr )
     74  {
     75    /* tell the user we're now removing the entry from the server list */
     76    orxout(user_info) << "MS: Deleting server \"" << todeladdr << "\"..."
     77      << std::endl;
     78
     79    /* see if we actually have that server on our list */
     80    ServerListSearchResult shandle =
     81      MasterServer::getInstance()->mainlist.findServerByAddress(todeladdr);
     82
     83    if( !shandle.success )
     84    { orxout(user_info) << "MS: Server not found, not removing." << std::endl;
     85      return;
     86    }
     87
     88    /* force-disconnect the server */ 
     89    enet_peer_disconnect( shandle.result.peer, NULL );
     90
     91    /* actually remove the entry from the server list by address */
     92    MasterServer::getInstance()->mainlist.delServerByAddress( todeladdr);
     93
     94    /* tell the user about our success */
     95    orxout(user_info) << "MS: Server deletion successful." << std::endl;
     96  }
     97
     98
    3699  /* helpers */
    37100  static void
     
    53116  {
    54117    /* get an iterator */
    55     std::list<packet::ServerInformation>::iterator i;
     118    std::list<ServerListElem>::iterator i;
    56119
    57120    /* packet holder */
     
    64127      /* send this particular server */
    65128      /* build reply string */
    66       char *tosend = (char *)calloc( (*i).getServerIP().length()
     129      char *tosend = (char *)calloc( (*i).ServerInfo.getServerIP().length()
    67130          + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 );
    68131      if( !tosend )
     
    71134      }
    72135      sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM,
    73           (*i).getServerIP().c_str() );
     136          (*i).ServerInfo.getServerIP().c_str() );
    74137
    75138      /* create packet from it */
     
    98161    /* One could just use enet_host_service() instead. */
    99162    enet_host_flush( this->server );
     163  }
     164
     165  /* maybe the two methods below can be merged into one and
     166   * made to use ENet's RTT functionality to check for disconnected
     167   * servers.
     168   */
     169  void
     170  MasterServer::helper_cleanupServers( void )
     171  {
     172    /* get an iterator */
     173    std::list<ServerListElem>::iterator i;
     174     
     175    if( mainlist.serverlist.size() == 0 )
     176      return;
     177
     178    /* loop through list elements */
     179    for( i = mainlist.serverlist.begin(); i
     180        != mainlist.serverlist.end(); ++i )
     181    { /* see if we have a disconnected peer */
     182      if( (*i).peer &&
     183         ((*i).peer->state == ENET_PEER_STATE_DISCONNECTED ||
     184          (*i).peer->state == ENET_PEER_STATE_ZOMBIE ))
     185      {
     186        /* Remove it from the list */
     187        orxout(internal_warning) << (char*)(*i).peer->data << " timed out.\n";
     188        mainlist.delServerByName( (*i).ServerInfo.getServerName() );
     189
     190        /* stop iterating, we manipulated the list */
     191        /* TODO note: this only removes one dead server per loop
     192         * iteration. not beautiful, but one iteration is ~100ms,
     193         * so not really relevant for the moment.
     194         */
     195        break;
     196      }
     197    }
     198 
    100199  }
    101200
     
    180279        MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) )
    181280      { /* register new server */
    182         mainlist.addServer( packet::ServerInformation( event ) );
     281        mainlist.addServer( packet::ServerInformation( event ),
     282          event->peer );
    183283       
    184284        /* tell people we did so */
     
    235335    }
    236336
    237     /* TODO schedule pings for servers somewhere here */
    238    
     337    /* check for timed out peers and remove those from * the server list */
     338    helper_cleanupServers();
     339
     340
    239341    /* create an iterator for the loop */
    240342    enet_host_service( this->server, event, 100 );
     
    291393    }
    292394
    293     /***** INITIALIZE GAME SERVER AND PEER LISTS *****/
    294     this->peers = new PeerList();
     395    /* set pointer to this instance */
     396    MasterServer::setInstance( this );
    295397
    296398    /* tell people we're now initialized */
Note: See TracChangeset for help on using the changeset viewer.