Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp CoreIpc [ROBO-3791] #106

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/NuGet.Config → NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<configuration>
<packageSources>
<add key="Nuget" value="https://api.nuget.org/v3/index.json" />
<add key="UiPath-Internal" value="https://uipath.pkgs.visualstudio.com/Public.Feeds/_packaging/UiPath-Internal/nuget/v3/index.json" />
</packageSources>
</configuration>
4 changes: 2 additions & 2 deletions src/CI/azp-dotnet-dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ steps:

- task: DotNetCoreCLI@2
displayName: 'dotnet push to UiPath-Internal'
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
condition: succeeded()
inputs:
command: push
packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg'
publishVstsFeed: 'Public.Feeds/UiPath-Internal'

- task: PublishSymbols@2
displayName: 'Publish Symbols to UiPath Azure Artifacts Symbol Server'
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
condition: succeeded()
inputs:
symbolsFolder: $(Build.SourcesDirectory)
searchPattern: '**/UiPath.CoreIpc/bin/**/UiPath.CoreIpc.pdb'
Expand Down
6 changes: 6 additions & 0 deletions src/CI/azp-dotnet.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
steps:
- task: DotNetCoreCLI@2
displayName: '$(Label_DotNet) Restore, build and pack'
inputs:
projects: '$(DotNet_SessionSolution)'
arguments: '--configuration $(DotNet_BuildConfiguration) -p:Version="$(FullVersion)" -p:DefineConstantsEx="CI"'

- task: DotNetCoreCLI@2
displayName: '$(Label_DotNet) Run unit tests'
inputs:
Expand Down
19 changes: 19 additions & 0 deletions src/CI/azp-initialization.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
steps:
# Runtime version should match with SDK version.
# The runtime should be the one that is contained in SDK.
# https://dotnet.microsoft.com/en-us/download/dotnet/6.0
- powershell: |
Write-Host "##vso[task.setvariable variable=DotnetRuntimeVersion;]8.0.8"
Write-Host "##vso[task.setvariable variable=DOTNET_NOLOGO;]true"
displayName: 'Use .NET Runtime 8.0.8'

- task: UseDotNet@2
displayName: 'Use .NET SDK 6.0.317'
inputs:
packageType: 'sdk'
version: '6.0.317'

- task: UseDotNet@2
displayName: 'Use .NET SDK 8.0.400'
inputs:
packageType: 'sdk'
version: 8.0.400

# Read $(Version) from the UiPath.CoreIpc.csproj file
- powershell: |
Expand Down
2 changes: 1 addition & 1 deletion src/CI/azp-nodejs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
inputs:
workingDirectory: $(NodeJS_ProjectPath)
script: 'npm test'

- task: PublishTestResults@2
displayName: 'Publish Web Test Results'
condition: succeededOrFailed()
Expand Down
50 changes: 25 additions & 25 deletions src/CI/azp-start.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ variables:
DotNet_MainProjectName: 'UiPath.CoreIpc'
DotNet_MainProjectPath: './src/UiPath.CoreIpc/UiPath.CoreIpc.csproj'
DotNet_ArtifactName: 'NuGet package'

NodeJS_DotNet_BuildConfiguration: 'Debug'
NodeJS_ProjectPath: './src/Clients/js'
NodeJS_ArchivePath: './src/Clients/js/dist/pack/nodejs.zip'
Expand All @@ -23,32 +23,32 @@ stages:
- stage: Build
displayName: '🏭 Build'
jobs:
# The following 3 jobs will run in parallel:
- job:
displayName: '.NET on Windows'
pool:
# The following 3 jobs will run in parallel:
- job:
displayName: '.NET on Windows'
pool:
vmImage: 'windows-2022'
steps:
- template: azp-initialization.yaml
- template: azp-dotnet.yaml
- template: azp-dotnet-dist.yaml
- job:
displayName: 'node.js on Windows'
pool:
steps:
- template: azp-initialization.yaml
- template: azp-dotnet.yaml
- template: azp-dotnet-dist.yaml

- job:
displayName: 'node.js on Windows'
pool:
vmImage: 'windows-2022'
steps:
- template: azp-initialization.yaml
- template: azp-nodejs.yaml
- template: azp-nodejs-dist.yaml
- job:
displayName: 'node.js on Ubuntu'
pool:
vmImage: 'ubuntu-20.04'
steps:
- template: azp-initialization.yaml
- template: azp-nodejs.yaml
steps:
- template: azp-initialization.yaml
- template: azp-nodejs.yaml
- template: azp-nodejs-dist.yaml

- job:
displayName: 'node.js on Ubuntu'
pool:
vmImage: 'ubuntu-20.04'
steps:
- template: azp-initialization.yaml
- template: azp-nodejs.yaml

- stage: Publish
displayName: 🚚 Publish
Expand Down
4 changes: 2 additions & 2 deletions src/Clients/js/dotnet/UiPath.CoreIpc.NodeInterop.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30320.27
# Visual Studio Version 17
VisualStudioVersion = 17.10.35027.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UiPath.CoreIpc.NodeInterop", "UiPath.CoreIpc.NodeInterop\UiPath.CoreIpc.NodeInterop.csproj", "{B514D2A2-B8ED-4A2A-BDE7-42F74A316FBE}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace UiPath.CoreIpc.NodeInterop;
namespace UiPath.Ipc.NodeInterop;

internal static class Contracts
{
Expand Down
148 changes: 90 additions & 58 deletions src/Clients/js/dotnet/UiPath.CoreIpc.NodeInterop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using UiPath.CoreIpc.NamedPipe;
using UiPath.CoreIpc.WebSockets;
using UiPath.Ipc.Transport.NamedPipe;
using UiPath.Ipc.Transport.WebSocket;

namespace UiPath.CoreIpc.NodeInterop;
namespace UiPath.Ipc.NodeInterop;

using static Contracts;
using static ServiceImpls;
using static Signalling;
using static UiPath.Ipc.NodeInterop.Extensions;

class Program
{
Expand Down Expand Up @@ -61,6 +63,11 @@ static async Task<int> Main(

static async Task MainCore(string? pipeName, string? webSocketUrl, int? maybeSecondsPowerOnDelay)
{
if (pipeName is null && webSocketUrl is null)
{
throw new ArgumentException($"At least one of {nameof(pipeName)} or {nameof(webSocketUrl)} must be specified.");
}

if (maybeSecondsPowerOnDelay is { } secondsPowerOnDelay)
{
await Task.Delay(TimeSpan.FromSeconds(secondsPowerOnDelay));
Expand All @@ -71,34 +78,27 @@ static async Task MainCore(string? pipeName, string? webSocketUrl, int? maybeSec

var sp = services
.AddLogging()
.AddIpc()
.AddSingleton<IAlgebra, Algebra>()
.AddSingleton<ICalculus, Calculus>()
.AddSingleton<IBrittleService, BrittleService>()
.AddSingleton<IEnvironmentVariableGetter, EnvironmentVariableGetter>()
.AddSingleton<IDtoService, DtoService>()
.BuildServiceProvider();

var serviceHost = new ServiceHostBuilder(sp)
.UseNamedPipesAndOrWebSockets(pipeName, webSocketUrl)
.AddEndpoint<IAlgebra, IArithmetic>()
.AddEndpoint<ICalculus>()
.AddEndpoint<IBrittleService>()
.AddEndpoint<IEnvironmentVariableGetter>()
.AddEndpoint<IDtoService>()
.Build();

var thread = new AsyncContextThread();
thread.Context.SynchronizationContext.Send(_ => Thread.CurrentThread.Name = "GuiThread", null);
var sched = thread.Context.Scheduler;
var scheduler = thread.Context.Scheduler;

var ipcServers = EnumerateServerTransports(pipeName, webSocketUrl)
.Select(CreateAndStartIpcServer)
.ToArray();

_ = Task.Run(async () =>
{
try
{
await using var sp = new ServiceCollection()
.AddLogging()
.AddIpc()
.BuildServiceProvider();

var callback = new Arithmetic();
Expand All @@ -107,25 +107,45 @@ IEnumerable<Task> EnumeratePings()
{
if (webSocketUrl is not null)
{
yield return new WebSocketClientBuilder<IAlgebra, IArithmetic>(uri: new(webSocketUrl), sp)
.RequestTimeout(TimeSpan.FromHours(5))
.CallbackInstance(callback)
.Build()
.Ping();
yield return new IpcClient
{
ServiceProvider = sp,
RequestTimeout = TimeSpan.FromHours(5),
Callbacks = new()
{
{ typeof(IArithmetic), callback }
},
Transport = new WebSocketClientTransport
{
Uri = new(webSocketUrl),
}
}
.GetProxy<IAlgebra>()
.Ping();
}

if (pipeName is not null)
{
yield return new NamedPipeClientBuilder<IAlgebra, IArithmetic>(pipeName, sp)
.RequestTimeout(TimeSpan.FromHours(5))
.CallbackInstance(callback)
.Build()
.Ping();
yield return new IpcClient
{
ServiceProvider = sp,
RequestTimeout = TimeSpan.FromHours(5),
Callbacks = new()
{
{ typeof(IArithmetic), callback }
},
Transport = new NamedPipeClientTransport()
{
PipeName = pipeName,
}
}
.GetProxy<IAlgebra>()
.Ping();
}
}

await Task.WhenAll(EnumeratePings());

Send(SignalKind.ReadyToConnect);
}
catch (Exception ex)
Expand All @@ -135,50 +155,62 @@ IEnumerable<Task> EnumeratePings()
}
});

await serviceHost.RunAsync(sched);
}

private class Arithmetic : IArithmetic
{
public Task<int> Sum(int x, int y) => Task.FromResult(x + y);

public Task<bool> SendMessage(Message<int> message) => Task.FromResult(true);
}
}

internal static class Extensions
{
public static ServiceHostBuilder UseNamedPipesAndOrWebSockets(this ServiceHostBuilder builder, string? pipeName, string? webSocketUrl)
{
if (pipeName is null && webSocketUrl is null)
{
throw new ArgumentOutOfRangeException();
}
await Task.Delay(Timeout.InfiniteTimeSpan);

if (pipeName is not null)
IpcServer CreateAndStartIpcServer(ServerTransport transport)
{
builder = builder.UseNamedPipes(new NamedPipeSettings(pipeName));
var ipcServer = new IpcServer()
{
Endpoints = new()
{
typeof(IAlgebra),
typeof(ICalculus),
typeof(IBrittleService),
typeof(IEnvironmentVariableGetter),
typeof(IDtoService)
},
Transport = transport,
ServiceProvider = sp,
Scheduler = scheduler
};
ipcServer.Start();
return ipcServer;
}

if (webSocketUrl is not null)

IEnumerable<ServerTransport> EnumerateServerTransports(string? pipeName, string? webSocketUrl)
{
string url = CurateWebSocketUrl(webSocketUrl);
var accept = new HttpSysWebSocketsListener(url).Accept;
WebSocketSettings settings = new(accept);
if (pipeName is not null)
{
yield return new NamedPipeServerTransport() { PipeName = pipeName };
}

builder = builder.UseWebSockets(settings);
}
if (webSocketUrl is not null)
{
string url = CurateWebSocketUrl(webSocketUrl);
var accept = new HttpSysWebSocketsListener(url).Accept;
yield return new WebSocketServerTransport() { Accept = accept };
}

return builder;
static string CurateWebSocketUrl(string raw)
{
var builder = new UriBuilder(raw);
builder.Scheme = "http";
return builder.ToString();
}
}
}

private static string CurateWebSocketUrl(string raw)
private class Arithmetic : IArithmetic
{
var builder = new UriBuilder(raw);
builder.Scheme = "http";
return builder.ToString();
public Task<int> Sum(int x, int y) => Task.FromResult(x + y);

public Task<bool> SendMessage(Message<int> message) => Task.FromResult(true);
}
}

internal static class Extensions
{
public class HttpSysWebSocketsListener : IDisposable
{
HttpListener _httpListener = new();
Expand Down
Loading