diff options
author | sr55 <[email protected]> | 2020-04-10 20:28:16 +0100 |
---|---|---|
committer | sr55 <[email protected]> | 2020-04-10 20:28:16 +0100 |
commit | b0a2c68ee60bd3d15889fa41cf1879c276a9e1b8 (patch) | |
tree | 52cf128eb55652d65d944e96e43c685b0c15da9f /win/CS/HandBrakeWPF/Instance/RemoteInstance.cs | |
parent | 0abc8cb07a4d6ca27d0a0c300a78957b610b773e (diff) |
WinGui:
- Adding multi-instance support in the UI for the worker process.
- Handling port conflicts better. It will now try up to 100 ports from the default port set in preferences.
Diffstat (limited to 'win/CS/HandBrakeWPF/Instance/RemoteInstance.cs')
-rw-r--r-- | win/CS/HandBrakeWPF/Instance/RemoteInstance.cs | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs b/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs index 847e9cf4c..1d11c1cce 100644 --- a/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs +++ b/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs @@ -11,8 +11,12 @@ namespace HandBrakeWPF.Instance { using System; + using System.Collections.Generic; using System.Diagnostics; using System.IO; + using System.Linq; + using System.Net; + using System.Net.NetworkInformation; using System.Threading.Tasks; using System.Timers; using System.Windows.Media.Animation; @@ -46,7 +50,7 @@ namespace HandBrakeWPF.Instance private readonly ILog logService; - private const double EncodePollIntervalMs = 250; + private const double EncodePollIntervalMs = 500; private Process workerProcess; private Timer encodePollTimer; @@ -56,7 +60,7 @@ namespace HandBrakeWPF.Instance { this.configuration = configuration; this.logService = logService; - this.port = configuration.RemoteServicePort; + this.port = this.GetOpenPort(this.configuration.RemoteServicePort); this.serverUrl = string.Format("http://127.0.0.1:{0}/", this.port); } @@ -139,7 +143,7 @@ namespace HandBrakeWPF.Instance StartInfo = { FileName = "HandBrake.Worker.exe", - Arguments = string.Format(" --port={0}", this.port), + Arguments = string.Format(" --port={0}", port), UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, @@ -155,7 +159,7 @@ namespace HandBrakeWPF.Instance workerProcess.BeginErrorReadLine(); - this.logService.LogMessage(string.Format("Worker Process started with Process ID: {0}", this.workerProcess.Id)); + this.logService.LogMessage(string.Format("Worker Process started with Process ID: {0} and port: {1}", this.workerProcess.Id, port)); } } @@ -267,5 +271,25 @@ namespace HandBrakeWPF.Instance this.EncodeCompleted?.Invoke(sender: this, e: new EncodeCompletedEventArgs(state.WorkDone.Error)); } } + + private int GetOpenPort(int startPort) + { + int portStartIndex = startPort; + + IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); + IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners(); + + List<int> usedPorts = tcpEndPoints.Select(p => p.Port).ToList<int>(); + int unusedPort = 0; + + unusedPort = Enumerable.Range(portStartIndex, 99).FirstOrDefault(p => !usedPorts.Contains(p)); + + if (startPort != unusedPort) + { + this.logService.LogMessage(string.Format("Port {0} in use. Using {1} instead", startPort, unusedPort)); + } + + return unusedPort; + } } } |