Async motd grab. Client and server version in MasterServerQuery
This commit is contained in:
33
OpenRA.Game/Server/MasterServerQuery.cs
Normal file → Executable file
33
OpenRA.Game/Server/MasterServerQuery.cs
Normal file → Executable file
@@ -14,6 +14,7 @@ using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Server
|
||||
{
|
||||
@@ -23,7 +24,8 @@ namespace OpenRA.Server
|
||||
public static event Action<string> OnVersion = _ => { };
|
||||
|
||||
static GameServer[] Games = { };
|
||||
static string Version = "";
|
||||
public static string ClientVersion = "";
|
||||
public static string ServerVersion = "";
|
||||
static AutoResetEvent ev = new AutoResetEvent(false);
|
||||
static AutoResetEvent ev2 = new AutoResetEvent(false);
|
||||
|
||||
@@ -49,12 +51,35 @@ namespace OpenRA.Server
|
||||
}).Start();
|
||||
}
|
||||
|
||||
public static void GetMOTD(string masterServerUrl)
|
||||
{
|
||||
new Thread(() =>
|
||||
{
|
||||
var motd = Widget.RootWidget.GetWidget<ScrollingTextWidget>("MOTD_SCROLLER");
|
||||
if (motd != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
motd.Text = GetData(new Uri(masterServerUrl + "motd.php?v=" + ClientVersion));
|
||||
motd.ResetScroll();
|
||||
}
|
||||
catch
|
||||
{
|
||||
motd.Text = "Welcome to OpenRA. MOTD unable to be loaded from server.";
|
||||
motd.ResetScroll();
|
||||
}
|
||||
}
|
||||
|
||||
ev.Set();
|
||||
}).Start();
|
||||
}
|
||||
|
||||
public static void Tick()
|
||||
{
|
||||
if (ev.WaitOne(TimeSpan.FromMilliseconds(0)))
|
||||
OnComplete(Games);
|
||||
if (ev2.WaitOne(TimeSpan.FromMilliseconds(0)))
|
||||
OnVersion(Version);
|
||||
OnVersion(ServerVersion);
|
||||
}
|
||||
|
||||
static string GetData(Uri uri)
|
||||
@@ -70,11 +95,11 @@ namespace OpenRA.Server
|
||||
{
|
||||
try
|
||||
{
|
||||
Version = GetData(new Uri(masterServerUrl + "VERSION"));
|
||||
ServerVersion = GetData(new Uri(masterServerUrl + "VERSION"));
|
||||
}
|
||||
catch
|
||||
{
|
||||
Version = "";
|
||||
ServerVersion = "";
|
||||
}
|
||||
|
||||
ev2.Set();
|
||||
|
||||
@@ -29,8 +29,6 @@ namespace OpenRA.Widgets.Delegates
|
||||
|
||||
var version = widget.GetWidget<LabelWidget>("VERSION_STRING");
|
||||
|
||||
var motd = widget.GetWidget<ScrollingTextWidget>("MOTD_SCROLLER");
|
||||
|
||||
if (FileSystem.Exists("VERSION"))
|
||||
{
|
||||
var s = FileSystem.Open("VERSION");
|
||||
@@ -43,29 +41,9 @@ namespace OpenRA.Widgets.Delegates
|
||||
{
|
||||
version.Text = "Dev Build";
|
||||
}
|
||||
MasterServerQuery.ClientVersion = version.Text;
|
||||
|
||||
if (motd != null)
|
||||
{
|
||||
motd.Text = "Welcome to OpenRA. MOTD unable to be loaded from server.";
|
||||
|
||||
string URL = "http://open-ra.org/master/motd.php?v=" + version.Text;
|
||||
|
||||
WebRequest req = WebRequest.Create(URL);
|
||||
StreamReader reader = null;
|
||||
try
|
||||
{
|
||||
reader = new StreamReader(req.GetResponse().GetResponseStream());
|
||||
}
|
||||
catch (WebException e)
|
||||
{
|
||||
reader.Close();
|
||||
return;
|
||||
}
|
||||
var result = reader.ReadToEnd();
|
||||
reader.Close();
|
||||
|
||||
motd.Text = result;
|
||||
}
|
||||
MasterServerQuery.GetMOTD(Game.Settings.Server.MasterServer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,12 @@ namespace OpenRA.Widgets
|
||||
UpdateScrollBuffer();
|
||||
}
|
||||
|
||||
public void ResetScroll()
|
||||
{
|
||||
ScrollLocation = 0;
|
||||
ScrollTick = 0;
|
||||
}
|
||||
|
||||
private void UpdateScrollBuffer()
|
||||
{
|
||||
ScrollTick++;
|
||||
|
||||
Reference in New Issue
Block a user