- Timestamp:
- Nov 16, 2011, 2:32:57 PM (13 years ago)
- Location:
- code/branches/masterserverfix/src/libraries/network
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/masterserverfix/src/libraries/network/MasterServer.cc
r8858 r8935 29 29 #include "MasterServer.h" 30 30 #include "util/ScopedSingletonManager.h" 31 #include "core/command/ConsoleCommand.h" 31 32 #include "core/CoreIncludes.h" 32 33 #include "core/CorePrereqs.h" 34 #include "util/Output.h" 33 35 34 36 namespace orxonox 35 37 { 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 36 99 /* helpers */ 37 100 static void … … 53 116 { 54 117 /* get an iterator */ 55 std::list< packet::ServerInformation>::iterator i;118 std::list<ServerListElem>::iterator i; 56 119 57 120 /* packet holder */ … … 64 127 /* send this particular server */ 65 128 /* build reply string */ 66 char *tosend = (char *)calloc( (*i). getServerIP().length()129 char *tosend = (char *)calloc( (*i).ServerInfo.getServerIP().length() 67 130 + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 ); 68 131 if( !tosend ) … … 71 134 } 72 135 sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM, 73 (*i). getServerIP().c_str() );136 (*i).ServerInfo.getServerIP().c_str() ); 74 137 75 138 /* create packet from it */ … … 98 161 /* One could just use enet_host_service() instead. */ 99 162 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 100 199 } 101 200 … … 180 279 MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) ) 181 280 { /* register new server */ 182 mainlist.addServer( packet::ServerInformation( event ) ); 281 mainlist.addServer( packet::ServerInformation( event ), 282 event->peer ); 183 283 184 284 /* tell people we did so */ … … 235 335 } 236 336 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 239 341 /* create an iterator for the loop */ 240 342 enet_host_service( this->server, event, 100 ); … … 291 393 } 292 394 293 /* **** INITIALIZE GAME SERVER AND PEER LISTS *****/294 this->peers = new PeerList();395 /* set pointer to this instance */ 396 MasterServer::setInstance( this ); 295 397 296 398 /* tell people we're now initialized */ -
code/branches/masterserverfix/src/libraries/network/MasterServer.h
r8351 r8935 61 61 int run(); 62 62 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 63 74 private: 64 75 /* methods */ … … 69 80 /* helpers */ 70 81 void helper_sendlist( ENetEvent *event ); 82 void helper_cleanupServers( void ); 71 83 72 84 /* members */ … … 74 86 ENetHost *server; 75 87 ServerList mainlist; 76 PeerList *peers;77 88 78 89 unsigned int port; -
code/branches/masterserverfix/src/libraries/network/ServerList.cc
r8351 r8935 40 40 41 41 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 ); 44 50 return 0; 45 51 } … … 49 55 { 50 56 /* get an iterator */ 51 std::list< packet::ServerInformation>::iterator i;57 std::list<ServerListElem>::iterator i; 52 58 53 59 /* loop through list elements */ 54 60 for( i = serverlist.begin(); i != serverlist.end(); ++i ) 55 if( (*i). getServerName() == name )61 if( (*i).ServerInfo.getServerName() == name ) 56 62 { /* found this name, remove and quit */ 57 63 this->serverlist.erase( i ); … … 64 70 { 65 71 /* get an iterator */ 66 std::list< packet::ServerInformation>::iterator i;72 std::list<ServerListElem>::iterator i; 67 73 68 74 /* 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 ) 71 77 { /* found this name, remove and quit */ 72 78 this->serverlist.erase( i ); … … 76 82 } 77 83 84 /* SEARCHING */ 85 ServerListSearchResult 86 ServerList::findServerByAddress( std::string address ) 87 { 88 /* get an iterator */ 89 std::list<ServerListElem>::iterator i; 78 90 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 */ 79 125 /* sort by name */ 80 bool sub_compare_names( packet::ServerInformationno1,81 packet::ServerInformationno2 )82 { return no1. getServerName() > no2.getServerName(); }126 bool sub_compare_names( ServerListElem no1, 127 ServerListElem no2 ) 128 { return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName(); } 83 129 84 130 void ServerList::sortByName() … … 88 134 89 135 /* sort by ping */ 90 bool sub_compare_pings( packet::ServerInformationno1,91 packet::ServerInformationno2 )136 bool sub_compare_pings( ServerListElem no1, 137 ServerListElem no2 ) 92 138 { 93 /* TODO */ 94 return no1.getServerName() > no2.getServerName(); 139 return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName(); 95 140 } 96 141 -
code/branches/masterserverfix/src/libraries/network/ServerList.h
r8351 r8935 37 37 namespace orxonox 38 38 { 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 39 62 /** This class is keeps a list of game servers 40 63 * and some info about them. … … 54 77 * Add server to the game server list 55 78 */ 56 int addServer( packet::ServerInformation toadd ); 79 int addServer( packet::ServerInformation toadd, 80 ENetPeer *peer ); 57 81 58 82 /** \param name Name of the server to remove … … 69 93 70 94 71 /* SORTING (to be implemented) */72 95 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 */ 73 120 /** sort by name */ 74 121 void sortByName(); … … 78 125 79 126 /** the list of servers for internal storage */ 80 std::list< packet::ServerInformation> serverlist;127 std::list<ServerListElem> serverlist; 81 128 private: 82 129 };
Note: See TracChangeset
for help on using the changeset viewer.