|
4 | 4 | using System.Data; |
5 | 5 | using System.Diagnostics; |
6 | 6 | using System.IO; |
7 | | -using System.Linq; |
8 | 7 | using System.Net; |
9 | 8 | using System.Net.Security; |
10 | 9 | using System.Net.Sockets; |
@@ -958,7 +957,7 @@ void Connect(NpgsqlTimeout timeout) |
958 | 957 | // Note that there aren't any timeout-able or cancellable DNS methods |
959 | 958 | var endpoints = NpgsqlConnectionStringBuilder.IsUnixSocket(Host, Port, out var socketPath) |
960 | 959 | ? new EndPoint[] { new UnixDomainSocketEndPoint(socketPath) } |
961 | | - : Dns.GetHostAddresses(Host).Select(a => new IPEndPoint(a, Port)).ToArray(); |
| 960 | + : IPAddressesToEndpoints(Dns.GetHostAddresses(Host), Port); |
962 | 961 | timeout.Check(); |
963 | 962 |
|
964 | 963 | // Give each endpoint an equal share of the remaining time |
@@ -997,7 +996,7 @@ void Connect(NpgsqlTimeout timeout) |
997 | 996 | var errorCode = (int) socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error)!; |
998 | 997 | if (errorCode != 0) |
999 | 998 | throw new SocketException(errorCode); |
1000 | | - if (!write.Any()) |
| 999 | + if (write.Count is 0) |
1001 | 1000 | throw new TimeoutException("Timeout during connection attempt"); |
1002 | 1001 | socket.Blocking = true; |
1003 | 1002 | SetSocketOptions(socket); |
@@ -1035,8 +1034,8 @@ Task<IPAddress[]> GetHostAddressesAsync(CancellationToken ct) => |
1035 | 1034 | // and raises the exception, while the actual task may be left running. |
1036 | 1035 | var endpoints = NpgsqlConnectionStringBuilder.IsUnixSocket(Host, Port, out var socketPath) |
1037 | 1036 | ? new EndPoint[] { new UnixDomainSocketEndPoint(socketPath) } |
1038 | | - : (await TaskTimeoutAndCancellation.ExecuteAsync(GetHostAddressesAsync, timeout, cancellationToken)) |
1039 | | - .Select(a => new IPEndPoint(a, Port)).ToArray(); |
| 1037 | + : IPAddressesToEndpoints(await TaskTimeoutAndCancellation.ExecuteAsync(GetHostAddressesAsync, timeout, cancellationToken), |
| 1038 | + Port); |
1040 | 1039 |
|
1041 | 1040 | // Give each IP an equal share of the remaining time |
1042 | 1041 | var perIpTimespan = default(TimeSpan); |
@@ -1103,6 +1102,14 @@ Task ConnectAsync(CancellationToken ct) => |
1103 | 1102 | } |
1104 | 1103 | } |
1105 | 1104 |
|
| 1105 | + IPEndPoint[] IPAddressesToEndpoints(IPAddress[] ipAddresses, int port) |
| 1106 | + { |
| 1107 | + var result = new IPEndPoint[ipAddresses.Length]; |
| 1108 | + for (var i = 0; i < ipAddresses.Length; i++) |
| 1109 | + result[i] = new IPEndPoint(ipAddresses[i], port); |
| 1110 | + return result; |
| 1111 | + } |
| 1112 | + |
1106 | 1113 | void SetSocketOptions(Socket socket) |
1107 | 1114 | { |
1108 | 1115 | if (socket.AddressFamily == AddressFamily.InterNetwork || socket.AddressFamily == AddressFamily.InterNetworkV6) |
|
0 commit comments