-
-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathRuntimeControllerElectronFirst.cs
More file actions
102 lines (84 loc) · 3.08 KB
/
RuntimeControllerElectronFirst.cs
File metadata and controls
102 lines (84 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
namespace ElectronNET.Runtime.Controllers
{
using ElectronNET.API;
using ElectronNET.Runtime.Data;
using ElectronNET.Runtime.Services.ElectronProcess;
using ElectronNET.Runtime.Services.SocketBridge;
using System;
using System.Threading.Tasks;
internal class RuntimeControllerElectronFirst : RuntimeControllerBase
{
private ElectronProcessBase electronProcess;
private SocketBridgeService socketBridge;
public RuntimeControllerElectronFirst()
{
}
internal override ISocketConnection Socket
{
get
{
if (this.State == LifetimeState.Ready)
{
return this.socketBridge.Socket;
}
throw new Exception("Cannot access socket bridge. Runtime is not in 'Ready' state");
}
}
internal override ElectronProcessBase ElectronProcess => this.electronProcess;
internal override SocketBridgeService SocketBridge => this.socketBridge;
protected override Task StartCore()
{
var port = ElectronNetRuntime.ElectronSocketPort.Value;
var token = ElectronNetRuntime.ElectronAuthToken;
if (!ElectronNetRuntime.ElectronProcessId.HasValue)
{
throw new Exception("No electronPID has been specified by Electron!");
}
this.TransitionState(LifetimeState.Starting);
this.socketBridge = new SocketBridgeService(port, token);
this.socketBridge.Ready += this.SocketBridge_Ready;
this.socketBridge.Stopped += this.SocketBridge_Stopped;
this.socketBridge.Start();
this.electronProcess = new ElectronProcessPassive(ElectronNetRuntime.ElectronProcessId.Value);
this.electronProcess.Ready += this.ElectronProcess_Ready;
this.electronProcess.Stopped += this.ElectronProcess_Stopped;
this.electronProcess.Start();
return Task.CompletedTask;
}
private void ElectronProcess_Ready(object sender, EventArgs e)
{
}
private void SocketBridge_Ready(object sender, EventArgs e)
{
this.TransitionState(LifetimeState.Ready);
}
private void SocketBridge_Stopped(object sender, EventArgs e)
{
this.HandleStopped();
}
private void ElectronProcess_Stopped(object sender, EventArgs e)
{
this.HandleStopped();
}
private void HandleStopped()
{
if (this.socketBridge.State != LifetimeState.Stopped)
{
this.socketBridge.Stop();
}
else if (this.electronProcess.State != LifetimeState.Stopped)
{
this.electronProcess.Stop();
}
else
{
this.TransitionState(LifetimeState.Stopped);
}
}
protected override Task StopCore()
{
this.socketBridge.Stop();
return Task.CompletedTask;
}
}
}