- Timestamp:
- Nov 16, 2011, 2:32:57 PM (13 years ago)
- File:
-
- 1 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 */
Note: See TracChangeset
for help on using the changeset viewer.