Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Process: Using Pipes/File Descriptors

Tags:

c#

process

pipe

I'm trying to connect to Chromium in the same way Puppeteer does in NodeJS.

This looks super simple in NodeJS. You add two more arguments to the stdio array and you have your pipes.

I'm not being able to implement the same logic in Puppeteer-Sharp. I spent some time reading many questions and answers here. I read about AnonymousPipeServerStream, but no joy.

This is one example I can't make it work:

AnonymousPipeServerStream streamReader = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);
AnonymousPipeServerStream streamWriter = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);

var chromeProcess = new Process();
chromeProcess.EnableRaisingEvents = true;
chromeProcess.StartInfo.UseShellExecute = false;
chromeProcess.StartInfo.FileName = "/.local-chromium/MacOS-536395/chrome-mac/Chromium.app/Contents/MacOS/Chromium";
chromeProcess.StartInfo.Arguments = 
    "--MANY-MANY-ARGUMENTS  " +
    "--remote-debugging-pipe  " +
    "--user-data-dir=/var/folders/0k/4qzqprl541b74ddz4wwj_ph40000gn/T/mz0trgjc.vlj " +
    "--no-sandbox " +
    "--disable-dev-shm-usage " + 
    streamReader.GetClientHandleAsString() +
    streamWriter.GetClientHandleAsString();

chromeProcess.Start();

streamReader.DisposeLocalCopyOfClientHandle();
streamWriter.DisposeLocalCopyOfClientHandle();

Task task = Task.Factory.StartNew(async () =>
{
    var reader = new StreamReader(streamReader);
    while (true)
    {
        var response = await reader.ReadToEndAsync();

        if (!string.IsNullOrEmpty(response))
        {
            Console.WriteLine(response);
        }
    }
});

Console.ReadLine();

Many examples show that you have to pass the GetClientHandleAsString() as an argument but I don't see how that can connect to processes.

This is a gist with the full example

like image 321
hardkoded Avatar asked Jun 19 '18 23:06

hardkoded


1 Answers

The answers lies in starting the Process with a ProcessStartInfo that has RedirectStandardInput, RedirectStandardOutput and/or RedirectStandardError properties set and then using the Process.StandardInput, Process.StandardOutput and/or Process.StandardError properties to access the pipes.

like image 125
Gerke Geurts Avatar answered Oct 20 '22 00:10

Gerke Geurts