我创建了一个有以下旗帜的专用管道:
- 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.
我认为,采用习惯头盔来表明电文本身中的缓冲长度,但这种变化很多。
从管道读取数据的时间是否更好和可靠?