104 lines
4.3 KiB
C#
104 lines
4.3 KiB
C#
namespace InspectionUI.RemoteControl
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading.Tasks;
|
|
|
|
using InspectionUI.RemoteControl.Handlers;
|
|
using InspectionUI.RemoteControl.Interfaces;
|
|
using InspectionUI.RemoteControl.Messages;
|
|
|
|
using Microsoft.AspNet.SignalR.Client;
|
|
|
|
[ClassInterface(ClassInterfaceType.None)]
|
|
[ComSourceInterfaces(typeof(IRemoteConnectionEvents))]
|
|
[Guid("FB571D10-F46A-4B32-B1B1-6E7DCE362CC1")]
|
|
[ComVisible(true)]
|
|
public class RemoteConnection : IRemoteConnection
|
|
{
|
|
private const string LogFile = @".\signalR.txt";
|
|
|
|
private readonly ICollection<IDisposable> disposables = new List<IDisposable>();
|
|
|
|
private HubConnection connection;
|
|
private IHubProxy proxy;
|
|
|
|
public bool IsConnected
|
|
=> this.connection != null
|
|
&& this.connection.State == ConnectionState.Connected;
|
|
|
|
public void Dispose()
|
|
{
|
|
foreach (var disposable in this.disposables)
|
|
{
|
|
disposable.Dispose();
|
|
}
|
|
|
|
Task.Factory.StartNew(this.connection.Dispose);
|
|
}
|
|
|
|
public void Connect(string uri)
|
|
{
|
|
this.connection = new HubConnection(uri, queryString: new Dictionary<string, string>
|
|
{
|
|
{ "GroupName", Environment.MachineName }
|
|
});
|
|
this.proxy = this.connection.CreateHubProxy("RemoteControl");
|
|
|
|
this.disposables.Add(this.proxy.On<FlowMessage>(nameof(this.StartFlow), this.ReceiveStartFlow));
|
|
this.disposables.Add(this.proxy.On<FlowMessage>(nameof(this.GetFlow), this.ReceiveGetFlow));
|
|
this.disposables.Add(this.proxy.On<FlowMessage>(nameof(this.StopFlow), this.ReceiveStopFlow));
|
|
this.disposables.Add(this.proxy.On<ReferenceMessage>(nameof(this.StartRM), this.ReceiveStartRM));
|
|
this.disposables.Add(this.proxy.On<ReferenceMessage>(nameof(this.GetRM), this.ReceiveGetRM));
|
|
this.disposables.Add(this.proxy.On<ReferenceMessage>(nameof(this.StopRM), this.ReceiveStopRM));
|
|
this.disposables.Add(this.proxy.On<ReferenceMessage>(nameof(this.StopRM), this.ReceiveStopRM));
|
|
this.disposables.Add(this.proxy.On<InfoMessage>(nameof(this.GetInfo), this.ReceiveGetInfo));
|
|
|
|
this.connection.Start();
|
|
}
|
|
|
|
#region Shared events
|
|
public event FlowHandler StartFlow;
|
|
public event FlowHandler GetFlow;
|
|
public event FlowHandler StopFlow;
|
|
public event ReferenceHandler StartRM;
|
|
public event ReferenceHandler GetRM;
|
|
public event ReferenceHandler StopRM;
|
|
public event InfoHandler GetInfo;
|
|
#endregion Shared events
|
|
|
|
#region Client send methods
|
|
public void SendStartFlow(FlowMessage message) => this.proxy.Invoke(nameof(this.StartFlow), message);
|
|
|
|
public void SendGetFlow(FlowMessage message) => this.proxy.Invoke(nameof(this.GetFlow), message);
|
|
|
|
public void SendStopFlow(FlowMessage message) => this.proxy.Invoke(nameof(this.StopFlow), message);
|
|
|
|
public void SendStartRM(ReferenceMessage message) => this.proxy.Invoke(nameof(this.StartRM), message);
|
|
|
|
public void SendGetRM(ReferenceMessage message) => this.proxy.Invoke(nameof(this.GetRM), message);
|
|
|
|
public void SendStopRM(ReferenceMessage message) => this.proxy.Invoke(nameof(this.StopRM), message);
|
|
|
|
public void SendGetInfo(InfoMessage message) => this.proxy.Invoke(nameof(this.GetInfo), message);
|
|
#endregion
|
|
|
|
#region Client receive methods
|
|
private void ReceiveStartFlow(FlowMessage message) => this.StartFlow?.Invoke(message);
|
|
|
|
private void ReceiveGetFlow(FlowMessage message) => this.GetFlow?.Invoke(message);
|
|
|
|
private void ReceiveStopFlow(FlowMessage message) => this.StopFlow?.Invoke(message);
|
|
|
|
private void ReceiveStartRM(ReferenceMessage message) => this.StartRM?.Invoke(message);
|
|
|
|
private void ReceiveGetRM(ReferenceMessage message) => this.GetRM?.Invoke(message);
|
|
|
|
private void ReceiveStopRM(ReferenceMessage message) => this.StopRM?.Invoke(message);
|
|
|
|
private void ReceiveGetInfo(InfoMessage message) => this.GetInfo?.Invoke(message);
|
|
#endregion Client receive methods
|
|
}
|
|
}
|