-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create IIdeChannel services and register it into DI to be usabl…
…e by add-ins
- Loading branch information
Showing
9 changed files
with
193 additions
and
177 deletions.
There are no files selected for viewing
9 changes: 0 additions & 9 deletions
9
src/Uno.UI.RemoteControl.Host/IDEChannel/IIdeChannelServerProvider.cs
This file was deleted.
Oops, something went wrong.
117 changes: 107 additions & 10 deletions
117
src/Uno.UI.RemoteControl.Host/IDEChannel/IdeChannelServer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,129 @@ | ||
using System; | ||
using System.IO.Pipes; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Uno.UI.RemoteControl.Host.IDEChannel; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using StreamJsonRpc; | ||
using Uno.Extensions; | ||
using Uno.UI.RemoteControl.Messaging.IdeChannel; | ||
using Uno.UI.RemoteControl.Services; | ||
|
||
namespace Uno.UI.RemoteControl.Host.IdeChannel; | ||
|
||
internal class IdeChannelServer : IIdeChannelServer | ||
/// <summary> | ||
/// The server end for the "ide-channel" communication. | ||
/// </summary> | ||
internal class IdeChannelServer : IIdeChannel, IDisposable | ||
{ | ||
public IdeChannelServer() | ||
private readonly ILogger _logger; | ||
private readonly IConfiguration _configuration; | ||
|
||
private readonly Task<bool> _initializeTask; | ||
private NamedPipeServerStream? _pipeServer; | ||
private JsonRpc? _rpcServer; | ||
private Proxy? _proxy; | ||
|
||
public IdeChannelServer(ILogger<IdeChannelServer> logger, IConfiguration configuration) | ||
{ | ||
_logger = logger; | ||
_configuration = configuration; | ||
|
||
_initializeTask = Task.Run(InitializeServer); | ||
} | ||
|
||
public event EventHandler<IdeMessage>? MessageFromIDE; | ||
|
||
public event EventHandler<IdeMessageEnvelope>? MessageFromDevServer; | ||
#region IIdeChannel | ||
|
||
public async Task SendToIdeAsync(IdeMessage message) | ||
/// <inheritdoc /> | ||
public event EventHandler<IdeMessage>? MessageFromIde; | ||
|
||
/// <inheritdoc /> | ||
async Task IIdeChannel.SendToIdeAsync(IdeMessage message, CancellationToken ct) | ||
{ | ||
MessageFromDevServer?.Invoke(this, IdeMessageSerializer.Serialize(message)); | ||
await WaitForReady(ct); | ||
|
||
if (_proxy is null) | ||
{ | ||
this.Log().Log(LogLevel.Information, "Received an message to send to the IDE, but there is no connection available for that."); | ||
} | ||
else | ||
{ | ||
_proxy.SendToIde(message); | ||
} | ||
|
||
await Task.Yield(); | ||
} | ||
#endregion | ||
|
||
/// <inheritdoc /> | ||
public async ValueTask<bool> WaitForReady(CancellationToken ct = default) | ||
#pragma warning disable VSTHRD003 // Avoid awaiting foreign Tasks | ||
=> await _initializeTask; | ||
#pragma warning restore VSTHRD003 | ||
|
||
public async Task SendToDevServerAsync(IdeMessageEnvelope message, CancellationToken ct) | ||
/// <summary> | ||
/// Initialize as dev-server (cf. IdeChannelClient for init as IDE) | ||
/// </summary> | ||
private async Task<bool> InitializeServer() | ||
{ | ||
MessageFromIDE?.Invoke(this, IdeMessageSerializer.Deserialize(message)); | ||
if (!Guid.TryParse(_configuration["ideChannel"], out var ideChannel)) | ||
{ | ||
_logger.LogDebug("No IDE Channel ID specified, skipping."); | ||
return false; | ||
} | ||
|
||
await Task.Yield(); | ||
_pipeServer = new NamedPipeServerStream( | ||
pipeName: ideChannel.ToString(), | ||
direction: PipeDirection.InOut, | ||
maxNumberOfServerInstances: 1, | ||
transmissionMode: PipeTransmissionMode.Byte, | ||
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough); | ||
|
||
await _pipeServer.WaitForConnectionAsync(); | ||
|
||
if (_logger.IsEnabled(LogLevel.Debug)) | ||
{ | ||
_logger.LogDebug("IDE Connected"); | ||
} | ||
|
||
_proxy = new(this); | ||
_rpcServer = JsonRpc.Attach(_pipeServer, _proxy); | ||
|
||
_ = StartKeepAliveAsync(); | ||
return true; | ||
} | ||
|
||
private async Task StartKeepAliveAsync() | ||
{ | ||
while (_pipeServer?.IsConnected ?? false) | ||
{ | ||
_proxy?.SendToIde(new KeepAliveIdeMessage("dev-server")); | ||
|
||
await Task.Delay(5000); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
_rpcServer?.Dispose(); | ||
_pipeServer?.Dispose(); | ||
} | ||
|
||
private class Proxy(IdeChannelServer Owner) : IIdeChannelServer | ||
{ | ||
/// <inheritdoc /> | ||
public event EventHandler<IdeMessageEnvelope>? MessageFromDevServer; | ||
|
||
/// <inheritdoc /> | ||
public Task SendToDevServerAsync(IdeMessageEnvelope envelope, CancellationToken ct) | ||
{ | ||
Owner.MessageFromIde?.Invoke(Owner, IdeMessageSerializer.Deserialize(envelope)); | ||
return Task.CompletedTask; | ||
} | ||
|
||
internal void SendToIde(IdeMessage message) | ||
=> MessageFromDevServer?.Invoke(this, IdeMessageSerializer.Serialize(message)); | ||
} | ||
} |
92 changes: 0 additions & 92 deletions
92
src/Uno.UI.RemoteControl.Host/IDEChannel/IdeChannelServerProvider.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
src/Uno.UI.RemoteControl.Messaging/IDEChannel/IIdeChannelServer.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.