English 中文(简体)
如何将数据长度(可靠)放在指定管道?
原标题:How to get the length of data to be read (reliably) in named pipes?

我创建了一个有以下旗帜的专用管道:

  • PIPE_ACCESS_DUPLEX - both side read/write access
  • PIPE_TYPE_MESSAGE - Message type read
  • PIPE_WAIT - blocking readwrite

在服务器方面,我打电话Connect NamedPipe,等待客户联系。

从客户方面,我打电话CallNamedPipe,与服务器连接,并撰写N.长度的数据。

在服务器方面:

  • After the client connects, PeekNamedPipe is called to get the length of the buffer to allocate to read the data buffer.
  • After getting the exact buffer size (N), I am allocating the buffer of length N and calling ReadFile to read the data from Pipe.

<<>Problem>:

  • The issue is that -- on Single processor machines the PeekNamedPipe API returns the buffer length as 0. Due to this later ReadFile fails.
  • after some investigation I could find that due to some race condition , PeekNamedPipe API gets called even before data is put onto the Pipe by the client.
  • Any idea how to solve this race condition ? I need to call PeekNamedPipe to get the buffer size and PeekNamedPipe cannot be called before the data is available.

我认为,采用习惯头盔来表明电文本身中的缓冲长度,但这种变化很多。

从管道读取数据的时间是否更好和可靠?

问题回答

很多种族条件,你可以带上标明的管道。 你必须用你的法典对待他们。 可能性:

  • ConnectNamedPipe() on the server side may return ERROR_PIPE_CONNECTED if the client managed to connect right after the CreateNamedPipe() call. Just treat it as connected.
  • WaitNamedPipe on client side does not set the error if it timed out. Assume a timeout.
  • CreateFile() on client side may return ERROR_PIPE_BUSY if another client managed to grab the pipe first, even after a successful WaitNamedPipe() call. Go back to WaitNamedPipe state.
  • FlushFileBuffers() may return ERROR_PIPE_NOT_CONNECTED if the client already saw the message and closed the pipe. Ignore that.
  • An overlapped ReadFile() call may complete immediately and not return ERROR_IO_PENDING. Consider the read completed.
  • PeekNamedPipe() may return 0 if the server has not written to the pipe yet. Sleep(1) and repeat.

你们喜欢Aynschronous I/O。 仅当有数据时,请Windows通知你,并在此刻通知Peek。

Having a packet size in the header is a good idea in any case, making the protocol less dependent on transport layer. Alternatively, if the client sends data and closes the pipe you can accumulate into buffer with ReadFile until EOF.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签