Skip to content

Commit 248c64d

Browse files
committed
Auto-determine used port
1 parent d1e8584 commit 248c64d

File tree

5 files changed

+37
-36
lines changed

5 files changed

+37
-36
lines changed

src/ElectronNET.API/Common/ProcessRunner.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class ProcessRunner : IDisposable
2626
private readonly StringBuilder stdOut = new StringBuilder(4 * 1024);
2727
private readonly StringBuilder stdErr = new StringBuilder(4 * 1024);
2828

29+
public event EventHandler<string> LineReceived;
30+
2931
private volatile ManualResetEvent stdOutEvent;
3032
private volatile ManualResetEvent stdErrEvent;
3133
private volatile Stopwatch stopwatch;
@@ -571,6 +573,7 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
571573
if (e.Data != null)
572574
{
573575
Console.WriteLine("|| " + e.Data);
576+
LineReceived?.Invoke(this, e.Data);
574577
}
575578
else
576579
{

src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ internal class RuntimeControllerDotNetFirst : RuntimeControllerBase
1414
{
1515
private ElectronProcessBase electronProcess;
1616
private SocketBridgeService socketBridge;
17-
private int? port;
1817

1918
public RuntimeControllerDotNetFirst()
2019
{
@@ -42,19 +41,13 @@ protected override Task StartCore()
4241
var isUnPacked = ElectronNetRuntime.StartupMethod.IsUnpackaged();
4342
var electronBinaryName = ElectronNetRuntime.ElectronExecutable;
4443
var args = string.Format("{0} {1}", ElectronNetRuntime.ElectronExtraArguments, Environment.CommandLine).Trim();
45-
this.port = ElectronNetRuntime.ElectronSocketPort;
46-
47-
if (!this.port.HasValue)
48-
{
49-
this.port = PortHelper.GetFreePort(ElectronNetRuntime.DefaultSocketPort);
50-
ElectronNetRuntime.ElectronSocketPort = this.port;
51-
}
44+
var port = ElectronNetRuntime.ElectronSocketPort ?? 0;
5245

5346
Console.Error.WriteLine("[StartCore]: isUnPacked: {0}", isUnPacked);
5447
Console.Error.WriteLine("[StartCore]: electronBinaryName: {0}", electronBinaryName);
5548
Console.Error.WriteLine("[StartCore]: args: {0}", args);
5649

57-
this.electronProcess = new ElectronProcessActive(isUnPacked, electronBinaryName, args, this.port.Value);
50+
this.electronProcess = new ElectronProcessActive(isUnPacked, electronBinaryName, args, port);
5851
this.electronProcess.Ready += this.ElectronProcess_Ready;
5952
this.electronProcess.Stopped += this.ElectronProcess_Stopped;
6053

@@ -64,8 +57,9 @@ protected override Task StartCore()
6457

6558
private void ElectronProcess_Ready(object sender, EventArgs e)
6659
{
60+
var port = ElectronNetRuntime.ElectronSocketPort.Value;
6761
this.TransitionState(LifetimeState.Started);
68-
this.socketBridge = new SocketBridgeService(this.port!.Value, "");
62+
this.socketBridge = new SocketBridgeService(port, "");
6963
this.socketBridge.Ready += this.SocketBridge_Ready;
7064
this.socketBridge.Stopped += this.SocketBridge_Stopped;
7165
this.socketBridge.Start();

src/ElectronNET.API/Runtime/Services/ElectronProcess/ElectronProcessActive.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ protected override Task StopCore()
155155
return Task.CompletedTask;
156156
}
157157

158+
private void Read_SocketIO_Port(object sender, string line)
159+
{
160+
// Look for "Electron Socket: listening on port %s at"
161+
var prefix = "Electron Socket: listening on port ";
162+
163+
if (line.StartsWith(prefix))
164+
{
165+
var start = prefix.Length;
166+
var end = line.IndexOf(' ', start + 1);
167+
var port = line[start..end];
168+
169+
if (int.TryParse(port, out var p))
170+
{
171+
// We got the port, so no more need for reading this
172+
this.process.LineReceived -= this.Read_SocketIO_Port;
173+
ElectronNetRuntime.ElectronSocketPort = p;
174+
}
175+
}
176+
}
177+
158178
private async Task StartInternal(string startCmd, string args, string directoriy)
159179
{
160180
try
@@ -166,6 +186,7 @@ private async Task StartInternal(string startCmd, string args, string directoriy
166186

167187
this.process = new ProcessRunner("ElectronRunner");
168188
this.process.ProcessExited += this.Process_Exited;
189+
this.process.LineReceived += this.Read_SocketIO_Port;
169190
this.process.Run(startCmd, args, directoriy);
170191

171192
await Task.Delay(500.ms()).ConfigureAwait(false);

src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetDotnetFirst.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
internal class RuntimeControllerAspNetDotnetFirst : RuntimeControllerAspNetBase
1212
{
1313
private ElectronProcessBase electronProcess;
14-
private int? port;
1514
private readonly string authorization;
1615

1716
public RuntimeControllerAspNetDotnetFirst(AspNetLifetimeAdapter aspNetLifetimeAdapter, IElectronAuthenticationService authenticationService = null) : base(aspNetLifetimeAdapter)
@@ -29,16 +28,10 @@ protected override Task StartCore()
2928
var isUnPacked = ElectronNetRuntime.StartupMethod.IsUnpackaged();
3029
var electronBinaryName = ElectronNetRuntime.ElectronExecutable;
3130
var authToken = this.authorization;
32-
this.port = ElectronNetRuntime.ElectronSocketPort;
33-
34-
if (!this.port.HasValue)
35-
{
36-
this.port = PortHelper.GetFreePort(ElectronNetRuntime.DefaultSocketPort);
37-
ElectronNetRuntime.ElectronSocketPort = this.port;
38-
}
39-
31+
var port = ElectronNetRuntime.ElectronSocketPort ?? 0;
4032
var args = $"{Environment.CommandLine} --authtoken={authToken}";
41-
this.electronProcess = new ElectronProcessActive(isUnPacked, electronBinaryName, args, this.port.Value);
33+
34+
this.electronProcess = new ElectronProcessActive(isUnPacked, electronBinaryName, args, port);
4235
this.electronProcess.Ready += this.ElectronProcess_Ready;
4336
this.electronProcess.Stopped += this.ElectronProcess_Stopped;
4437

@@ -53,8 +46,9 @@ protected override Task StopCore()
5346

5447
private void ElectronProcess_Ready(object sender, EventArgs e)
5548
{
49+
var port = ElectronNetRuntime.ElectronSocketPort.Value;
5650
this.TransitionState(LifetimeState.Started);
57-
this.CreateSocketBridge(this.port!.Value, this.authorization);
51+
this.CreateSocketBridge(port, this.authorization);
5852
}
5953

6054
private void ElectronProcess_Stopped(object sender, EventArgs e)

src/ElectronNET.Host/main.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ let processInfo;
1616
let splashScreen;
1717
let nativeTheme;
1818
let dock;
19-
let desktopCapturer;
20-
let electronHostHook;
21-
let touchBar;
2219
let launchFile;
2320
let launchUrl;
24-
let processApi;
2521

2622
let manifestJsonFileName = 'package.json';
2723
let unpackedelectron = false;
@@ -45,7 +41,7 @@ else if (app.commandLine.hasSwitch('dotnetpacked')) {
4541
}
4642

4743
if (app.commandLine.hasSwitch('electronforcedport')) {
48-
electronforcedport = app.commandLine.getSwitchValue('electronforcedport');
44+
electronforcedport = +app.commandLine.getSwitchValue('electronforcedport');
4945
}
5046

5147
let authToken;
@@ -172,17 +168,10 @@ app.on('ready', async () => {
172168
if (electronforcedport) {
173169
console.info('Electron Socket IO (forced) Port: ' + electronforcedport);
174170
startSocketApiBridge(electronforcedport);
175-
return;
171+
} else {
172+
console.info('Electron Socket dynamic IO Port');
173+
startSocketApiBridge(0);
176174
}
177-
178-
// Added default port as configurable for port restricted environments.
179-
let defaultElectronPort = 8000;
180-
if (manifestJsonFile.electronPort) {
181-
defaultElectronPort = manifestJsonFile.electronPort;
182-
}
183-
184-
console.info('Electron Socket dynamic IO Port');
185-
startSocketApiBridge(0);
186175
});
187176

188177
app.on('quit', async (event, exitCode) => {

0 commit comments

Comments
 (0)