简体   繁体   中英

Duplex named pipe in .NET gets stuck when writing during a read (async write)

I have a .NET app that reads messages from its parent NodeJS app via a NamedPipe and then writes messages back via stdout. This works fine and the code looks roughly like this:

Node:

const { spawn } = require("child_process");
const net = require("net");
const os = require("os");

let proc;
function main() {
    const socket = await setUpNamedPipe();

    proc.stdout.setEncoding("utf-8");
    proc.stdout.on("data", message => {
        message.trimEnd().split(os.EOL).forEach(m => {
          console.log(m);
        })   
    });

    socket.write("message1" + os.EOL);
    socket.write("message2" + os.EOL);
    socket.write("message3" + os.EOL);
    socket.write("message4" + os.EOL);
    socket.write("message5" + os.EOL);
}

function setUpNamedPipe() {
  const pipeName = "MyPipe";
    const pipePath = `\\\\.\\pipe\\${pipeName}`;
    let myResolve;
    const somePromise = new Promise(r =>
    {
        myResolve = r;
    });
    const server = net.createServer((socket) =>
    {
        myResolve(socket);
    });

    server.listen(pipePath, () =>
    {
        proc = spawn("Test.exe", [pipeName]);
    });

    return somePromise;
}

.NET app:

using System.IO.Pipes;

public class Program
{
    public static void ListenForMessages(TextReader reader)
    {
        while (true)
        {
            string message = reader.ReadLine();
            if (message == null)
            {
                return;
            }
            Console.WriteLine(message);
        }
    }

    public static void Main(string[] args)
    {
        string pipeName = args[0];
        using NamedPipeClientStream client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut); // Just `In` doesn't work for some reason
        client.Connect();
        using TextReader reader = TextReader.Synchronized(new StreamReader(client));

        Task.Factory.StartNew(() => ListenForMessages(reader), TaskCreationOptions.LongRunning).Wait();
    }
}

If I set this up as a duplex named pipe by replacing proc.stdout.on("data", ...) with socket.on("data", ...) and in my .NET app, creating a TextWriter similar to how I create my TextReader :

using TextReader reader = TextReader.Synchronized(new StreamReader(client));

And then using writer.WriteLine(...); writer.Flush(); writer.WriteLine(...); writer.Flush(); instead of Console.WriteLine and do the write on a Task, I don't get any messages from my .NET app (unless I wait for the Task to finish). Am I missing a step?

Roughly like:

public static void ListenForMessages(NamedPipeClientStream client)
{
    using TextWriter writer = TextWriter.Synchronized(new StreamWriter(client));
    using TextReader reader = TextReader.Synchronized(new StreamReader(client));
    while (true)
    {
        client.WaitForPipeDrain();
        string message = reader.ReadLine();
        if (message == null)
        {
            return;
        }

        Task.Run(() => {
            writer.WriteLine(message);
            writer.Flush();
        }); // only works if I add `.Wait()`
    }
}

public static void Main(string[] args)
{
    string pipeName = args[0];
    using NamedPipeClientStream client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut);
    client.Connect();

    Task.Factory.StartNew(() => ListenForMessages(client), TaskCreationOptions.LongRunning).Wait();
}

I just had to pass the PipeOptions.Asynchronous flag to the constructor like so:

new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM