我有一个.NET应用程序,它通过一个NamedPipe从它的父NodeJS应用程序读取消息,然后通过stdout将消息写回来。这很好,代码看起来大致如下所示:
节点:
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应用程序:
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();
}
}
如果我将其设置为双工命名管道,将proc.stdout.on("data", ...)
替换为socket.on("data", ...)
,并在socket.on("data", ...)
应用程序中创建类似于创建TextReader
的TextWriter
using TextReader reader = TextReader.Synchronized(new StreamReader(client));
然后使用writer.WriteLine(...); writer.Flush();
而不是Console.WriteLine
编写任务,我不会从我的.NET应用程序中收到任何消息(除非我等待任务完成)。我是不是错过了一步?
大致类似:
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();
}
发布于 2022-04-06 20:11:19
我只需将PipeOptions.Asynchronous
标志传递给构造函数,如下所示:
new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
https://stackoverflow.com/questions/71741113
复制相似问题