Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions Enyim.Caching/Memcached/PooledSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ void Cancel()
}
cts.Token.Register(Cancel);

_socket.Connect(_endpoint);
try
{
_socket.Connect(_endpoint);
}
catch (PlatformNotSupportedException)
{
var ep = GetIPEndPoint(_endpoint);
_socket.Connect(ep.Address, ep.Port);
}

if (_socket != null)
{
Expand Down Expand Up @@ -94,20 +102,28 @@ public async Task ConnectAsync()
{
bool success = false;

var connTask = _socket.ConnectAsync(_endpoint);

if (await Task.WhenAny(connTask, Task.Delay(_connectionTimeout)) == connTask)
{
await connTask;
}
else
try
{
if (_socket != null)
var connTask = _socket.ConnectAsync(_endpoint);

if (await Task.WhenAny(connTask, Task.Delay(_connectionTimeout)) == connTask)
{
_socket.Dispose();
_socket = null;
await connTask;
}
throw new TimeoutException($"Timeout to connect to {_endpoint}.");
else
{
if (_socket != null)
{
_socket.Dispose();
_socket = null;
}
throw new TimeoutException($"Timeout to connect to {_endpoint}.");
}
}
catch (PlatformNotSupportedException)
{
var ep = GetIPEndPoint(_endpoint);
await _socket.ConnectAsync(ep.Address, ep.Port);
}

if (_socket != null)
Expand Down Expand Up @@ -423,6 +439,27 @@ public async Task WriteAsync(IList<ArraySegment<byte>> buffers)
throw;
}
}

private IPEndPoint GetIPEndPoint(EndPoint endpoint)
{
if (endpoint is DnsEndPoint)
{
var dnsEndPoint = (DnsEndPoint)endpoint;
var address = Dns.GetHostAddresses(dnsEndPoint.Host).FirstOrDefault(ip =>
ip.AddressFamily == AddressFamily.InterNetwork);
if (address == null)
throw new ArgumentException(String.Format("Could not resolve host '{0}'.", endpoint));
return new IPEndPoint(address, dnsEndPoint.Port);
}
else if (endpoint is IPEndPoint)
{
return endpoint as IPEndPoint;
}
else
{
throw new Exception("Not supported EndPoint type");
}
}
}
}

Expand Down