Skip to content

Commit

Permalink
WinGui:
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
sr55 committed Apr 10, 2020
1 parent 0abc8cb commit b0a2c68
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
7 changes: 3 additions & 4 deletions win/CS/HandBrake.Worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@ public static void Main(string[] args)
}
}

Console.WriteLine("Starting HandBrake Engine ...");
Console.WriteLine("Worker: Starting HandBrake Engine ...");
router = new ApiRouter();
router.TerminationEvent += Router_TerminationEvent;

Console.WriteLine("Starting Web Server ...");
Console.WriteLine("Using Port: {0}", port);
Console.WriteLine("Worker: Starting Web Server on port {0} ...", port);
Dictionary<string, Func<HttpListenerRequest, string>> apiHandlers = RegisterApiHandlers();
HttpServer webServer = new HttpServer(apiHandlers, port);
webServer.Run();

Console.WriteLine("Web Server Started");
Console.WriteLine("Worker: Server Started");

manualResetEvent.WaitOne();

Expand Down
32 changes: 28 additions & 4 deletions win/CS/HandBrakeWPF/Instance/RemoteInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +50,7 @@ public class RemoteInstance : HttpRequestBase, IEncodeInstance, IDisposable

private readonly ILog logService;

private const double EncodePollIntervalMs = 250;
private const double EncodePollIntervalMs = 500;

private Process workerProcess;
private Timer encodePollTimer;
Expand All @@ -56,7 +60,7 @@ public RemoteInstance(HBConfiguration configuration, ILog logService)
{
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);
}

Expand Down Expand Up @@ -139,7 +143,7 @@ private async void StartServer()
StartInfo =
{
FileName = "HandBrake.Worker.exe",
Arguments = string.Format(" --port={0}", this.port),
Arguments = string.Format(" --port={0}", port),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
Expand All @@ -155,7 +159,7 @@ private async void StartServer()
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));
}
}

Expand Down Expand Up @@ -267,5 +271,25 @@ private async void PollEncodeProgress()
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;
}
}
}
2 changes: 1 addition & 1 deletion win/CS/HandBrakeWPF/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion win/CS/HandBrakeWPF/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2210,7 +2210,7 @@ This will also stop any existing running jobs.</value>
<value>Please select a single job to view summary information.</value>
</data>
<data name="OptionsView_EnableWorkerProcesses" xml:space="preserve">
<value>Run each queued job in a separate worker process. (Experimental! Note, Limited to one instance of HandBrake currently!)</value>
<value>Run each queued job in a separate worker process. (Experimental)</value>
</data>
<data name="OptionsView_WorkerDefaultPort" xml:space="preserve">
<value>Default network port for worker:</value>
Expand Down
23 changes: 21 additions & 2 deletions win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public EncodeTask GetActiveJob()
/// <param name="message">Log message content</param>
protected void ServiceLogMessage(string message)
{
this.log.LogMessage(string.Format("{0}# {1}{0}", Environment.NewLine, message));
this.log.LogMessage(string.Format("{0}# {1}", Environment.NewLine, message));
}

protected void TimedLogMessage(string message)
Expand Down Expand Up @@ -249,7 +249,26 @@ private void InstanceEncodeCompleted(object sender, EncodeCompletedEventArgs e)
{
this.IsEncoding = false;

this.ServiceLogMessage(e.Error != 0 ? string.Format("Encode Failed ({0})", e.Error) : "Encode Completed!");
string completeMessage = "Job Completed!";
switch (e.Error)
{
case 0:
break;
case 1:
completeMessage = "Job Cancelled!";
break;
case 2:
completeMessage = string.Format("Job Failed. Check log and input settings ({0})", e.Error);
break;
case 3:
completeMessage = string.Format("Job Failed to Initialise. Check log and input settings ({0})", e.Error);
break;
default:
completeMessage = string.Format("Job Failed ({0})", e.Error);
break;
}

this.ServiceLogMessage(completeMessage);

// Handling Log Data
string hbLog = this.ProcessLogs(this.currentTask.Destination, this.isPreviewInstance, this.currentConfiguration);
Expand Down
2 changes: 1 addition & 1 deletion win/CS/HandBrakeWPF/Services/UserSettingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private Dictionary<string, object> GetDefaults()
defaults.Add(UserSettingConstants.DefaultPlayer, false);

// Experimental
defaults.Add(UserSettingConstants.RemoteServiceEnabled, false);
defaults.Add(UserSettingConstants.RemoteServiceEnabled, true);
defaults.Add(UserSettingConstants.RemoteServicePort, 8037);

// Misc
Expand Down

0 comments on commit b0a2c68

Please sign in to comment.