Skip to content

Commit

Permalink
- server lists now persist when switching between game tabs
Browse files Browse the repository at this point in the history
- fixed issue with UDP master server queries when more servers than the set limit are returned
- fixed: Using Steam Web API as master server showed tags/keywords/description in wrong columns / fields
- Reflex Arena: game type and location can now be extracted from the tags with both comma and pipe as separator
- Reflex Arena: added server rules tab
  • Loading branch information
PredatH0r committed Jun 15, 2017
1 parent 4e34900 commit 367d6a7
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 61 deletions.
2 changes: 1 addition & 1 deletion QueryMaster/QueryMaster/MasterServer.cs
Expand Up @@ -9,7 +9,7 @@ namespace QueryMaster
/// Invoked when a reply from Master Server is received.
/// </summary>
/// <param name="endPoints">Server Sockets</param>
public delegate void MasterIpCallback(ReadOnlyCollection<Tuple<IPEndPoint,ServerInfo>> endPoints, Exception error);
public delegate void MasterIpCallback(ReadOnlyCollection<Tuple<IPEndPoint,ServerInfo>> endPoints, Exception error, bool isPartialResult);

/// <summary>
/// Provides methods to query master server.
Expand Down
30 changes: 20 additions & 10 deletions QueryMaster/QueryMaster/MasterServerUdp.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -34,31 +35,41 @@ public override void GetAddresses(Region region, MasterIpCallback callback, IpFi
{
var udpSocket = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, ProtocolType.Udp);
udpSocket.SendTimeout = 500;
udpSocket.ReceiveTimeout = 1000;
udpSocket.ReceiveTimeout = 500;
udpSocket.Connect(endPoint);
byte[] recvData = new byte[1400];
try
{
var nextSeed = SeedEndpoint;
int totalCount = 0;
do
bool atEnd = false;
while (!atEnd)
{
var curSeed = nextSeed;
var endpoints = Util.RunWithRetries(() => SendAndReceive(udpSocket, recvData, region, filter, curSeed), this.Retries);
if (endpoints == null)
{
callback(null, null);
callback(null, null, false);
break;
}
ThreadPool.QueueUserWorkItem(y => callback(endpoints, null));
nextSeed = endpoints.Last();
atEnd = nextSeed.Equals(SeedEndpoint);
var serverList = endpoints.Select(ep => new Tuple<IPEndPoint, ServerInfo>(ep, null)).ToList();
if (atEnd)
serverList.RemoveAt(serverList.Count - 1); // remove the 0.0.0.0:0 end-of-list marker
ThreadPool.QueueUserWorkItem(y => callback(new ReadOnlyCollection<Tuple<IPEndPoint, ServerInfo>>(serverList), null, !atEnd));
totalCount += endpoints.Count;
nextSeed = endpoints.Last().Item1;
} while (!nextSeed.Equals(SeedEndpoint) && totalCount < GetAddressesLimit);
atEnd |= totalCount >= GetAddressesLimit;
} //while (!nextSeed.Equals(SeedEndpoint) && totalCount < GetAddressesLimit);
}
catch (Exception ex)
{
callback(null, ex);
callback(null, ex, false);
}
finally
{
Expand All @@ -68,16 +79,15 @@ public override void GetAddresses(Region region, MasterIpCallback callback, IpFi
});
}

private ReadOnlyCollection<Tuple<IPEndPoint, ServerInfo>> SendAndReceive(Socket udpSocket, byte[] recvData, Region region, IpFilter filter, IPEndPoint seed)
private ReadOnlyCollection<IPEndPoint> SendAndReceive(Socket udpSocket, byte[] recvData, Region region, IpFilter filter, IPEndPoint seed)
{
var msg = MasterUtil.BuildPacket(seed.ToString(), region, filter);
udpSocket.Send(msg);

int len = udpSocket.Receive(recvData, 0, recvData.Length, SocketFlags.None);
byte[] data = new byte[len];
Array.Copy(recvData, data, data.Length);
var endpoints = MasterUtil.ProcessPacket(data);
return new ReadOnlyCollection<Tuple<IPEndPoint, ServerInfo>>(endpoints.Select(ep => new Tuple<IPEndPoint, ServerInfo>(ep, null)).ToList());
return MasterUtil.ProcessPacket(data);
}
}
}
7 changes: 4 additions & 3 deletions QueryMaster/QueryMaster/MasterServerWebApi.cs
Expand Up @@ -88,12 +88,12 @@ public override void GetAddresses(Region region, MasterIpCallback callback, IpFi
{
}
}
callback(new ReadOnlyCollection<Tuple<IPEndPoint,ServerInfo>>(endpoints), null);
callback(new ReadOnlyCollection<Tuple<IPEndPoint,ServerInfo>>(endpoints), null, false);
}
}
catch(Exception ex)
{
callback(null, ex);
callback(null, ex, false);
}
});
}
Expand All @@ -103,10 +103,11 @@ private ServerInfo ConvertToServerInfo(Message msg)
var si = new ServerInfo();
si.Address = msg.addr;
si.Bots = (byte)msg.bots;
si.Description = msg.gametype;
si.Description = msg.product;
si.Directory = msg.gamedir;
si.Environment = msg.os == "w" ? "Windows" : msg.os == "l" ? "Linux" : msg.os;
si.Extra.GameId = msg.appid;
si.Extra.Keywords = msg.gametype;
si.Extra.Port = msg.gameport;
si.Extra.SteamID = msg.steamid;
si.GameVersion = msg.version;
Expand Down
6 changes: 3 additions & 3 deletions ServerBrowser/Games/Reflex.cs
Expand Up @@ -20,7 +20,7 @@ public class Reflex : GameExtension
public Reflex()
{
// Reflex doesn't reply to A2S_GETRULES queries and would thus show "timeout" for all servers.
this.supportsRulesQuery = false;
//this.supportsRulesQuery = false;
this.OptionMenuCaption = "Reflex...";
}

Expand Down Expand Up @@ -72,12 +72,12 @@ public override object GetServerCellValue(ServerRow row, string fieldName)
{
if (fieldName == "_gametype")
{
var parts = row.ServerInfo?.Extra.Keywords?.Split('|');
var parts = row.ServerInfo?.Extra.Keywords?.Split(',', '|');
return parts?[0];
}
if (fieldName == "_location")
{
var parts = row.ServerInfo?.Extra.Keywords?.Split('|');
var parts = row.ServerInfo?.Extra.Keywords?.Split(',', '|');
return parts?.Length > 1 ? parts[1] : null;
}

Expand Down
61 changes: 33 additions & 28 deletions ServerBrowser/ServerBrowserForm.cs
Expand Up @@ -31,7 +31,7 @@ namespace ServerBrowser
{
public partial class ServerBrowserForm : XtraForm
{
private const string Version = "2.44";
private const string Version = "2.45";
private const string DevExpressVersion = "v15.2";
private const string SteamWebApiText = "<Steam Web API>";
private const string CustomDetailColumnPrefix = "ServerInfo.";
Expand Down Expand Up @@ -100,6 +100,7 @@ public ServerBrowserForm(IniFile ini)

var vm = new TabViewModel();
vm.Source = TabViewModel.SourceType.Favorites;
vm.Caption = this.tabFavorites.Text;
this.tabFavorites.Tag = vm;
}
#endregion
Expand Down Expand Up @@ -325,7 +326,7 @@ private void AddNewTab(string name, TabViewModel.SourceType sourceType)
{
var vm = new TabViewModel();
vm.Source = sourceType;
vm.servers = new List<ServerRow>();
vm.Servers = new List<ServerRow>();
vm.gameExtension = unknownGame;

var page = new XtraTabPage();
Expand All @@ -347,12 +348,12 @@ private void SetViewModel(TabViewModel vm)
this.viewModel = vm;
if (vm.Source == TabViewModel.SourceType.Favorites)
{
vm.servers = new List<ServerRow>();
vm.Servers = new List<ServerRow>();
foreach (var fav in this.favServers)
{
var row = new ServerRow(fav.Key, this.extenders.Get(0));
row.CachedName = fav.Value;
vm.servers.Add(row);
vm.Servers.Add(row);
}
}

Expand All @@ -378,6 +379,8 @@ private void SetViewModel(TabViewModel vm)
this.txtVersion.Text = vm.VersionMatch;

UpdatePanelVisibility();
this.isSingleRowUpdate = false;
UpdateViews(true);
this.miFindServers.Enabled = vm.Source == TabViewModel.SourceType.MasterServer;
}
#endregion
Expand Down Expand Up @@ -637,7 +640,10 @@ protected virtual void UpdateViewModel()
var vm = this.viewModel;
if (vm == null)
return;

vm.MasterServer = this.comboMasterServer.Text;
if (vm.Source == TabViewModel.SourceType.MasterServer && !this.queryLogic.IsCancelled)
vm.Servers = this.queryLogic.Servers;

if (this.comboGames.SelectedIndex < 0)
{
Expand Down Expand Up @@ -836,9 +842,9 @@ private void RefreshServerInfo()
this.isSingleRowUpdate = false;
this.miStopUpdate.Enabled = true;
this.timerReloadServers.Stop();
this.SetStatusMessage("Updating status of " + this.viewModel.servers.Count + " servers...");
this.SetStatusMessage("Updating status of " + this.viewModel.Servers.Count + " servers...");
this.RefreshGameExtensions();
this.queryLogic.RefreshAllServers(this.viewModel.servers);
this.queryLogic.RefreshAllServers(this.viewModel.Servers);
}
#endregion

Expand Down Expand Up @@ -969,14 +975,14 @@ protected void UpdateViews(bool forceUpdateDetails = false)
this.LookupGeoIps();
this.UpdateCachedServerNames();

this.gcServers.DataSource = this.viewModel.servers;
this.gcServers.DataSource = this.viewModel.Servers;
this.gvServers.EndDataUpdate();
this.gvServers.TopRowIndex = topRow;

if (this.viewModel.lastSelectedServer != null)
{
int i = 0;
foreach (var server in this.viewModel.servers)
foreach (var server in this.viewModel.Servers)
{
if (server.EndPoint.Equals(this.viewModel.lastSelectedServer.EndPoint))
{
Expand Down Expand Up @@ -1017,9 +1023,9 @@ private void RefreshDataInSelectedRows()
#region UpdateCachedServerNames()
private void UpdateCachedServerNames()
{
if (this.viewModel?.servers != null)
if (this.viewModel?.Servers != null)
{
foreach (var server in this.viewModel.servers)
foreach (var server in this.viewModel.Servers)
{
if (server.ServerInfo?.Name != null && this.favServers.ContainsKey(server.EndPoint))
this.favServers[server.EndPoint] = server.CachedName = server.ServerInfo.Name;
Expand Down Expand Up @@ -1237,8 +1243,8 @@ private void AddColumnForRuleToServerGrid(string prefix, UnboundColumnType unbou
#region LookupGeoIps()
private void LookupGeoIps()
{
if (this.viewModel?.servers == null) return;
foreach (var server in this.viewModel.servers)
if (this.viewModel?.Servers == null) return;
foreach (var server in this.viewModel.Servers)
{
if (server.GeoInfo != null)
continue;
Expand Down Expand Up @@ -1373,7 +1379,7 @@ protected virtual void queryLogic_SetStatusMessage(object sender, TextEventArgs
#region queryLogic_ServerListReceived
private void queryLogic_ServerListReceived()
{
this.viewModel.servers = this.queryLogic.Servers;
this.viewModel.Servers = this.queryLogic.Servers;
this.UpdateViews();
}
#endregion
Expand All @@ -1383,7 +1389,7 @@ protected virtual void queryLogic_ReloadServerListComplete(List<ServerRow> rows)
{
this.UpdateBuddyCount();
this.UpdateViews();
this.SetStatusMessage("Update of " + this.viewModel.servers.Count + " servers complete");
this.SetStatusMessage("Update of " + this.viewModel.Servers.Count + " servers complete");
this.miStopUpdate.Enabled = false;
this.CheckAlertCondition();
if (this.spinRefreshInterval.Value > 0)
Expand All @@ -1397,7 +1403,7 @@ protected virtual void queryLogic_ReloadServerListComplete(List<ServerRow> rows)
protected virtual void queryLogic_RefreshSingleServerComplete(ServerEventArgs e)
{
this.UpdateBuddyCount(e.Server);
var idx = this.viewModel?.servers?.IndexOf(e.Server) ?? -1;
var idx = this.viewModel?.Servers?.IndexOf(e.Server) ?? -1;
if (idx >= 0)
this.gvServers.RefreshRow(this.gvServers.GetRowHandle(idx));
this.UpdateGridDataSources();
Expand Down Expand Up @@ -1706,7 +1712,7 @@ private void txtGameServer_ButtonClick(object sender, ButtonPressedEventArgs e)
var endpoint = new IPEndPoint(addr[i], parts.Length > 1 ? int.Parse(parts[1]) : 27015);
if (endpoint.Address.ToString() == "0.0.0.0") return;
ServerRow serverRow = null;
foreach (var row in this.viewModel.servers)
foreach (var row in this.viewModel.Servers)
{
if (row.EndPoint.Equals(endpoint))
{
Expand All @@ -1719,10 +1725,10 @@ private void txtGameServer_ButtonClick(object sender, ButtonPressedEventArgs e)
{
this.gvServers.BeginDataUpdate();
serverRow = new ServerRow(endpoint, this.extenders.Get(0));
this.viewModel.servers.Add(serverRow);
this.viewModel.Servers.Add(serverRow);
this.gvServers.EndDataUpdate();
serverRow.SetModified();
var handle = this.gvServers.GetRowHandle(this.viewModel.servers.Count - 1);
var handle = this.gvServers.GetRowHandle(this.viewModel.Servers.Count - 1);
this.gvServers.FocusedRowHandle = handle;
this.gvServers.SelectRow(handle);
}
Expand Down Expand Up @@ -1831,7 +1837,7 @@ private void timerUpdateServerList_Tick(object sender, EventArgs e)
#region gvServers_CustomRowFilter
private void gvServers_CustomRowFilter(object sender, RowFilterEventArgs e)
{
var row = this.viewModel.servers[e.ListSourceRow];
var row = this.viewModel.Servers[e.ListSourceRow];

if (FilterServerRow(row))
{
Expand Down Expand Up @@ -1884,9 +1890,9 @@ private void gvServers_CustomColumnDisplayText(object sender, CustomColumnDispla
{
if (e.Column == this.colLocation)
{
if (e.ListSourceRowIndex < 0 || e.ListSourceRowIndex >= this.viewModel.servers.Count)
if (e.ListSourceRowIndex < 0 || e.ListSourceRowIndex >= this.viewModel.Servers.Count)
return;
var row = this.viewModel.servers[e.ListSourceRowIndex];
var row = this.viewModel.Servers[e.ListSourceRowIndex];
var geoInfo = row.GeoInfo;
if (geoInfo != null && geoInfo.Iso2 == "US" && !string.IsNullOrEmpty(geoInfo.State))
e.DisplayText = geoInfo.State;
Expand Down Expand Up @@ -2079,7 +2085,7 @@ private void miDelete_ItemClick(object sender, ItemClickEventArgs e)

int offset = 0;
foreach (var index in indices)
this.viewModel.servers.RemoveAt(index - offset++);
this.viewModel.Servers.RemoveAt(index - offset++);
this.UpdateViews();
}
#endregion
Expand Down Expand Up @@ -2118,12 +2124,12 @@ private void miPasteAddress_ItemClick(object sender, ItemClickEventArgs e)
if (regex.IsMatch(addr))
{
var endpoint = Ip4Utils.ParseEndpoint(addr);
var row = this.viewModel.servers.FirstOrDefault(r => r.EndPoint.Equals(endpoint));
var row = this.viewModel.Servers.FirstOrDefault(r => r.EndPoint.Equals(endpoint));
if (row == null)
{
row = new ServerRow(endpoint, unknownGame);
row.SetModified();
this.viewModel.servers.Add(row);
this.viewModel.Servers.Add(row);
this.queryLogic.RefreshSingleServer(row);
}
}
Expand Down Expand Up @@ -2392,9 +2398,8 @@ private void tabControl_SelectedPageChanged(object sender, TabPageChangedEventAr
{
if (this.ignoreUiEvents > 0) return;
this.SetViewModel((TabViewModel)e.Page.Tag);
this.UpdateViews(true);

if (this.viewModel.servers == null)
if (this.viewModel.Servers == null)
this.ReloadServerList();
else if (this.viewModel.Source != TabViewModel.SourceType.MasterServer)
this.RefreshServerInfo();
Expand Down Expand Up @@ -2494,9 +2499,9 @@ private void miCreateSnapshot_ItemClick(object sender, ItemClickEventArgs e)
page.Tag = vm;
page.ImageIndex = vm.ImageIndex;

vm.servers = new List<ServerRow>();
vm.Servers = new List<ServerRow>();
for(int i=0, c= this.gvServers.RowCount; i<c; i++)
vm.servers.Add((ServerRow)this.gvServers.GetRow(i));
vm.Servers.Add((ServerRow)this.gvServers.GetRow(i));

this.tabControl.TabPages.Insert(this.tabControl.TabPages.Count - 1, page);
this.tabControl.SelectedTabPage = page;
Expand Down

0 comments on commit 367d6a7

Please sign in to comment.