-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathPortFinder.cs
More file actions
76 lines (68 loc) · 2.38 KB
/
Copy pathPortFinder.cs
File metadata and controls
76 lines (68 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
namespace Ocelot.Testing;
public static class PortFinder
{
private const int EndPortRange = 45000;
private static volatile int CurrentPort = 20000;
private static readonly object SyncRoot = new();
//private static readonly ConcurrentBag<int> UsedPorts = new();
/// <summary>
/// Gets a pseudo-random port from the range [<see cref="CurrentPort"/>, <see cref="EndPortRange"/>] for one testing scenario.
/// </summary>
/// <returns>New allocated port.</returns>
/// <exception cref="ExceedingPortRangeException">Critical situation where available ports range has been exceeded.</exception>
public static int GetRandomPort()
{
lock (SyncRoot)
{
ExceedingPortRangeException.ThrowIf(CurrentPort > EndPortRange);
while (!TryUsePort(++CurrentPort));
return CurrentPort;
}
}
/// <summary>
/// Gets the exact number of ports from the range [<see cref="CurrentPort"/>, <see cref="EndPortRange"/>] for one testing scenario.
/// </summary>
/// <param name="count">The number of wanted ports.</param>
/// <returns>Array of allocated ports.</returns>
/// <exception cref="ExceedingPortRangeException">Critical situation where available ports range has been exceeded.</exception>
public static int[] GetPorts(int count)
{
var ports = new int[count];
for (int i = 0; i < count; i++)
{
ports[i] = GetRandomPort();
}
return ports;
}
private static bool TryUsePort(int port)
{
//UsedPorts.Add(port); // TODO Review or remove, now useless
Socket? socket = null;
try
{
var ipe = new IPEndPoint(IPAddress.Loopback, port);
socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipe);
socket.Close();
return true;
}
catch
{
return false;
}
finally
{
socket?.Dispose();
}
}
}
public class ExceedingPortRangeException : Exception
{
public ExceedingPortRangeException()
: base("Cannot find available port to bind to!") { }
public static void ThrowIf(bool condition)
=> _ = condition ? throw new ExceedingPortRangeException() : 0;
}