简体   繁体   English

Windows 在进程间通信 pipe 中发送前两个字节字符时,是否会自动添加一个 EOL 作为签名?

[英]Does Windows automatically add, as a signature, an EOL when sending the first two-byte characters in the interprocess communication pipe?

I have a general question about sending commands and data through Windows pipes.我有一个关于通过 Windows 管道发送命令和数据的一般性问题。 It seems that this problem does not happen on Unix operating systems.在Unix操作系统上好像没有出现这个问题。

This question is not specific to my application, but for illustration purposes, I will explain my problem through two examples using the gnuplot-iostream and the matplotplusplus C++ libraries.这个问题并不特定于我的应用程序,但出于说明目的,我将使用gnuplot-iostreammatplotplusplus C++ 库通过两个示例来解释我的问题。

Gnuplot-iostream Gnuplot-iostream

In my application, I am interacting with gnuplot through gnuplot-iostream , which is a C++ IO stream API.在我的应用程序中,我通过gnuplot-iostreamgnuplot交互,它是 C++ IO stream API。

In a nutshell, the gnuplot-iostream API is distributed as a simple header file, which, among others, links against the boost-iostream library to send commands and data to the gnuplot application.简而言之,gnuplot-iostream API 作为一个简单的 header 文件分发,其中链接到 boost-iostream 库以将命令和数据发送到 gnuplot 应用程序。 To do so, the API opens a pipe between the user's C++ program and gnuplot, and, through a very thin abstract layer, the user is able to send commands and data through the pipe.为此,API 在用户的 C++ 程序和 gnuplot 之间打开一个 pipe,并且通过一个非常薄的抽象层,用户能够通过 pipe 发送命令和数据。

When sending data to gnuplot, I've noticed that the first data point was not plotted correctly.将数据发送到 gnuplot 时,我注意到第一个数据点没有正确绘制。 After a very careful investigation, I've discovered that the first two characters of the data sent through the pipe were ignored.经过非常仔细的调查,我发现通过 pipe 发送的数据的前两个字符被忽略了。

Consider the following code snippet.考虑以下代码片段。

#include <vector>
#include "gnuplot-iostream.h"

std::vector<double> x = {-1.45809};
std::vector<double> y = {1.34003};
std::vector<double> z = {3.92172};

Gnuplot gp(stdout);
gp << "set view 90, 0\n";
gp << "set xlabel 'x'\n";
gp << "set ylabel 'y'\n";
gp << "set zlabel 'z'\n";
gp << "splot '-' with points\n";
gp.send1d(boost::make_tuple(x, y, z));

When plotting the above, I noticed that gnuplot received x = 0.45809, which means that "-1" has been truncated.在绘制上面的图时,我注意到 gnuplot 收到 x = 0.45809,这意味着“-1”已被截断。 Below is the image proof of what I have on my screen.下面是我屏幕上的图像证明。

x 坐标错误。该图显示 x = 0.45809 而不是 x = -1.45809,表明“-1”已被截断。

I think this truncation is due to the fact that Windows adds an EOL at the end of the first two characters.我认为这种截断是由于 Windows 在前两个字符的末尾添加了一个 EOL。 The next example will hopefully clarify my claim.下一个例子有望澄清我的主张。 Note that this bug has not been officially reported, but the author of gnuplot-iostream has reported several issues about the iostream for Windows users, which have not been fixed yet.请注意,此错误尚未正式报告,但 gnuplot-iostream 的作者报告了 Windows 用户的 iostream 的几个问题,但尚未修复。

Maplotplusplus地图加加

Matplotplusplus is another API to plot graphs using C++ with an interface very similar to Python's matplotlib. This API does not use Python interpreter and communicates with gnuplot as the backend application. Matplotplusplus是另一个使用 C++ 的 API 到 plot 图形,其界面与 Python 的 matplotlib 非常相似。这个 API 不使用 Python 解释器,与后端 gnuplot 通信作为

I will not enter into the details, but after a thorough investigation in the source codes, it happened that there was no mistake but the console outputted the following warning messages when running a simple "hello world" plotting program.细节我就不多说了,但是仔细查了源码后,碰巧在运行一个简单的“hello world”绘图程序时,并没有出错,但是控制台输出了如下的警告信息。

gnuplot> se
           ^
         line 0: unrecognized option - see 'help set'.

gnuplot> t terminal wxt title "Figure 1" size 560,420 enhanced font "Sans,10"
         ^
         line 0: invalid command

To me, it becomes quite clear that Windows is adding an undesired "\n" at the end of the first two characters.对我来说,很明显 Windows 在前两个字符的末尾添加了一个不需要的“\n”。 The desired command sent should be: set terminal wxt title "Figure 1" size 560,420 enhanced font "Sans,10" , but the "se" of "set" has been truncated.发送的所需命令应为: set terminal wxt title "Figure 1" size 560,420 enhanced font "Sans,10" ,但“set”的“se”已被截断。 So Windows has sent: se\nt terminal wxt title "Figure 1" size 560,420 enhanced font "Sans,10" instead.所以 Windows 已经发送: se\nt terminal wxt title "Figure 1" size 560,420 enhanced font "Sans,10" instead. By the way, I am not the only user reporting this bug .顺便说一句,我不是唯一报告此错误的用户。

My question is the following.我的问题如下。 Is it possible to cirucumvent this annoying problem due to Windows?由于 Windows,是否有可能绕过这个烦人的问题? Why such useless two-byte signature is sent over the pipe?为什么这么无用的两字节签名要通过pipe发送呢? An easy trick to solve this problem is to add two spaces in the string and send a dummy signature command before sending the actual data, but that is not a clean solution.解决这个问题的一个简单技巧是在字符串中添加两个空格并在发送实际数据之前发送一个虚拟签名命令,但这不是一个干净的解决方案。

Do you have any idea in which direction I should search to solve this problem?你知道我应该朝哪个方向寻找来解决这个问题吗? Is it a boost library problem or a Windows problem?是boost库问题还是Windows问题? It seems that there is no such of a fuss using Unix operating systems, as the pipe structure is well integrated into the OS.使用 Unix 操作系统似乎没有什么大惊小怪的,因为 pipe 结构很好地集成到操作系统中。 In the case of Windows, the OS pipe structure is quite different, which can cause unreliable communication issues.在 Windows 的情况下,操作系统 pipe 结构完全不同,这可能导致不可靠的通信问题。

The output stream buffer needs to be flushed.需要刷新 output stream 缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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