Skip to content

Commit

Permalink
Replace EndPoint with DnsEndPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Jun 8, 2018
1 parent 6d41f90 commit 6b78cf5
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 82 deletions.
23 changes: 3 additions & 20 deletions Enyim.Caching/Configuration/ConfigurationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void CheckForInterface(Type type, Type interfaceType)
// throw new System.Configuration.ConfigurationErrorsException("The type " + type.AssemblyQualifiedName + " must implement " + interfaceType.AssemblyQualifiedName);
}

public static EndPoint ResolveToEndPoint(string value)
public static DnsEndPoint ResolveToEndPoint(string value)
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
Expand All @@ -86,25 +86,8 @@ public static EndPoint ResolveToEndPoint(string value)
if (!Int32.TryParse(parts[1], out port))
throw new ArgumentException("Cannot parse port: " + parts[1], "value");

return ResolveToEndPoint(parts[0], port);
}

public static EndPoint ResolveToEndPoint(string host, int port)
{
if (String.IsNullOrEmpty(host))
throw new ArgumentNullException("host");

IPAddress address;
// parse as an IP address
if (!IPAddress.TryParse(host, out address))
{
var addresses = Dns.GetHostAddresses(host);
address = addresses.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
if (address == null)
throw new ArgumentException(String.Format("Could not resolve host '{0}'.", host));
}
return new IPEndPoint(address, port);
}
return new DnsEndPoint(parts[0], port);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IMemcachedClientConfiguration
/// <summary>
/// Gets a list of <see cref="T:IPEndPoint"/> each representing a Memcached server in the pool.
/// </summary>
IList<EndPoint> Servers { get; }
IList<DnsEndPoint> Servers { get; }

/// <summary>
/// Gets the configuration of the socket pool.
Expand Down
18 changes: 5 additions & 13 deletions Enyim.Caching/Configuration/MemcachedClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,10 @@ public class MemcachedClientConfiguration : IMemcachedClientConfiguration
configuration.GetSection("enyimMemcached").Bind(options);
}

Servers = new List<EndPoint>();
Servers = new List<DnsEndPoint>();
foreach (var server in options.Servers)
{
IPAddress address;
if (IPAddress.TryParse(server.Address, out address))
{
Servers.Add(new IPEndPoint(address, server.Port));
}
else
{
Servers.Add(new DnsEndPoint(server.Address, server.Port));
}
Servers.Add(new DnsEndPoint(server.Address, server.Port));
}

SocketPool = new SocketPoolConfiguration();
Expand Down Expand Up @@ -187,13 +179,13 @@ public void AddServer(string address)
/// <param name="port">The port number of the memcached instance.</param>
public void AddServer(string host, int port)
{
this.Servers.Add(ConfigurationHelper.ResolveToEndPoint(host, port));
this.Servers.Add(new DnsEndPoint(host, port));
}

/// <summary>
/// Gets a list of <see cref="T:IPEndPoint"/> each representing a Memcached server in the pool.
/// </summary>
public IList<EndPoint> Servers { get; private set; }
public IList<DnsEndPoint> Servers { get; private set; }

/// <summary>
/// Gets the configuration of the socket pool.
Expand Down Expand Up @@ -250,7 +242,7 @@ public ITranscoder Transcoder

#region [ interface ]

IList<System.Net.EndPoint> IMemcachedClientConfiguration.Servers
IList<System.Net.DnsEndPoint> IMemcachedClientConfiguration.Servers
{
get { return this.Servers; }
}
Expand Down
6 changes: 3 additions & 3 deletions Enyim.Caching/Memcached/DefaultServerPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class DefaultServerPool : IServerPool, IDisposable
catch { }
}

protected virtual IMemcachedNode CreateNode(EndPoint endpoint)
protected virtual IMemcachedNode CreateNode(DnsEndPoint endpoint)
{
return new MemcachedNode(endpoint, this.configuration.SocketPool, _logger);
}
Expand Down Expand Up @@ -207,9 +207,9 @@ IEnumerable<IMemcachedNode> IServerPool.GetWorkingNodes()
void IServerPool.Start()
{
this.allNodes = this.configuration.Servers.
Select(ip =>
Select(ep =>
{
var node = this.CreateNode(ip);
var node = this.CreateNode(ep);
node.Failed += this.NodeFail;
return node;
Expand Down
4 changes: 2 additions & 2 deletions Enyim.Caching/Memcached/MemcachedNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public class MemcachedNode : IMemcachedNode

private bool isDisposed;

private EndPoint endPoint;
private DnsEndPoint endPoint;
private ISocketPoolConfiguration config;
private InternalPoolImpl internalPoolImpl;
private bool isInitialized;

public MemcachedNode(
EndPoint endpoint,
DnsEndPoint endpoint,
ISocketPoolConfiguration socketPoolConfig,
ILogger logger
)
Expand Down
46 changes: 5 additions & 41 deletions Enyim.Caching/Memcached/PooledSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public partial class PooledSocket : IDisposable
private Stream inputStream;
private AsyncSocketHelper helper;

public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout, ILogger logger)
public PooledSocket(DnsEndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout, ILogger logger)
{
_logger = logger;

Expand Down Expand Up @@ -59,59 +59,23 @@ public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan rece
this.inputStream = new BasicNetworkStream(socket);
}

private void ConnectWithTimeout(Socket socket, EndPoint endpoint, int timeout)
private void ConnectWithTimeout(Socket socket, DnsEndPoint endpoint, int timeout)
{
//var task = socket.ConnectAsync(endpoint);
//if(!task.Wait(timeout))
//{
// using (socket)
// {
// throw new TimeoutException("Could not connect to " + endpoint);
// }
//}

if (endpoint is DnsEndPoint && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var dnsEndPoint = ((DnsEndPoint)endpoint);
var host = dnsEndPoint.Host;
var addresses = Dns.GetHostAddresses(dnsEndPoint.Host);
var address = addresses.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
if (address == null)
{
throw new ArgumentException(String.Format("Could not resolve host '{0}'.", host));
}
_logger.LogDebug($"Resolved '{host}' to '{address}'");
endpoint = new IPEndPoint(address, dnsEndPoint.Port);
}

var completed = new AutoResetEvent(false);

var args = new SocketAsyncEventArgs();
args.RemoteEndPoint = endpoint;
args.Completed += OnConnectCompleted;
args.UserToken = completed;
socket.ConnectAsync(args);

if (!completed.WaitOne(timeout) || !socket.Connected)
{
using (socket)
{
throw new TimeoutException("Could not connect to " + endpoint);
}
}

/*
var mre = new ManualResetEvent(false);
socket.Connect(endpoint, iar =>
{
try { using (iar.AsyncWaitHandle) socket.EndConnect(iar); }
catch { }
mre.Set();
}, null);
if (!mre.WaitOne(timeout) || !socket.Connected)
using (socket)
throw new TimeoutException("Could not connect to " + endpoint);
*/
}
}

private void OnConnectCompleted(object sender, SocketAsyncEventArgs args)
Expand Down
2 changes: 1 addition & 1 deletion Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class BinaryNode : MemcachedNode
ISaslAuthenticationProvider authenticationProvider;

public BinaryNode(
EndPoint endpoint,
DnsEndPoint endpoint,
ISocketPoolConfiguration config,
ISaslAuthenticationProvider authenticationProvider,
ILogger logger)
Expand Down
2 changes: 1 addition & 1 deletion Enyim.Caching/Memcached/Protocol/Binary/BinaryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public BinaryPool(IMemcachedClientConfiguration configuration, ILogger logger)
_logger = logger;
}

protected override IMemcachedNode CreateNode(EndPoint endpoint)
protected override IMemcachedNode CreateNode(DnsEndPoint endpoint)
{
return new BinaryNode(endpoint, this.configuration.SocketPool, this.authenticationProvider, _logger);
}
Expand Down

0 comments on commit 6b78cf5

Please sign in to comment.