public OperationHost(IFlowRuntime runtime, IHostStub hostStub, IStandInProxy standInProxy)
        {
            _hostStub = hostStub;
            _standInProxy = standInProxy;

            _translator = new HostTranslator();

            hostStub.ReceivedFromStandIn += _translator.Process_remote_input;
            _translator.Translated_input += runtime.Process;
            runtime.Result += _translator.Process_local_output;
            _translator.Translated_output += standInProxy.SendToStandIn;
        }
        public OperationHost(IFlowRuntime runtime, IHostStub hostStub, IStandInProxy standInProxy)
        {
            _hostStub     = hostStub;
            _standInProxy = standInProxy;

            _translator = new HostTranslator();

            hostStub.ReceivedFromStandIn += _translator.Process_remote_input;
            _translator.Translated_input += runtime.Process;
            runtime.Result += _translator.Process_local_output;
            _translator.Translated_output += standInProxy.SendToStandIn;
        }
Exemple #3
0
        private async Task <Process> StartProcessAsync()
        {
            Debug.Assert(!_Process.IsValueCreated); // Can be called only once.
            Debug.Assert(disposables == null);
            Directory.CreateDirectory(WorkingDirectory);
            Process process          = null;
            var     localDisposables = new List <IDisposable>();

            try
            {
                var txPipe = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);
                localDisposables.Add(txPipe);
                var rxPipe = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);
                localDisposables.Add(rxPipe);
                process = StartExecutionProcess(txPipe, rxPipe);
                localDisposables.Add(process);
                txPipe.DisposeLocalCopyOfClientHandle();
                rxPipe.DisposeLocalCopyOfClientHandle();

                var procReader = new ByLineTextMessageReader(rxPipe);
                localDisposables.Add(procReader);
                localDisposables.Remove(rxPipe);
                var procWriter = new ByLineTextMessageWriter(txPipe);
                localDisposables.Add(procWriter);
                localDisposables.Remove(txPipe);
                // Wait for host to start up.
                using (var cts = new CancellationTokenSource(5000))
                {
                    Message startedMessage = null;
                    try
                    {
                        startedMessage = await procReader.ReadAsync(m =>
                                                                    m is RequestMessage rm && rm.Method == "NotifyStarted",
                                                                    cts.Token);
                    }
                    catch (OperationCanceledException)
                    {
                    }
                    if (startedMessage == null)
                    {
                        throw new ExecutionHostException(Prompts.CannotStartExecutionHost_MissingNotifyStarted);
                    }
                }

                //// HOST
                //var hostBuilder = new JsonRpcServiceHostBuilder();
                //// Currently this host does nothing.
                //var host = hostBuilder.Build();
                //var serverHandler = new StreamRpcServerHandler(host);
                //serverHandler.DefaultFeatures.Set<ISandboxContextFeature>(new SandboxContextFeature(this, null));
                //localDisposables.Add(serverHandler.Attach(procReader, procWriter));

                // CLIENT
                var clientHandler = new StreamRpcClientHandler();
                RpcClient = new JsonRpcClient(clientHandler);
                localDisposables.Add(clientHandler.Attach(procReader, procWriter));

                HostStub = ProxyBuilder.CreateProxy <IHostStub>(RpcClient);
                return(process);
            }
            catch (Exception ex)
            {
                if (process != null)
                {
                    try
                    {
                        if (!process.WaitForExit(1000))
                        {
                            process.Kill();
                        }
                    }
                    catch (Exception ex1)
                    {
                        throw new AggregateException(ex, ex1);
                    }
                }
                foreach (var d in localDisposables)
                {
                    try
                    {
                        d.Dispose();
                    }
                    catch (ObjectDisposedException)
                    {
                        // StreamWriter will attempt to flush before disposal,
                    }
                    catch (Exception ex1)
                    {
                        throw new AggregateException(ex, ex1);
                    }
                }
                localDisposables = null;
                if (ex is ExecutionHostException)
                {
                    throw;
                }
                throw new ExecutionHostException(Prompts.CannotStartExecutionHost, ex);
            }
            finally
            {
                if (localDisposables != null)
                {
                    disposables = localDisposables;
                }
            }
        }