[Orxonox-commit 4264] r8935 - code/branches/masterserverfix/src/libraries/network
smerkli at orxonox.net
smerkli at orxonox.net
Wed Nov 16 14:32:57 CET 2011
Author: smerkli
Date: 2011-11-16 14:32:57 +0100 (Wed, 16 Nov 2011)
New Revision: 8935
Modified:
code/branches/masterserverfix/src/libraries/network/MasterServer.cc
code/branches/masterserverfix/src/libraries/network/MasterServer.h
code/branches/masterserverfix/src/libraries/network/ServerList.cc
code/branches/masterserverfix/src/libraries/network/ServerList.h
Log:
merged changes from masterserver2 branch into newer trunk here.
Modified: code/branches/masterserverfix/src/libraries/network/MasterServer.cc
===================================================================
--- code/branches/masterserverfix/src/libraries/network/MasterServer.cc 2011-11-16 13:07:17 UTC (rev 8934)
+++ code/branches/masterserverfix/src/libraries/network/MasterServer.cc 2011-11-16 13:32:57 UTC (rev 8935)
@@ -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/branches/masterserverfix/src/libraries/network/MasterServer.h
===================================================================
--- code/branches/masterserverfix/src/libraries/network/MasterServer.h 2011-11-16 13:07:17 UTC (rev 8934)
+++ code/branches/masterserverfix/src/libraries/network/MasterServer.h 2011-11-16 13:32:57 UTC (rev 8935)
@@ -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/branches/masterserverfix/src/libraries/network/ServerList.cc
===================================================================
--- code/branches/masterserverfix/src/libraries/network/ServerList.cc 2011-11-16 13:07:17 UTC (rev 8934)
+++ code/branches/masterserverfix/src/libraries/network/ServerList.cc 2011-11-16 13:32:57 UTC (rev 8935)
@@ -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/branches/masterserverfix/src/libraries/network/ServerList.h
===================================================================
--- code/branches/masterserverfix/src/libraries/network/ServerList.h 2011-11-16 13:07:17 UTC (rev 8934)
+++ code/branches/masterserverfix/src/libraries/network/ServerList.h 2011-11-16 13:32:57 UTC (rev 8935)
@@ -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