Skip to content

Commit

Permalink
- added Geo-IP lookup (API hosted by http://freegeoip.net, flag icon …
Browse files Browse the repository at this point in the history
…provided by http://www.famfamfam.com)

- improved connecting to QuakeLive Testing game servers
  • Loading branch information
PredatH0r committed Jul 14, 2015
1 parent b5e50c1 commit 2408465
Show file tree
Hide file tree
Showing 12 changed files with 4,169 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Thumbs.db
ServerBrowser/phgp/
Main/bin/
Main/obj/
ServerBrowser/images/flags/*
2 changes: 1 addition & 1 deletion Main/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Main
{
public static class Program
{
private const string Version = "1.11";
private const string Version = "1.12";

[STAThread]
static void Main()
Expand Down
99 changes: 99 additions & 0 deletions ServerBrowser/GeoIpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Threading;

namespace ServerBrowser
{
class GeoIpClient
{
private const int ThreadCount = 10;
private const string DefaultServiceUrlFormat = "http://freegeoip.net/csv/{0}";
/// <summary>
/// the cache holds either a string with the 2-letter country ISO code, a simple callback delegate, or a multicast callback delegate
/// </summary>
private readonly Dictionary<uint,object> cache = new Dictionary<uint, object>();
private readonly BlockingCollection<IPAddress> queue = new BlockingCollection<IPAddress>();

public string ServiceUrlFormat { get; set; }

public GeoIpClient()
{
this.ServiceUrlFormat = DefaultServiceUrlFormat;
for (int i=0; i<ThreadCount; i++)
ThreadPool.QueueUserWorkItem(context => this.ProcessLoop());
}

#region ProcessLoop()
private void ProcessLoop()
{
while (true)
{
var ip = this.queue.Take();
if (ip == null)
break;
var ipInt = Ip4Utils.ToInt(ip);
try
{
var url = string.Format(this.ServiceUrlFormat, ip);
using (var client = new XWebClient())
{
var result = client.DownloadString(url);
var callbacks = (Action<string>)cache[ipInt];
string country = this.HandleResult(ipInt, result);
ThreadPool.QueueUserWorkItem(ctx => callbacks(country));
}
}
catch
{
}
}
}
#endregion

#region HandleResult()
private string HandleResult(uint ip, string result)
{
var parts = result.Split(',');
if (parts.Length >= 2)
{
lock (cache)
{
cache[ip] = parts[1];
return parts[1];
}
}
return null;
}
#endregion

#region Lookup()
public void Lookup(IPAddress ip, Action<string> callback)
{
uint ipInt = Ip4Utils.ToInt(ip);
string country;
lock (cache)
{
object cached;
if (!cache.TryGetValue(ipInt, out cached))
{
cache.Add(ipInt, callback);
this.queue.Add(ip);
return;
}
country = cached as string;
if (country == null)
{
var callbacks = (Action<string>) cached;
callbacks += callback;
cache[ipInt] = callbacks;
return;
}
}
callback(country);
}
#endregion

}
}
12 changes: 6 additions & 6 deletions ServerBrowser/Ip4Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ namespace ServerBrowser
{
internal static class Ip4Utils
{
internal static int IpAsInt(IPAddress ip)
public static uint ToInt(IPAddress ip)
{
return IpAsInt(ip.GetAddressBytes());
return ToInt(ip.GetAddressBytes());
}

internal static int IpAsInt(params byte[] bytes)
internal static uint ToInt(params byte[] bytes)
{
if (bytes.Length != 4)
throw new ArgumentException("expected 4 bytes, got " + bytes.Length, "bytes");
return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
return (uint)((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]);
}

internal static bool IsInSubnet(int ip, int bits, byte subnetA, byte subnetB, byte subnetC, byte subnetD)
{
var mask = -1 << bits;
return (ip & mask) == (IpAsInt(subnetA, subnetB, subnetC, subnetD) & mask);
}
return (ip & mask) == (ToInt(subnetA, subnetB, subnetC, subnetD) & mask);
}
}
}
13 changes: 7 additions & 6 deletions ServerBrowser/Properties/licenses.licx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
DevExpress.XtraEditors.ButtonEdit, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraBars.BarManager, DevExpress.XtraBars.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.CheckEdit, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.ComboBoxEdit, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraBars.Ribbon.GalleryControl, DevExpress.XtraBars.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraGrid.GridControl, DevExpress.XtraGrid.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraBars.Docking.DockManager, DevExpress.XtraBars.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.ButtonEdit, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraBars.Ribbon.GalleryControl, DevExpress.XtraBars.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.PictureEdit, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.CheckEdit, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.TextEdit, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraGrid.GridControl, DevExpress.XtraGrid.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit, DevExpress.XtraEditors.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraBars.BarManager, DevExpress.XtraBars.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
1 change: 1 addition & 0 deletions ServerBrowser/ServerBrowser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Games\CounterStrikeGO.cs" />
<Compile Include="GeoIpClient.cs" />
<Compile Include="KeyBindForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
Loading

0 comments on commit 2408465

Please sign in to comment.