forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for devserver IDE channel
- Loading branch information
1 parent
61e5886
commit dcecc26
Showing
18 changed files
with
915 additions
and
552 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/Uno.UI.RemoteControl.Host/IDEChannel/IIdeChannelServerProvider.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
using Uno.UI.RemoteControl.Messaging.IdeChannel; | ||
|
||
namespace Uno.UI.RemoteControl.Host.IdeChannel; | ||
|
||
internal interface IIdeChannelServerProvider | ||
{ | ||
Task<IdeChannelServer?> GetIdeChannelServerAsync(); | ||
} |
33 changes: 33 additions & 0 deletions
33
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Uno.UI.RemoteControl.Messaging.IdeChannel; | ||
|
||
namespace Uno.UI.RemoteControl.Host.IdeChannel; | ||
|
||
internal class IdeChannelServer : IIdeChannelServer | ||
{ | ||
private IServiceProvider _serviceProvider; | ||
|
||
public IdeChannelServer(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public event EventHandler<IdeMessage>? MessageFromIDE; | ||
|
||
public event EventHandler<IdeMessage>? MessageFromDevServer; | ||
|
||
public async Task SendToIdeAsync(IdeMessage message) | ||
{ | ||
MessageFromDevServer?.Invoke(this, message); | ||
|
||
await Task.Yield(); | ||
} | ||
|
||
public async Task SendToDevServerAsync(IdeMessage message) | ||
{ | ||
MessageFromIDE?.Invoke(this, message); | ||
|
||
await Task.Yield(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Uno.UI.RemoteControl.Host/IDEChannel/IdeChannelServerProvider.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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.IO.Pipes; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using StreamJsonRpc; | ||
using Uno.UI.RemoteControl.Messaging.IdeChannel; | ||
|
||
namespace Uno.UI.RemoteControl.Host.IdeChannel; | ||
|
||
internal class IdeChannelServerProvider : IIdeChannelServerProvider | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly IConfiguration _configuration; | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly Task<IdeChannelServer?> _initializeTask; | ||
private NamedPipeServerStream? _pipeServer; | ||
private IdeChannelServer? _ideChannelServer; | ||
private JsonRpc? _rpcServer; | ||
|
||
public IdeChannelServerProvider(ILogger<IdeChannelServerProvider> logger, IConfiguration configuration, IServiceProvider serviceProvider) | ||
{ | ||
_logger = logger; | ||
_configuration = configuration; | ||
_serviceProvider = serviceProvider; | ||
|
||
_initializeTask = Task.Run(Initialize); | ||
} | ||
|
||
private async Task<IdeChannelServer?> Initialize() | ||
{ | ||
if (!Guid.TryParse(_configuration["ideChannel"], out var ideChannel)) | ||
{ | ||
_logger.LogDebug("No IDE Channel ID specified, skipping"); | ||
return null; | ||
} | ||
|
||
_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"); | ||
} | ||
|
||
_ideChannelServer = new IdeChannelServer(_serviceProvider); | ||
_ideChannelServer.MessageFromIDE += OnMessageFromIDE; | ||
_rpcServer = JsonRpc.Attach(_pipeServer, _ideChannelServer); | ||
|
||
_ = StartKeepaliveAsync(); | ||
|
||
return _ideChannelServer; | ||
} | ||
|
||
private async Task StartKeepaliveAsync() | ||
{ | ||
while (_pipeServer?.IsConnected ?? false) | ||
{ | ||
_ideChannelServer?.SendToIdeAsync(new KeepAliveIdeMessage()); | ||
|
||
await Task.Delay(5000); | ||
} | ||
} | ||
|
||
private void OnMessageFromIDE(object? sender, IdeMessage ideMessage) | ||
{ | ||
if (ideMessage is KeepAliveIdeMessage) | ||
{ | ||
#if DEBUG | ||
_logger.LogDebug("Keepalive from IDE"); | ||
#endif | ||
} | ||
else | ||
{ | ||
_logger.LogDebug($"Unknown message type {ideMessage?.GetType()} from IDE"); | ||
} | ||
} | ||
|
||
public async Task<IdeChannelServer?> GetIdeChannelServerAsync() | ||
{ | ||
#pragma warning disable IDE0022 // Use expression body for method | ||
#pragma warning disable VSTHRD003 // Avoid awaiting foreign Tasks | ||
return await _initializeTask; | ||
#pragma warning restore VSTHRD003 // Avoid awaiting foreign Tasks | ||
#pragma warning restore IDE0022 // Use expression body for method | ||
} | ||
} |
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
Oops, something went wrong.