2025-10-13 13:24:28 +02:00
|
|
|
|
namespace ElectronNET.Runtime.Services.SocketBridge
|
|
|
|
|
|
{
|
|
|
|
|
|
using ElectronNET.API;
|
|
|
|
|
|
using ElectronNET.Runtime.Data;
|
2025-11-09 12:05:07 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2025-10-13 13:24:28 +02:00
|
|
|
|
|
2025-11-09 03:50:24 +01:00
|
|
|
|
internal class SocketBridgeService : LifetimeServiceBase
|
2025-10-13 13:24:28 +02:00
|
|
|
|
{
|
|
|
|
|
|
private readonly int socketPort;
|
|
|
|
|
|
private readonly string socketUrl;
|
|
|
|
|
|
private SocketIoFacade socket;
|
|
|
|
|
|
|
|
|
|
|
|
public SocketBridgeService(int socketPort)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.socketPort = socketPort;
|
|
|
|
|
|
this.socketUrl = $"http://localhost:{this.socketPort}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int SocketPort => this.socketPort;
|
|
|
|
|
|
|
|
|
|
|
|
internal SocketIoFacade Socket => this.socket;
|
|
|
|
|
|
|
|
|
|
|
|
protected override Task StartCore()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.socket = new SocketIoFacade(this.socketUrl);
|
|
|
|
|
|
this.socket.BridgeConnected += this.Socket_BridgeConnected;
|
|
|
|
|
|
this.socket.BridgeDisconnected += this.Socket_BridgeDisconnected;
|
|
|
|
|
|
Task.Run(this.Connect);
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Task StopCore()
|
|
|
|
|
|
{
|
2026-02-14 21:25:25 +01:00
|
|
|
|
this.socket.Dispose();
|
2025-10-13 13:24:28 +02:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Connect()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.socket.Connect();
|
2025-11-09 03:07:05 +01:00
|
|
|
|
if (this.State < LifetimeState.Started)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.TransitionState(LifetimeState.Started);
|
|
|
|
|
|
}
|
2025-10-13 13:24:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Socket_BridgeDisconnected(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.TransitionState(LifetimeState.Stopped);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Socket_BridgeConnected(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.TransitionState(LifetimeState.Ready);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-09 03:50:24 +01:00
|
|
|
|
}
|