Fix #589 GetProcessByUsedTcpPort multiple process

This commit is contained in:
ChsBuffer
2021-03-22 21:59:39 +08:00
parent 3f4a31dac8
commit 9a3a1e3664
2 changed files with 4 additions and 7 deletions

View File

@@ -191,8 +191,7 @@ namespace Netch.Controllers
public static void TryReleaseTcpPort(ushort port, string portName)
{
Process? p;
if ((p = PortHelper.GetProcessByUsedTcpPort(port)) != null)
foreach (var p in PortHelper.GetProcessByUsedTcpPort(port))
{
if (p.MainModule!.FileName.StartsWith(Global.NetchDir))
{

View File

@@ -28,16 +28,14 @@ namespace Netch.Utils
}
}
public static Process? GetProcessByUsedTcpPort(ushort port)
public static IEnumerable<Process> GetProcessByUsedTcpPort(ushort port)
{
if (port == 0)
throw new ArgumentOutOfRangeException();
var row = GetTcpTable2().SingleOrDefault(r => ntohs((ushort) r.dwLocalPort) == port);
if (row.dwOwningPid == 0)
return null;
var row = GetTcpTable2().Where(r => ntohs((ushort) r.dwLocalPort) == port);
return Process.GetProcessById((int) row.dwOwningPid);
return row.Select(r => Process.GetProcessById((int) r.dwOwningPid));
}
private static void GetReservedPortRange(PortType portType, ref List<Range> targetList)