Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8935 for code


Ignore:
Timestamp:
Nov 16, 2011, 2:32:57 PM (13 years ago)
Author:
smerkli
Message:

merged changes from masterserver2 branch into newer trunk here.

Location:
code/branches/masterserverfix/src/libraries/network
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/masterserverfix/src/libraries/network/MasterServer.cc

    r8858 r8935  
    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 */
  • code/branches/masterserverfix/src/libraries/network/MasterServer.h

    r8351 r8935  
    6161      int run();
    6262
     63      /* static pointer for commands */
     64      static MasterServer *instance;
     65      static MasterServer *getInstance()
     66        { return instance; }
     67      static void setInstance( MasterServer *setto )
     68        { instance = setto;  }
     69     
     70      /* functions for commands */
     71      static void listServers( void );
     72      static void delServer( std::string todeladdr );
     73
    6374    private:
    6475      /* methods */
     
    6980      /* helpers */
    7081      void helper_sendlist( ENetEvent *event );
     82      void helper_cleanupServers( void );
    7183
    7284      /* members */
     
    7486      ENetHost *server;
    7587      ServerList mainlist;
    76       PeerList *peers;
    7788
    7889      unsigned int port;
  • code/branches/masterserverfix/src/libraries/network/ServerList.cc

    r8351 r8935  
    4040
    4141  int
    42   ServerList::addServer( packet::ServerInformation toadd )
    43   { this->serverlist.push_back( toadd );
     42  ServerList::addServer( packet::ServerInformation toadd,
     43    ENetPeer *peer )
     44  {
     45    ServerListElem toAdd;
     46    toAdd.ServerInfo = toadd;
     47    toAdd.peer = peer;
     48
     49    this->serverlist.push_back( toAdd );
    4450    return 0;
    4551  }
     
    4955  {
    5056    /* get an iterator */
    51     std::list<packet::ServerInformation>::iterator i;
     57    std::list<ServerListElem>::iterator i;
    5258
    5359    /* loop through list elements */
    5460    for( i = serverlist.begin(); i != serverlist.end(); ++i )
    55       if( (*i).getServerName() == name )
     61      if( (*i).ServerInfo.getServerName() == name )
    5662      { /* found this name, remove and quit */
    5763        this->serverlist.erase( i );
     
    6470  {
    6571    /* get an iterator */
    66     std::list<packet::ServerInformation>::iterator i;
     72    std::list<ServerListElem>::iterator i;
    6773
    6874    /* loop through list elements */
    69     for( i=serverlist.begin(); i != serverlist.end(); ++i )
    70       if( (*i).getServerIP() == address )
     75    for( i = serverlist.begin(); i != serverlist.end(); ++i )
     76      if( (*i).ServerInfo.getServerIP() == address )
    7177      { /* found this name, remove and quit */
    7278        this->serverlist.erase( i );
     
    7682  }
    7783
     84  /* SEARCHING */
     85  ServerListSearchResult
     86  ServerList::findServerByAddress( std::string address )
     87  {
     88    /* get an iterator */
     89    std::list<ServerListElem>::iterator i;
    7890
     91    /* loop through list elements */
     92    for( i = serverlist.begin(); i != serverlist.end(); ++i )
     93      if( (*i).ServerInfo.getServerIP() == address )
     94      { /* found the target, return it */
     95        ServerListSearchResult res = { (*i), true };
     96        return res;
     97      }
     98
     99    /* no success */
     100    ServerListSearchResult res = { (*i), false };
     101    return res;
     102  }
     103
     104  ServerListSearchResult
     105  ServerList::findServerByName( std::string name )
     106  {
     107    /* get an iterator */
     108    std::list<ServerListElem>::iterator i;
     109
     110    /* iterate, return when name found */
     111    /* loop through list elements */
     112    for( i = serverlist.begin(); i != serverlist.end(); ++i )
     113      if( (*i).ServerInfo.getServerName() == name )
     114      {
     115        ServerListSearchResult res = { (*i), true };
     116        return res;
     117      }
     118
     119    /* no luck, return a struct that tells the caller so */
     120    ServerListSearchResult res = { (*i), false };
     121    return res;
     122  }
     123
     124  /* SORTING */
    79125  /* sort by name */
    80   bool sub_compare_names( packet::ServerInformation no1,
    81     packet::ServerInformation no2 )
    82   { return no1.getServerName() > no2.getServerName(); }
     126  bool sub_compare_names( ServerListElem no1,
     127    ServerListElem no2 )
     128  { return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName(); }
    83129
    84130  void ServerList::sortByName()
     
    88134 
    89135  /* sort by ping */
    90   bool sub_compare_pings( packet::ServerInformation no1,
    91     packet::ServerInformation no2 )
     136  bool sub_compare_pings( ServerListElem no1,
     137    ServerListElem no2 )
    92138  {
    93     /* TODO */
    94     return no1.getServerName() > no2.getServerName();
     139    return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName();
    95140  }
    96141
  • code/branches/masterserverfix/src/libraries/network/ServerList.h

    r8351 r8935  
    3737namespace orxonox
    3838{
     39  /* HELPER STRUCTURES */
     40  struct ServerListElem
     41  {
     42    /* server information (name, IP, etc) */
     43    packet::ServerInformation ServerInfo;
     44
     45    /* peer pointer */
     46    ENetPeer* peer;
     47  };
     48
     49  struct ServerListSearchResult
     50  {
     51    /* list element found */
     52    ServerListElem result;
     53
     54    /* successful search */
     55    bool success;
     56  };
     57
     58
     59
     60
     61
    3962  /** This class is keeps a list of game servers
    4063   * and some info about them.
     
    5477       * Add server to the game server list
    5578       */
    56       int addServer( packet::ServerInformation toadd );
     79      int addServer( packet::ServerInformation toadd,
     80        ENetPeer *peer );
    5781
    5882      /** \param name Name of the server to remove
     
    6993
    7094
    71       /* SORTING (to be implemented) */
    7295
     96
     97      /* SEARCHING */
     98      /* \param address The address of the server that is to be
     99       *  found
     100       * \return A struct containing a result of the search and a boolean
     101       *  that is only true if the search was successful
     102       *
     103       * Find and return the list handle of a given address.
     104       */
     105      ServerListSearchResult
     106      findServerByAddress( std::string address );
     107
     108
     109      /* \param name The name of the server that is to be
     110       *  found
     111       * \return The struct containing the list entry of the server
     112       *
     113       * Find and return the list handle of a given name.
     114       */
     115      ServerListSearchResult
     116      findServerByName( std::string name );
     117
     118
     119      /* SORTING */
    73120      /** sort by name  */
    74121      void sortByName();
     
    78125
    79126      /** the list of servers for internal storage */
    80       std::list<packet::ServerInformation> serverlist;
     127      std::list<ServerListElem> serverlist;
    81128    private:
    82129  };
Note: See TracChangeset for help on using the changeset viewer.