async server browser

This commit is contained in:
Chris Forbes
2010-04-19 18:06:48 +12:00
parent d8c525d39c
commit faea3d33e3
7 changed files with 131 additions and 48 deletions

View File

@@ -4,20 +4,45 @@ using System.Linq;
using System.Text;
using System.Net;
using OpenRA.FileFormats;
using System.Threading;
namespace OpenRA.Server
{
static class MasterServerQuery
{
public static IEnumerable<GameServer> GetGameList(string masterServerUrl)
{
var wc = new WebClient();
var data = wc.DownloadData(masterServerUrl + "list.php");
var str = Encoding.UTF8.GetString(data);
public static event Action<GameServer[]> OnComplete = _ => { };
var yaml = MiniYaml.FromString(str);
return yaml.Select(a => { var gs = new GameServer(); FieldLoader.Load(gs, a.Value); return gs; })
.Where(gs => gs.Address != null);
static GameServer[] Games = { };
static AutoResetEvent ev = new AutoResetEvent(false);
public static void Refresh(string masterServerUrl)
{
new Thread(() =>
{
try
{
var wc = new WebClient();
var data = wc.DownloadData(new Uri(masterServerUrl + "list.php"));
var str = Encoding.UTF8.GetString(data);
var yaml = MiniYaml.FromString(str);
Games = yaml.Select(a => FieldLoader.Load<GameServer>(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);
}
}