2025-10-13 13:26:26 +02:00
|
|
|
|
namespace ElectronNET.AspNet.Runtime
|
|
|
|
|
|
{
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2026-03-02 18:03:03 +01:00
|
|
|
|
using ElectronNET.AspNet.Services;
|
2025-10-13 13:26:26 +02:00
|
|
|
|
using ElectronNET.Common;
|
|
|
|
|
|
using ElectronNET.Runtime.Data;
|
|
|
|
|
|
using ElectronNET.Runtime.Helpers;
|
|
|
|
|
|
using ElectronNET.Runtime.Services.ElectronProcess;
|
|
|
|
|
|
|
|
|
|
|
|
internal class RuntimeControllerAspNetDotnetFirst : RuntimeControllerAspNetBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private ElectronProcessBase electronProcess;
|
|
|
|
|
|
private int? port;
|
2026-03-02 18:03:03 +01:00
|
|
|
|
private readonly string authorization;
|
2025-10-13 13:26:26 +02:00
|
|
|
|
|
2026-03-02 18:03:03 +01:00
|
|
|
|
public RuntimeControllerAspNetDotnetFirst(AspNetLifetimeAdapter aspNetLifetimeAdapter, IElectronAuthenticationService authenticationService = null) : base(aspNetLifetimeAdapter)
|
2025-10-13 13:26:26 +02:00
|
|
|
|
{
|
2026-03-02 18:03:03 +01:00
|
|
|
|
this.authorization = Guid.NewGuid().ToString("N"); // 32 hex chars, no hyphens
|
|
|
|
|
|
|
|
|
|
|
|
// Only if somebody registered an IElectronAuthenticationService service - otherwise we do not care
|
|
|
|
|
|
authenticationService?.SetExpectedToken(this.authorization);
|
2025-10-13 13:26:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal override ElectronProcessBase ElectronProcess => this.electronProcess;
|
|
|
|
|
|
|
|
|
|
|
|
protected override Task StartCore()
|
|
|
|
|
|
{
|
|
|
|
|
|
var isUnPacked = ElectronNetRuntime.StartupMethod.IsUnpackaged();
|
|
|
|
|
|
var electronBinaryName = ElectronNetRuntime.ElectronExecutable;
|
2026-03-02 18:03:03 +01:00
|
|
|
|
var authToken = this.authorization;
|
2025-10-13 13:26:26 +02:00
|
|
|
|
this.port = ElectronNetRuntime.ElectronSocketPort;
|
|
|
|
|
|
|
|
|
|
|
|
if (!this.port.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.port = PortHelper.GetFreePort(ElectronNetRuntime.DefaultSocketPort);
|
|
|
|
|
|
ElectronNetRuntime.ElectronSocketPort = this.port;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-02 18:03:03 +01:00
|
|
|
|
var args = $"{Environment.CommandLine} --authtoken={authToken}";
|
2025-10-13 13:26:26 +02:00
|
|
|
|
this.electronProcess = new ElectronProcessActive(isUnPacked, electronBinaryName, args, this.port.Value);
|
|
|
|
|
|
this.electronProcess.Ready += this.ElectronProcess_Ready;
|
|
|
|
|
|
this.electronProcess.Stopped += this.ElectronProcess_Stopped;
|
|
|
|
|
|
|
|
|
|
|
|
return this.electronProcess.Start();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Task StopCore()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.electronProcess.Stop();
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ElectronProcess_Ready(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.TransitionState(LifetimeState.Started);
|
2026-03-02 18:03:03 +01:00
|
|
|
|
this.CreateSocketBridge(this.port!.Value, this.authorization);
|
2025-10-13 13:26:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ElectronProcess_Stopped(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.HandleStopped();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|