From cd756885a646d1a548362b05e90b79d6cf9d01c3 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Tue, 17 May 2011 01:52:49 +1200 Subject: [PATCH] remove MasterServerQuery in favour of ServerList --- OpenRA.Game/Game.cs | 3 - OpenRA.Game/Network/GameServer.cs | 37 ++++++++ OpenRA.Game/Network/ServerList.cs | 52 +++++++++++ OpenRA.Game/OpenRA.Game.csproj | 3 +- OpenRA.Game/Server/MasterServerQuery.cs | 90 ------------------- .../Widgets/CncServerBrowserLogic.cs | 41 +-------- .../Delegates/ServerBrowserDelegate.cs | 7 +- 7 files changed, 96 insertions(+), 137 deletions(-) create mode 100644 OpenRA.Game/Network/GameServer.cs create mode 100644 OpenRA.Game/Network/ServerList.cs delete mode 100755 OpenRA.Game/Server/MasterServerQuery.cs diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index a3f36b7e89..c9b850a556 100755 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -18,7 +18,6 @@ using OpenRA.FileFormats; using OpenRA.GameRules; using OpenRA.Graphics; using OpenRA.Network; -using OpenRA.Server; using OpenRA.Support; using OpenRA.Widgets; @@ -149,8 +148,6 @@ namespace OpenRA PerfHistory.items["render"].Tick(); PerfHistory.items["batches"].Tick(); - MasterServerQuery.Tick(); - afterTickActions.PerformActions(); } diff --git a/OpenRA.Game/Network/GameServer.cs b/OpenRA.Game/Network/GameServer.cs new file mode 100644 index 0000000000..9ec144f29a --- /dev/null +++ b/OpenRA.Game/Network/GameServer.cs @@ -0,0 +1,37 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System.Collections.Generic; +using System.Linq; + +namespace OpenRA.Network +{ + public class GameServer + { + public readonly int Id = 0; + public readonly string Name = null; + public readonly string Address = null; + public readonly int State = 0; + public readonly int Players = 0; + public readonly string Map = null; + public readonly string[] Mods = { }; + public readonly int TTL = 0; + + public Dictionary UsefulMods + { + get + { + return Mods + .Where(v => v.Contains('@')) + .ToDictionary(v => v.Split('@')[0], v => v.Split('@')[1]); + } + } + } +} diff --git a/OpenRA.Game/Network/ServerList.cs b/OpenRA.Game/Network/ServerList.cs new file mode 100644 index 0000000000..3a9f699e02 --- /dev/null +++ b/OpenRA.Game/Network/ServerList.cs @@ -0,0 +1,52 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading; +using OpenRA.FileFormats; + +namespace OpenRA.Network +{ + public static class ServerList + { + public static void Query(Action onComplete) + { + var masterServerUrl = Game.Settings.Server.MasterServer; + + new Thread(() => + { + GameServer[] games = null; + try + { + var str = GetData(new Uri(masterServerUrl + "list.php")); + + var yaml = MiniYaml.FromString(str); + + games = yaml.Select(a => FieldLoader.Load(a.Value)) + .Where(gs => gs.Address != null).ToArray(); + } + catch { } + + Game.RunAfterTick(() => onComplete(games)); + }) { IsBackground = true }.Start(); + } + + static string GetData(Uri uri) + { + var wc = new WebClient(); + wc.Proxy = null; + var data = wc.DownloadData(uri); + return Encoding.UTF8.GetString(data); + } + } +} diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index c81c2958c0..be7011fb21 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -100,7 +100,6 @@ - @@ -198,10 +197,12 @@ + + diff --git a/OpenRA.Game/Server/MasterServerQuery.cs b/OpenRA.Game/Server/MasterServerQuery.cs deleted file mode 100755 index a2079127e3..0000000000 --- a/OpenRA.Game/Server/MasterServerQuery.cs +++ /dev/null @@ -1,90 +0,0 @@ -#region Copyright & License Information -/* - * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) - * This file is part of OpenRA, which is free software. It is made - * available to you under the terms of the GNU General Public License - * as published by the Free Software Foundation. For more information, - * see COPYING. - */ -#endregion - -using System; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading; -using System.Collections.Generic; -using OpenRA.FileFormats; -using OpenRA.Widgets; - -namespace OpenRA.Server -{ - // TODO: This can die once ra is sane - [Obsolete] public static class MasterServerQuery - { - public static event Action OnComplete = _ => { }; - public static event Action OnVersion = _ => { }; - - static GameServer[] Games = { }; - public static string ClientVersion = ""; - public static string ServerVersion = ""; - static AutoResetEvent ev = new AutoResetEvent(false); - static AutoResetEvent ev2 = new AutoResetEvent(false); - - public static void Refresh(string masterServerUrl) - { - new Thread(() => - { - try - { - var str = GetData(new Uri(masterServerUrl + "list.php")); - - var yaml = MiniYaml.FromString(str); - - Games = yaml.Select(a => FieldLoader.Load(a.Value)) - .Where(gs => gs.Address != null).ToArray(); - } - catch - { - Games = null; - } - - ev.Set(); - }).Start(); - } - - public static void Tick() - { - if (ev.WaitOne(TimeSpan.FromMilliseconds(0))) - OnComplete(Games); - if (ev2.WaitOne(TimeSpan.FromMilliseconds(0))) - OnVersion(ServerVersion); - } - - static string GetData(Uri uri) - { - var wc = new WebClient(); - wc.Proxy = null; - var data = wc.DownloadData(uri); - return Encoding.UTF8.GetString(data); - } - } - - public class GameServer - { - public readonly int Id = 0; - public readonly string Name = null; - public readonly string Address = null; - public readonly int State = 0; - public readonly int Players = 0; - public readonly string Map = null; - public readonly string[] Mods = { }; - public readonly int TTL = 0; - - public Dictionary UsefulMods { - get { - return Mods.Where(v => v.Contains('@')).ToDictionary(v => v.Split('@')[0], v => v.Split('@')[1]); - } - } - } -} diff --git a/OpenRA.Mods.Cnc/Widgets/CncServerBrowserLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncServerBrowserLogic.cs index 2b74870b19..0e97a4bcf9 100644 --- a/OpenRA.Mods.Cnc/Widgets/CncServerBrowserLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncServerBrowserLogic.cs @@ -11,49 +11,12 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net; -using System.Text; -using System.Threading; -using OpenRA.FileFormats; -using OpenRA.Graphics; using OpenRA.Mods.RA.Widgets.Delegates; -using OpenRA.Server; +using OpenRA.Network; using OpenRA.Widgets; namespace OpenRA.Mods.Cnc.Widgets -{ - public class ServerList - { - public static void Query(Action onComplete) - { - var masterServerUrl = Game.Settings.Server.MasterServer; - new Thread(() => - { - GameServer[] games = null; - try - { - var str = GetData(new Uri(masterServerUrl + "list.php")); - - var yaml = MiniYaml.FromString(str); - - games = yaml.Select(a => FieldLoader.Load(a.Value)) - .Where(gs => gs.Address != null).ToArray(); - } - catch { } - - Game.RunAfterTick(() => onComplete(games)); - }) { IsBackground = true }.Start(); - } - - static string GetData(Uri uri) - { - var wc = new WebClient(); - wc.Proxy = null; - var data = wc.DownloadData(uri); - return Encoding.UTF8.GetString(data); - } - } - +{ public class CncServerBrowserLogic : IWidgetDelegate { GameServer currentServer; diff --git a/OpenRA.Mods.RA/Widgets/Delegates/ServerBrowserDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/ServerBrowserDelegate.cs index 52eafe22e6..c718fc638c 100644 --- a/OpenRA.Mods.RA/Widgets/Delegates/ServerBrowserDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/ServerBrowserDelegate.cs @@ -11,6 +11,7 @@ using System.Collections.Generic; using System.Linq; using OpenRA.FileFormats; +using OpenRA.Network; using OpenRA.Server; using OpenRA.Widgets; @@ -28,15 +29,13 @@ namespace OpenRA.Mods.RA.Widgets.Delegates { var bg = widget.GetWidget("JOINSERVER_BG"); - MasterServerQuery.OnComplete += games => RefreshServerList(games); - bg.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = true; bg.GetWidget("JOINSERVER_PROGRESS_TITLE").Text = "Fetching game list..."; bg.Children.RemoveAll(a => GameButtons.Contains(a)); GameButtons.Clear(); - MasterServerQuery.Refresh(Game.Settings.Server.MasterServer); + ServerList.Query(RefreshServerList); bg.GetWidget("SERVER_INFO").IsVisible = () => currentServer != null; var preview = bg.GetWidget("MAP_PREVIEW"); @@ -68,7 +67,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates bg.Children.RemoveAll(a => GameButtons.Contains(a)); GameButtons.Clear(); - MasterServerQuery.Refresh(Game.Settings.Server.MasterServer); + ServerList.Query(RefreshServerList); return true; };