[英]An efficient way to use Windows Named Pipe for IPC
我正在使用jna
模块连接两个都执行 FFMPEG 命令的进程。 将服务器端的SDTOUT
命令的 SDTOUT 发送到 NampedPipe,并从该 NampedPipe 接收STDIN
,用于客户端的其他 FFMPEG 命令。
这就是我捕获STDOUT
并将其发送到服务器端的 pipe 的方式:
InputStream out = inputProcess.getInputStream();
byte[] buffer = new byte[maxBufferSize];
while (inputProcess.isAlive()) {
int no = out.available();
if (no > 0 && no > maxBufferSize) {
int n = out.read(buffer, 0,maxBufferSize);
IntByReference lpNumberOfBytesWritten = new IntByReference(maxBufferSize);
Kernel32.INSTANCE.WriteFile(pipe, buffer, buffer.length, lpNumberOfBytesWritten, null);
}
}
这就是我捕获STDIN
并将其提供给客户端的方式:
OutputStream in = outputProcess.getOutputStream();
while (pipeOpenValue >= 1 && outputProcess.isAlive() && ServerIdState) {
// read from pipe
resp = Kernel32.INSTANCE.ReadFile(handle, readBuffer,readBuffer.length, lpNumberOfBytesRead, null);
// Write to StdIn inputProcess
if (outputProcess != null) {
in.write(readBuffer);
in.flush();
}
// check pipe status
Kernel32.INSTANCE.GetNamedPipeHandleState(handle, null,PipeOpenStatus, null, null, null, 2048);
pipeOpenValue = PipeOpenStatus.getValue();
WinDef.ULONGByReference ServerId = new WinDef.ULONGByReference();
ServerIdState = Kernel32.INSTANCE.GetNamedPipeServerProcessId(handle, ServerId);
}
但是我遇到了两个问题:
|
在命令提示符下。 速度取决于缓冲区大小,但大缓冲区大小会阻止操作,小缓冲区大小会进一步降低速度。问题:
STDOUT
到 Namedpipe 并在客户端中捕获它。 (消除两个循环)谢谢
似乎您一直在服务器端轮询进程状态标准输入,而无需等待任何事件或使线程休眠。 可能你想看看这里: Concurrent read/write of named pipe in Java (on windows)
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.