LAN games discovery

This commit is contained in:
rob-v
2017-04-19 10:20:51 +02:00
committed by reaperrr
parent b2e6a0484b
commit ffc3f6e0d0
11 changed files with 132 additions and 27 deletions

View File

@@ -41,6 +41,10 @@
<HintPath>..\thirdparty\download\FuzzyLogicLibrary.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="rix0rrr.BeaconLib, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\thirdparty\download\rix0rrr.BeaconLib.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />

View File

@@ -14,44 +14,60 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using BeaconLib;
using OpenRA.Server;
using S = OpenRA.Server.Server;
namespace OpenRA.Mods.Common.Server
{
public class MasterServerPinger : ServerTrait, ITick, INotifySyncLobbyInfo, IStartGame, IEndGame
public class MasterServerPinger : ServerTrait, ITick, INotifyServerStart, INotifySyncLobbyInfo, IStartGame, IEndGame
{
// 3 minutes. Server has a 5 minute TTL for games, so give ourselves a bit of leeway.
const int MasterPingInterval = 60 * 3;
static readonly Beacon LanGameBeacon = new Beacon("OpenRALANGame", (ushort)new Random(DateTime.Now.Millisecond).Next(2048, 60000));
public int TickTimeout { get { return MasterPingInterval * 10000; } }
public void Tick(S server)
{
if ((Game.RunTime - lastPing > MasterPingInterval * 1000) || isInitialPing)
PingMasterServer(server);
else
lock (masterServerMessages)
while (masterServerMessages.Count > 0)
server.SendMessage(masterServerMessages.Dequeue());
}
public void LobbyInfoSynced(S server) { PingMasterServer(server); }
public void GameStarted(S server) { PingMasterServer(server); }
public void GameEnded(S server) { PingMasterServer(server); }
long lastPing = 0;
bool isInitialPing = true;
volatile bool isBusy;
Queue<string> masterServerMessages = new Queue<string>();
public void PingMasterServer(S server)
public void Tick(S server)
{
if (isBusy || !server.Settings.AdvertiseOnline) return;
if ((Game.RunTime - lastPing > MasterPingInterval * 1000) || isInitialPing)
PublishGame(server);
else
lock (masterServerMessages)
while (masterServerMessages.Count > 0)
server.SendMessage(masterServerMessages.Dequeue());
}
lastPing = Game.RunTime;
isBusy = true;
public void ServerStarted(S server)
{
if (!server.Ip.Equals(IPAddress.Loopback))
LanGameBeacon.Start();
}
public void LobbyInfoSynced(S server)
{
PublishGame(server);
}
public void GameStarted(S server)
{
PublishGame(server);
}
public void GameEnded(S server)
{
LanGameBeacon.Stop();
PublishGame(server);
}
void PublishGame(S server)
{
var mod = server.ModData.Manifest;
// important to grab these on the main server thread, not in the worker we're about to spawn -- they may be modified
@@ -60,9 +76,21 @@ namespace OpenRA.Mods.Common.Server
var numBots = server.LobbyInfo.Clients.Where(c1 => c1.Bot != null).Count();
var numSpectators = server.LobbyInfo.Clients.Where(c1 => c1.Bot == null && c1.Slot == null).Count();
var numSlots = server.LobbyInfo.Slots.Where(s => !s.Value.Closed).Count() - numBots;
var passwordProtected = string.IsNullOrEmpty(server.Settings.Password) ? 0 : 1;
var passwordProtected = !string.IsNullOrEmpty(server.Settings.Password);
var clients = server.LobbyInfo.Clients.Where(c1 => c1.Bot == null).Select(c => Convert.ToBase64String(Encoding.UTF8.GetBytes(c.Name))).ToArray();
UpdateMasterServer(server, numPlayers, numSlots, numBots, numSpectators, mod, passwordProtected, clients);
UpdateLANGameBeacon(server, numPlayers, numSlots, numBots, numSpectators, mod, passwordProtected);
}
void UpdateMasterServer(S server, int numPlayers, int numSlots, int numBots, int numSpectators, Manifest mod, bool passwordProtected, string[] clients)
{
if (isBusy || !server.Settings.AdvertiseOnline)
return;
lastPing = Game.RunTime;
isBusy = true;
Action a = () =>
{
try
@@ -84,7 +112,7 @@ namespace OpenRA.Mods.Common.Server
server.LobbyInfo.GlobalSettings.Map,
numSlots,
numSpectators,
passwordProtected,
passwordProtected ? 1 : 0,
string.Join(",", clients)));
if (isInitialPing)
@@ -116,5 +144,28 @@ namespace OpenRA.Mods.Common.Server
a.BeginInvoke(null, null);
}
void UpdateLANGameBeacon(S server, int numPlayers, int numSlots, int numBots, int numSpectators, Manifest mod, bool passwordProtected)
{
var settings = server.Settings;
// TODO: Serialize and send client names
var lanGameYaml =
@"Game:
Id: {0}
Name: {1}
Address: {2}:{3}
State: {4}
Players: {5}
MaxPlayers: {6}
Bots: {7}
Spectators: {8}
Map: {9}
Mods: {10}@{11}
Protected: {12}".F(Platform.SessionGUID, settings.Name, server.Ip, settings.ListenPort, (int)server.State, numPlayers, numSlots, numBots, numSpectators,
server.Map.Uid, mod.Id, mod.Metadata.Version, passwordProtected);
LanGameBeacon.BeaconData = lanGameYaml;
}
}
}

View File

@@ -10,12 +10,12 @@
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using BeaconLib;
using OpenRA.Network;
using OpenRA.Server;
using OpenRA.Widgets;
@@ -39,6 +39,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly Color incompatibleGameColor;
readonly ModData modData;
readonly WebServices services;
readonly Probe lanGameProbe;
GameServer currentServer;
MapPreview currentMap;
@@ -53,6 +54,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
SearchStatus searchStatus = SearchStatus.Fetching;
Download currentQuery;
Widget serverList;
IEnumerable<BeaconLocation> lanGameLocations;
public string ProgressLabelText()
{
@@ -109,6 +111,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
widget.Get<ButtonWidget>("BACK_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };
Game.LoadWidget(null, "GLOBALCHAT_PANEL", widget.Get("GLOBALCHAT_ROOT"), new WidgetArgs());
lanGameLocations = new List<BeaconLocation>();
lanGameProbe = new Probe("OpenRALANGame");
lanGameProbe.BeaconsUpdated += locations => lanGameLocations = locations;
lanGameProbe.Start();
RefreshServerList();
if (directConnectHost != null)
@@ -322,7 +329,29 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
}
Game.RunAfterTick(() => RefreshServerListInner(games));
var lanGames = new List<GameServer>();
foreach (var bl in lanGameLocations)
{
var game = MiniYaml.FromString(bl.Data)[0].Value;
var idNode = game.Nodes.FirstOrDefault(n => n.Key == "Id");
// Skip beacons created by this instance and replace Id by expected int value
if (idNode != null && idNode.Value.Value != Platform.SessionGUID.ToString())
{
idNode.Value.Value = "-1";
// Rewrite the server address with the correct IP
var addressNode = game.Nodes.FirstOrDefault(n => n.Key == "Address");
if (addressNode != null)
addressNode.Value.Value = bl.Address.ToString().Split(':')[0] + ":" + addressNode.Value.Value.Split(':')[1];
lanGames.Add(new GameServer(game));
}
}
lanGames = lanGames.GroupBy(gs => gs.Address).Select(g => g.Last()).ToList();
Game.RunAfterTick(() => RefreshServerListInner(games.Concat(lanGames).ToList()));
};
var queryURL = services.ServerList + "games?version={0}&mod={1}&modversion={2}".F(
@@ -444,7 +473,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (location != null)
{
var font = Game.Renderer.Fonts[location.Font];
var cachedServerLocation = GeoIP.LookupCountry(game.Address.Split(':')[0]);
var cachedServerLocation = game.Id != -1 ? GeoIP.LookupCountry(game.Address.Split(':')[0]) : "Local Network";
var label = WidgetUtils.TruncateText(cachedServerLocation, location.Bounds.Width, font);
location.GetText = () => label;
location.GetColor = () => canJoin ? location.TextColor : incompatibleGameColor;