remove some sillyness from the masterserver -- it shouldnt hold up the works.

This commit is contained in:
Chris Forbes
2010-08-12 19:27:11 +12:00
parent 1ad8b58f1b
commit 7cd9afb6d2

View File

@@ -45,7 +45,8 @@ namespace OpenRA.Server
static string masterServerUrl; static string masterServerUrl;
static bool isInitialPing; static bool isInitialPing;
public static void ServerMain(bool internetServer, string masterServerUrl, string name, int port, int extport, string[] mods, string map, bool cheats) public static void ServerMain(bool internetServer, string masterServerUrl, string name, int port, int extport,
string[] mods, string map, bool cheats)
{ {
Log.AddChannel("server", "server.log", false, false); Log.AddChannel("server", "server.log", false, false);
@@ -86,7 +87,7 @@ namespace OpenRA.Server
checkRead.Add( listener.Server ); checkRead.Add( listener.Server );
foreach( var c in conns ) checkRead.Add( c.socket ); foreach( var c in conns ) checkRead.Add( c.socket );
Socket.Select( checkRead, null, null, MasterPingInterval * 1000000 ); Socket.Select( checkRead, null, null, MasterPingInterval * 10000 );
foreach( Socket s in checkRead ) foreach( Socket s in checkRead )
if( s == listener.Server ) AcceptConnection(); if( s == listener.Server ) AcceptConnection();
@@ -94,6 +95,10 @@ namespace OpenRA.Server
if (Environment.TickCount - lastPing > MasterPingInterval * 1000) if (Environment.TickCount - lastPing > MasterPingInterval * 1000)
PingMasterServer(); PingMasterServer();
else
lock (masterServerMessages)
while (masterServerMessages.Count > 0)
SendChat(null, masterServerMessages.Dequeue());
if (conns.Count() == 0) if (conns.Count() == 0)
{ {
@@ -445,50 +450,57 @@ namespace OpenRA.Server
PingMasterServer(); PingMasterServer();
} }
static volatile bool isBusy;
static Queue<string> masterServerMessages = new Queue<string>();
static void PingMasterServer() static void PingMasterServer()
{ {
if (wc.IsBusy || !isInternetServer) return; if (isBusy || !isInternetServer) return;
var url = "ping.php?port={0}&name={1}&state={2}&players={3}&mods={4}&map={5}"; lastPing = Environment.TickCount;
wc.DownloadDataCompleted += PingMasterServerResponse; isBusy = true;
if (isInitialPing)
Action a = () =>
{ {
url += "&new=1"; try
isInitialPing = false; {
} var url = "ping.php?port={0}&name={1}&state={2}&players={3}&mods={4}&map={5}";
else if (isInitialPing) url += "&new=1";
wc.DownloadDataCompleted -= PingMasterServerResponse;
wc.DownloadDataAsync(new Uri( using (var wc = new WebClient())
{
var result = wc.DownloadData(
masterServerUrl + url.F( masterServerUrl + url.F(
ExternalPort, Uri.EscapeUriString(Name), ExternalPort, Uri.EscapeUriString(Name),
GameStarted ? 2 : 1, // todo: post-game states, etc. GameStarted ? 2 : 1, // todo: post-game states, etc.
lobbyInfo.Clients.Count, lobbyInfo.Clients.Count,
string.Join(",", lobbyInfo.GlobalSettings.Mods), string.Join(",", lobbyInfo.GlobalSettings.Mods),
lobbyInfo.GlobalSettings.Map))); lobbyInfo.GlobalSettings.Map));
lastPing = Environment.TickCount; if (isInitialPing)
}
static void PingMasterServerResponse(object sender, DownloadDataCompletedEventArgs e)
{ {
if (e.Error != null) isInitialPing = false;
{
Log.Write("server", "Error pinging Master Server; {0}", e.Error.Message);
return;
}
if (e.Result.Length == 0) var s = Encoding.UTF8.GetString(result);
{
Log.Write("server", "Error pinging Master Server; Empty Response");
return;
}
string s = Encoding.UTF8.GetString(e.Result);
int gameId; int gameId;
if (int.TryParse(s.Trim(), out gameId)) if (int.TryParse(s.Trim(), out gameId))
Game.SetGameId(gameId); Game.SetGameId(gameId);
Log.Write("server", "Game ID: {0}", gameId);
lock (masterServerMessages)
masterServerMessages.Enqueue("Master server communication established. Game ID = {0}".F(gameId));
}
}
}
catch(Exception ex)
{
Log.Write("server", ex.ToString());
lock( masterServerMessages )
masterServerMessages.Enqueue( "Master server communication failed." );
}
isBusy = false;
};
a.BeginInvoke(null, null);
} }
} }
} }