async server browser
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user