Skip to content

Commit

Permalink
Few improvements:
Browse files Browse the repository at this point in the history
* Complexity of array enumeration changed: n*n in worst case with 2n
* Long to int (timeout) is now checked to avoid timeout errors
  • Loading branch information
Evgeny Gorbovoy authored and Evgeny Gorbovoy committed Apr 15, 2021
1 parent e26b2a3 commit 6347c4a
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,9 @@ private static Socket Connect(string serverName, int port, TimeSpan timeout, boo
// and then all AddressFamily.InterNetworkV6.
// Keeping address families inlined in entire method because parameter pendingDNSInfo is bound on them.
ipAddresses = ipAddresses
.Where(address => address.AddressFamily == AddressFamily.InterNetworkV6 ||
address.AddressFamily == AddressFamily.InterNetwork)
.OrderBy(address => address.AddressFamily == AddressFamily.InterNetworkV6);

.Where (address => address.AddressFamily==AddressFamily.InterNetwork)
.Concat(ipAddresses.Where(address => address.AddressFamily==AddressFamily.InterNetworkV6));

Stopwatch timeTaken = Stopwatch.StartNew();

foreach (IPAddress ipAddress in ipAddresses)
Expand Down Expand Up @@ -367,7 +366,8 @@ private static Socket Connect(string serverName, int port, TimeSpan timeout, boo
return null;
try
{
int connectionTimeout = isInfiniteTimeout? -1 : (int)(timeLeft.TotalMilliseconds * 1000);
int timeLeftTotalMilliseconds = checked((int)(timeLeft.TotalMilliseconds * 1000));
int connectionTimeout = isInfiniteTimeout? -1 : timeLeftTotalMilliseconds;
Socket.Select(null, new List<Socket> {socket}, null,
connectionTimeout);
}
Expand Down

0 comments on commit 6347c4a

Please sign in to comment.