[Orxonox-commit 4266] r8937 - in code/trunk: . src/libraries/network

smerkli at orxonox.net smerkli at orxonox.net
Wed Nov 16 14:55:40 CET 2011


Author: smerkli
Date: 2011-11-16 14:55:40 +0100 (Wed, 16 Nov 2011)
New Revision: 8937

Modified:
   code/trunk/
   code/trunk/src/libraries/network/MasterServer.cc
   code/trunk/src/libraries/network/MasterServer.h
   code/trunk/src/libraries/network/ServerList.cc
   code/trunk/src/libraries/network/ServerList.h
Log:
merged masterserverfix corretly now.


Property changes on: code/trunk
___________________________________________________________________
Modified: svn:mergeinfo
   - /code/branches/ai:6592-7033
/code/branches/ai2:8721-8880
/code/branches/bigships:8137-8588
/code/branches/buildsystem:1874-2276,2278-2400
/code/branches/buildsystem2:2506-2658
/code/branches/buildsystem3:2662-2708
/code/branches/ceguilua:1802-1808
/code/branches/chat:6527-6797
/code/branches/chat2:6836-6910
/code/branches/console:5941-6104
/code/branches/consolecommands2:6451-7178
/code/branches/consolecommands3:7178-7283
/code/branches/core3:1572-1739
/code/branches/core4:3221-3224,3227,3234-3238,3242,3244-3250,3252-3254,3256,3259-3261,3264-3265,3268-3275,3277-3278,3280,3284-3285,3287,3289-3294,3305,3309-3310
/code/branches/core5:5768-5928,6009
/code/branches/data_cleanup:7537-7686
/code/branches/doc:7290-7400
/code/branches/dockingsystem:8101-8192
/code/branches/dockingsystem2:8196-8560
/code/branches/dynamicmatch:6584-7030
/code/branches/fps:6591-7072
/code/branches/gameimmersion:8102-8577
/code/branches/gamestate:6430-6572,6621-6661
/code/branches/gamestates2:6594-6745
/code/branches/gametypes:2826-3031
/code/branches/gcc43:1580
/code/branches/gui:1635-1723,2795-2894
/code/branches/hudelements:6584-6941
/code/branches/hudimprovements:7920-8672
/code/branches/ingamemenu:6000-6023
/code/branches/input:1629-1636
/code/branches/ipv6:7293-7458
/code/branches/kicklib:7940-8096,8098-8277
/code/branches/kicklib2:8282-8350
/code/branches/lastmanstanding:7479-7644
/code/branches/lastmanstanding3:7903-8175
/code/branches/libraries:5612-5692
/code/branches/libraries2:5703-5737
/code/branches/lod:6586-6911
/code/branches/lodfinal:2372-2411
/code/branches/mac_osx:7789-8128,8135
/code/branches/map:2801-3086,3089
/code/branches/masterserver:7502-7738
/code/branches/masterserverfix:8933
/code/branches/menu:5941-6146,6148,7536-7687
/code/branches/miniprojects:2754-2824
/code/branches/netp2:2835-2988
/code/branches/netp3:2988-3082
/code/branches/netp6:3214-3302
/code/branches/network:2356
/code/branches/network2:6434-6465
/code/branches/network3:7196-7344
/code/branches/network4:7497-7755
/code/branches/network5:7757-7781
/code/branches/network6:7823-8315
/code/branches/network64:2210-2355
/code/branches/notifications:7314-7401
/code/branches/objecthierarchy:1911-2085,2100,2110-2169
/code/branches/objecthierarchy2:2171-2479
/code/branches/ois_update:7506-7788
/code/branches/output:8739-8857
/code/branches/overlay:2117-2385
/code/branches/particles:2829-3085
/code/branches/particles2:6050-6106,6109
/code/branches/pch:3113-3194
/code/branches/physics:1912-2055,2107-2439
/code/branches/physics_merge:2436-2457
/code/branches/pickup:8145-8555
/code/branches/pickup2:5942-6405
/code/branches/pickup3:6418-6523
/code/branches/pickup4:6594-6710
/code/branches/pickups:1926-2086,2127,2827-2915
/code/branches/pickups2:2107-2497,2915-3071
/code/branches/png2:7262-7263
/code/branches/portals:8087-8455
/code/branches/portals2:8460-8602
/code/branches/ppspickups1:6552-6708
/code/branches/ppspickups2:6527-6532,6554-6709
/code/branches/ppspickups3:6757-6997
/code/branches/ppspickups4:7003-7089
/code/branches/presentation:2369-2652,2654-2660,7736-7786,8500-8705
/code/branches/presentation2:6106-6416,7787-7800
/code/branches/presentation3:6913-7162
/code/branches/questsystem:1894-2088
/code/branches/questsystem2:2107-2259
/code/branches/questsystem5:2776-2905
/code/branches/releasetodo:7614-7647
/code/branches/resource:3327-3366
/code/branches/resource2:3372-5694
/code/branches/rocket:6523-6950
/code/branches/rocket2:6953-6970
/code/branches/script_trigger:1295-1953,1955
/code/branches/skybox2:6559-6989
/code/branches/sound:2829-3010
/code/branches/sound3:5941-6102
/code/branches/spaceboundaries:8085-8457
/code/branches/spaceboundaries2:8460-8613
/code/branches/spacerace:8182-8630
/code/branches/steering:5949-6091,8140-8595
/code/branches/tetris:8100-8563
/code/branches/tutoriallevel:7827-8370
/code/branches/tutoriallevel2:8370-8452
/code/branches/tutoriallevel3:8453-8636
/code/branches/unity_build:8440-8716
/code/branches/usability:7915-8078
/code/branches/weapon:1925-2094
/code/branches/weapon2:2107-2488
/code/branches/weapons:2897-3051,8143-8591
/code/branches/weaponsystem:2742-2890
   + /code/branches/ai:6592-7033
/code/branches/ai2:8721-8880
/code/branches/bigships:8137-8588
/code/branches/buildsystem:1874-2276,2278-2400
/code/branches/buildsystem2:2506-2658
/code/branches/buildsystem3:2662-2708
/code/branches/ceguilua:1802-1808
/code/branches/chat:6527-6797
/code/branches/chat2:6836-6910
/code/branches/console:5941-6104
/code/branches/consolecommands2:6451-7178
/code/branches/consolecommands3:7178-7283
/code/branches/core3:1572-1739
/code/branches/core4:3221-3224,3227,3234-3238,3242,3244-3250,3252-3254,3256,3259-3261,3264-3265,3268-3275,3277-3278,3280,3284-3285,3287,3289-3294,3305,3309-3310
/code/branches/core5:5768-5928,6009
/code/branches/data_cleanup:7537-7686
/code/branches/doc:7290-7400
/code/branches/dockingsystem:8101-8192
/code/branches/dockingsystem2:8196-8560
/code/branches/dynamicmatch:6584-7030
/code/branches/fps:6591-7072
/code/branches/gameimmersion:8102-8577
/code/branches/gamestate:6430-6572,6621-6661
/code/branches/gamestates2:6594-6745
/code/branches/gametypes:2826-3031
/code/branches/gcc43:1580
/code/branches/gui:1635-1723,2795-2894
/code/branches/hudelements:6584-6941
/code/branches/hudimprovements:7920-8672
/code/branches/ingamemenu:6000-6023
/code/branches/input:1629-1636
/code/branches/ipv6:7293-7458
/code/branches/kicklib:7940-8096,8098-8277
/code/branches/kicklib2:8282-8350
/code/branches/lastmanstanding:7479-7644
/code/branches/lastmanstanding3:7903-8175
/code/branches/libraries:5612-5692
/code/branches/libraries2:5703-5737
/code/branches/lod:6586-6911
/code/branches/lodfinal:2372-2411
/code/branches/mac_osx:7789-8128,8135
/code/branches/map:2801-3086,3089
/code/branches/masterserver:7502-7738
/code/branches/masterserverfix:8933-8936
/code/branches/menu:5941-6146,6148,7536-7687
/code/branches/miniprojects:2754-2824
/code/branches/netp2:2835-2988
/code/branches/netp3:2988-3082
/code/branches/netp6:3214-3302
/code/branches/network:2356
/code/branches/network2:6434-6465
/code/branches/network3:7196-7344
/code/branches/network4:7497-7755
/code/branches/network5:7757-7781
/code/branches/network6:7823-8315
/code/branches/network64:2210-2355
/code/branches/notifications:7314-7401
/code/branches/objecthierarchy:1911-2085,2100,2110-2169
/code/branches/objecthierarchy2:2171-2479
/code/branches/ois_update:7506-7788
/code/branches/output:8739-8857
/code/branches/overlay:2117-2385
/code/branches/particles:2829-3085
/code/branches/particles2:6050-6106,6109
/code/branches/pch:3113-3194
/code/branches/physics:1912-2055,2107-2439
/code/branches/physics_merge:2436-2457
/code/branches/pickup:8145-8555
/code/branches/pickup2:5942-6405
/code/branches/pickup3:6418-6523
/code/branches/pickup4:6594-6710
/code/branches/pickups:1926-2086,2127,2827-2915
/code/branches/pickups2:2107-2497,2915-3071
/code/branches/png2:7262-7263
/code/branches/portals:8087-8455
/code/branches/portals2:8460-8602
/code/branches/ppspickups1:6552-6708
/code/branches/ppspickups2:6527-6532,6554-6709
/code/branches/ppspickups3:6757-6997
/code/branches/ppspickups4:7003-7089
/code/branches/presentation:2369-2652,2654-2660,7736-7786,8500-8705
/code/branches/presentation2:6106-6416,7787-7800
/code/branches/presentation3:6913-7162
/code/branches/questsystem:1894-2088
/code/branches/questsystem2:2107-2259
/code/branches/questsystem5:2776-2905
/code/branches/releasetodo:7614-7647
/code/branches/resource:3327-3366
/code/branches/resource2:3372-5694
/code/branches/rocket:6523-6950
/code/branches/rocket2:6953-6970
/code/branches/script_trigger:1295-1953,1955
/code/branches/skybox2:6559-6989
/code/branches/sound:2829-3010
/code/branches/sound3:5941-6102
/code/branches/spaceboundaries:8085-8457
/code/branches/spaceboundaries2:8460-8613
/code/branches/spacerace:8182-8630
/code/branches/steering:5949-6091,8140-8595
/code/branches/tetris:8100-8563
/code/branches/tutoriallevel:7827-8370
/code/branches/tutoriallevel2:8370-8452
/code/branches/tutoriallevel3:8453-8636
/code/branches/unity_build:8440-8716
/code/branches/usability:7915-8078
/code/branches/weapon:1925-2094
/code/branches/weapon2:2107-2488
/code/branches/weapons:2897-3051,8143-8591
/code/branches/weaponsystem:2742-2890

Modified: code/trunk/src/libraries/network/MasterServer.cc
===================================================================
--- code/trunk/src/libraries/network/MasterServer.cc	2011-11-16 13:53:23 UTC (rev 8936)
+++ code/trunk/src/libraries/network/MasterServer.cc	2011-11-16 13:55:40 UTC (rev 8937)
@@ -28,11 +28,74 @@
 
 #include "MasterServer.h"
 #include "util/ScopedSingletonManager.h"
+#include "core/command/ConsoleCommand.h"
 #include "core/CoreIncludes.h"
 #include "core/CorePrereqs.h"
+#include "util/Output.h"
 
 namespace orxonox 
 {
+  /*** MACROS ***/
+  /* commands for the terminal interface */
+  SetConsoleCommand( "ms-listservers", &MasterServer::listServers );
+  SetConsoleCommand( "ms-delserver", &MasterServer::delServer );
+  //SetConsoleCommand( "ms-serverinfo", &MasterServer::serverInfo );
+
+  /* forward declaration so the linker doesn't complain */
+  MasterServer *MasterServer::instance = NULL;
+
+
+
+
+  /* command: list servers */
+  void 
+  MasterServer::listServers( void )
+  {
+    /* get an iterator */
+    std::list<ServerListElem>::iterator i;
+
+    /* print list header */
+    orxout(user_info) << "List of connected servers" << std::endl;
+
+    /* loop through list elements */
+    for( i = MasterServer::getInstance()->mainlist.serverlist.begin(); 
+      i != MasterServer::getInstance()->mainlist.serverlist.end(); ++i ) 
+    {
+      orxout(user_info) << "  " << (*i).ServerInfo.getServerIP() << std::endl;
+    }
+
+    /* display end of list */
+    orxout(user_info) << MasterServer::getInstance()->mainlist.serverlist.size() <<
+      " servers connected." << std::endl;
+  }
+
+  void 
+  MasterServer::delServer( std::string todeladdr )
+  {
+    /* tell the user we're now removing the entry from the server list */
+    orxout(user_info) << "MS: Deleting server \"" << todeladdr << "\"..." 
+      << std::endl;
+
+    /* see if we actually have that server on our list */
+    ServerListSearchResult shandle = 
+      MasterServer::getInstance()->mainlist.findServerByAddress(todeladdr);
+
+    if( !shandle.success )
+    { orxout(user_info) << "MS: Server not found, not removing." << std::endl;
+      return;
+    }
+
+    /* force-disconnect the server */  
+    enet_peer_disconnect( shandle.result.peer, NULL );
+
+    /* actually remove the entry from the server list by address */
+    MasterServer::getInstance()->mainlist.delServerByAddress( todeladdr);
+
+    /* tell the user about our success */
+    orxout(user_info) << "MS: Server deletion successful." << std::endl;
+  }
+
+
   /* helpers */
   static void 
   helper_output_debug( ENetEvent *event, char *addrconv )
@@ -52,7 +115,7 @@
   MasterServer::helper_sendlist( ENetEvent *event )
   {
     /* get an iterator */
-    std::list<packet::ServerInformation>::iterator i;
+    std::list<ServerListElem>::iterator i;
 
     /* packet holder */
     ENetPacket *reply;
@@ -63,14 +126,14 @@
     {
       /* send this particular server */
       /* build reply string */
-      char *tosend = (char *)calloc( (*i).getServerIP().length() 
+      char *tosend = (char *)calloc( (*i).ServerInfo.getServerIP().length() 
           + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 );
       if( !tosend ) 
       { orxout(internal_warning, context::master_server) << "Masterserver.cc: Memory allocation failed." << endl;
         continue;
       } 
       sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM, 
-          (*i).getServerIP().c_str() );
+          (*i).ServerInfo.getServerIP().c_str() );
 
       /* create packet from it */
       reply = enet_packet_create( tosend,
@@ -99,9 +162,45 @@
     enet_host_flush( this->server );
   }
 
+  /* maybe the two methods below can be merged into one and 
+   * made to use ENet's RTT functionality to check for disconnected 
+   * servers.
+   */
+  void 
+  MasterServer::helper_cleanupServers( void )
+  {
+    /* get an iterator */
+    std::list<ServerListElem>::iterator i;
+     
+    if( mainlist.serverlist.size() == 0 )
+      return;
 
+    /* loop through list elements */
+    for( i = mainlist.serverlist.begin(); i 
+        != mainlist.serverlist.end(); ++i ) 
+    { /* see if we have a disconnected peer */
+      if( (*i).peer && 
+         ((*i).peer->state == ENET_PEER_STATE_DISCONNECTED ||
+          (*i).peer->state == ENET_PEER_STATE_ZOMBIE ))
+      { 
+        /* Remove it from the list */
+        orxout(internal_warning) << (char*)(*i).peer->data << " timed out.\n";
+        mainlist.delServerByName( (*i).ServerInfo.getServerName() );
 
+        /* stop iterating, we manipulated the list */
+        /* TODO note: this only removes one dead server per loop
+         * iteration. not beautiful, but one iteration is ~100ms, 
+         * so not really relevant for the moment.
+         */
+        break;
+      }
+    }
+ 
+  }
 
+
+
+
   /***** EVENTS *****/
   /* connect event */
   int 
@@ -179,7 +278,8 @@
         + MSPROTO_GAME_SERVER_LEN+1, 
         MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) )
       { /* register new server */
-        mainlist.addServer( packet::ServerInformation( event ) );
+        mainlist.addServer( packet::ServerInformation( event ),
+          event->peer );
         
         /* tell people we did so */
         orxout(internal_info, context::master_server) << "Added new server to list: " << 
@@ -234,8 +334,10 @@
       exit( EXIT_FAILURE );
     }
 
-    /* TODO schedule pings for servers somewhere here */
-    
+    /* check for timed out peers and remove those from * the server list */
+    helper_cleanupServers();
+
+
     /* create an iterator for the loop */
     enet_host_service( this->server, event, 100 );
 
@@ -290,8 +392,8 @@
       exit( EXIT_FAILURE );
     }
 
-    /***** INITIALIZE GAME SERVER AND PEER LISTS *****/
-    this->peers = new PeerList();
+    /* set pointer to this instance */
+    MasterServer::setInstance( this );
 
     /* tell people we're now initialized */
     orxout(internal_status, context::master_server) << "MasterServer initialized, waiting for connections." << endl;

Modified: code/trunk/src/libraries/network/MasterServer.h
===================================================================
--- code/trunk/src/libraries/network/MasterServer.h	2011-11-16 13:53:23 UTC (rev 8936)
+++ code/trunk/src/libraries/network/MasterServer.h	2011-11-16 13:55:40 UTC (rev 8937)
@@ -60,6 +60,17 @@
       /* main routine */
       int run();
 
+      /* static pointer for commands */
+      static MasterServer *instance;
+      static MasterServer *getInstance() 
+        { return instance; }
+      static void setInstance( MasterServer *setto ) 
+        { instance = setto;  }
+      
+      /* functions for commands */
+      static void listServers( void );
+      static void delServer( std::string todeladdr );
+
     private:
       /* methods */
       int eventConnect( ENetEvent *event );
@@ -68,12 +79,12 @@
 
       /* helpers */
       void helper_sendlist( ENetEvent *event );
+      void helper_cleanupServers( void );
 
       /* members */
       ENetAddress address;
       ENetHost *server;
       ServerList mainlist;
-      PeerList *peers;
 
       unsigned int port;
       bool quit;

Modified: code/trunk/src/libraries/network/ServerList.cc
===================================================================
--- code/trunk/src/libraries/network/ServerList.cc	2011-11-16 13:53:23 UTC (rev 8936)
+++ code/trunk/src/libraries/network/ServerList.cc	2011-11-16 13:55:40 UTC (rev 8937)
@@ -39,8 +39,14 @@
   }
 
   int 
-  ServerList::addServer( packet::ServerInformation toadd )
-  { this->serverlist.push_back( toadd ); 
+  ServerList::addServer( packet::ServerInformation toadd,
+    ENetPeer *peer )
+  { 
+    ServerListElem toAdd;
+    toAdd.ServerInfo = toadd;
+    toAdd.peer = peer;
+
+    this->serverlist.push_back( toAdd ); 
     return 0;
   }
 
@@ -48,11 +54,11 @@
   ServerList::delServerByName( std::string name )
   { 
     /* get an iterator */
-    std::list<packet::ServerInformation>::iterator i;
+    std::list<ServerListElem>::iterator i;
 
     /* loop through list elements */
     for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
-      if( (*i).getServerName() == name )
+      if( (*i).ServerInfo.getServerName() == name )
       { /* found this name, remove and quit */
         this->serverlist.erase( i );
         return true;
@@ -63,11 +69,11 @@
   bool ServerList::delServerByAddress( std::string address )
   { 
     /* get an iterator */
-    std::list<packet::ServerInformation>::iterator i;
+    std::list<ServerListElem>::iterator i;
 
     /* loop through list elements */
-    for( i=serverlist.begin(); i != serverlist.end(); ++i ) 
-      if( (*i).getServerIP() == address )
+    for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i).ServerInfo.getServerIP() == address )
       { /* found this name, remove and quit */
         this->serverlist.erase( i );
         return true;
@@ -75,11 +81,51 @@
     return false;
   }
 
+  /* SEARCHING */
+  ServerListSearchResult
+  ServerList::findServerByAddress( std::string address )
+  {
+    /* get an iterator */
+    std::list<ServerListElem>::iterator i;
 
+    /* loop through list elements */
+    for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i).ServerInfo.getServerIP() == address )
+      { /* found the target, return it */
+        ServerListSearchResult res = { (*i), true };
+        return res;
+      }
+
+    /* no success */
+    ServerListSearchResult res = { (*i), false };
+    return res;
+  }
+
+  ServerListSearchResult
+  ServerList::findServerByName( std::string name )
+  {
+    /* get an iterator */
+    std::list<ServerListElem>::iterator i;
+
+    /* iterate, return when name found */
+    /* loop through list elements */
+    for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i).ServerInfo.getServerName() == name )
+      { 
+        ServerListSearchResult res = { (*i), true };
+        return res;
+      }
+
+    /* no luck, return a struct that tells the caller so */
+    ServerListSearchResult res = { (*i), false };
+    return res;
+  }
+
+  /* SORTING */
   /* sort by name */
-  bool sub_compare_names( packet::ServerInformation no1, 
-    packet::ServerInformation no2 )
-  { return no1.getServerName() > no2.getServerName(); }
+  bool sub_compare_names( ServerListElem no1, 
+    ServerListElem no2 )
+  { return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName(); }
 
   void ServerList::sortByName()
   { 
@@ -87,11 +133,10 @@
   }
   
   /* sort by ping */
-  bool sub_compare_pings( packet::ServerInformation no1, 
-    packet::ServerInformation no2 )
+  bool sub_compare_pings( ServerListElem no1, 
+    ServerListElem no2 )
   { 
-    /* TODO */
-    return no1.getServerName() > no2.getServerName();
+    return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName();
   }
 
   void ServerList::sortByPing()

Modified: code/trunk/src/libraries/network/ServerList.h
===================================================================
--- code/trunk/src/libraries/network/ServerList.h	2011-11-16 13:53:23 UTC (rev 8936)
+++ code/trunk/src/libraries/network/ServerList.h	2011-11-16 13:55:40 UTC (rev 8937)
@@ -36,6 +36,29 @@
 /* methods necessary */
 namespace orxonox 
 { 
+  /* HELPER STRUCTURES */
+  struct ServerListElem 
+  {
+    /* server information (name, IP, etc) */
+    packet::ServerInformation ServerInfo;
+
+    /* peer pointer */
+    ENetPeer* peer;
+  };
+
+  struct ServerListSearchResult
+  {
+    /* list element found */
+    ServerListElem result;
+
+    /* successful search */
+    bool success;
+  };
+
+
+
+
+
   /** This class is keeps a list of game servers
    * and some info about them.
    */
@@ -53,7 +76,8 @@
        * 
        * Add server to the game server list
        */
-      int addServer( packet::ServerInformation toadd );
+      int addServer( packet::ServerInformation toadd,
+        ENetPeer *peer );
 
       /** \param name Name of the server to remove
        * 
@@ -68,8 +92,31 @@
       bool delServerByAddress( std::string address );
 
 
-      /* SORTING (to be implemented) */
 
+
+      /* SEARCHING */
+      /* \param address The address of the server that is to be 
+       *  found
+       * \return A struct containing a result of the search and a boolean
+       *  that is only true if the search was successful
+       * 
+       * Find and return the list handle of a given address.
+       */
+      ServerListSearchResult
+      findServerByAddress( std::string address );
+
+
+      /* \param name The name of the server that is to be 
+       *  found
+       * \return The struct containing the list entry of the server
+       * 
+       * Find and return the list handle of a given name.
+       */
+      ServerListSearchResult
+      findServerByName( std::string name );
+
+
+      /* SORTING */
       /** sort by name  */
       void sortByName();
       
@@ -77,7 +124,7 @@
       void sortByPing();
 
       /** the list of servers for internal storage */
-      std::list<packet::ServerInformation> serverlist;
+      std::list<ServerListElem> serverlist;
     private:
   };
 }




More information about the Orxonox-commit mailing list