private Cap CreateCap(object sender, string uri)
{
if (_disposed) return null;
if (this.State == ManagerState.Stop) return null;
if (!uri.StartsWith("i2p:")) return null;
var garbages = new List<IDisposable>();
try
{
string scheme = null;
string host = null;
{
var regex = new Regex(@"(.*?):(.*)");
var match = regex.Match(uri);
if (match.Success)
{
scheme = match.Groups[1].Value;
host = match.Groups[2].Value;
}
}
if (host == null) return null;
{
string proxyScheme = null;
string proxyHost = null;
int proxyPort = -1;
{
var regex = new Regex(@"(.*?):(.*):(\d*)");
var match = regex.Match(this.SamBridgeUri);
if (match.Success)
{
proxyScheme = match.Groups[1].Value;
proxyHost = match.Groups[2].Value;
proxyPort = int.Parse(match.Groups[3].Value);
}
}
if (proxyHost == null) return null;
if (scheme == "i2p")
{
Socket socket = null;
try
{
socket = _samManager.Connect(host);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
if (socket != null) socket.Dispose();
throw;
}
return new SocketCap(socket);
}
}
}
catch (SamException)
{
foreach (var item in garbages)
{
item.Dispose();
}
}
return null;
}