/
MyHelper.cs
24 lines (19 loc) · 1.11 KB
/
MyHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Net.NetworkInformation;
using Bbob.Plugin;
namespace bbob_plugin_prerender;
public static class MyHelper
{
public static readonly string prerenderDirectory = Path.Combine(PluginHelper.CurrentDirectory, "prerender");
public static int GetAvailablePort(int startingPort)
{
if (startingPort > ushort.MaxValue) throw new ArgumentException($"Can't be greater than {ushort.MaxValue}", nameof(startingPort));
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
var connectionsEndpoints = ipGlobalProperties.GetActiveTcpConnections().Select(c => c.LocalEndPoint);
var tcpListenersEndpoints = ipGlobalProperties.GetActiveTcpListeners();
var udpListenersEndpoints = ipGlobalProperties.GetActiveUdpListeners();
var portsInUse = connectionsEndpoints.Concat(tcpListenersEndpoints)
.Concat(udpListenersEndpoints)
.Select(e => e.Port);
return Enumerable.Range(startingPort, ushort.MaxValue - startingPort + 1).Except(portsInUse).FirstOrDefault();
}
}