-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added Geo-IP lookup (API hosted by http://freegeoip.net, flag icon …
…provided by http://www.famfamfam.com) - improved connecting to QuakeLive Testing game servers
- Loading branch information
Showing
12 changed files
with
4,169 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ Thumbs.db | |
ServerBrowser/phgp/ | ||
Main/bin/ | ||
Main/obj/ | ||
ServerBrowser/images/flags/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.