Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/2881099/FreeRedis
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Mar 29, 2022
2 parents a828299 + 0ab93a6 commit c182d4c
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/FreeRedis/Internal/DefaultRedisSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ public void Connect()
{
ResetHost(Host);

IPEndPoint endpoint = IPAddress.TryParse(_ip, out var tryip) ?
new IPEndPoint(tryip, _port) :
new IPEndPoint(Dns.GetHostAddresses(_ip).FirstOrDefault() ?? IPAddress.Parse(_ip), _port);
var localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
EndPoint endpoint = ParseEndPoint(_ip, _port);
var localSocket = endpoint.AddressFamily == AddressFamily.InterNetworkV6 ?
new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp):
new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

var asyncResult = localSocket.BeginConnect(endpoint, null, null);
if (!asyncResult.AsyncWaitHandle.WaitOne(ConnectTimeout, true))
Expand Down Expand Up @@ -268,5 +268,20 @@ public static KeyValuePair<string, int> SplitHost(string host)

return new KeyValuePair<string, int>(host, 6379);
}

private static EndPoint ParseEndPoint(string ip, int port)
{
if (IPAddress.TryParse(ip, out var tryip))
{
return new IPEndPoint(tryip, port);
}

if (Dns.GetHostAddresses(ip).Length == 0)
{
throw new Exception($"无法解析“{ip}”");
}

return new DnsEndPoint(ip, port);
}
}
}

0 comments on commit c182d4c

Please sign in to comment.