Using an overlapped named pipe to read and write to Windows at the same time

I have a pipe handle that was created as overlapped. I need to read and write data in parallel. How can I achieve this?

The documentation on the Named Pipe Server using Overlapped I / O shows an example of how to read and write for many channels, but "this avoids simultaneous operations on the same pipe instance."

What is the correct way to do this in C ++ on Windows? I cannot give a correct example and some help on this topic.

The main problem I encounter when regular ReadFiles are locked when there is no data to read, and in the end I can not write using WriteFile. I have not found a single method that could tell me if there is something to read that does not block. As far as I understand, I need to transfer the OVERLAPPED structure, but I don’t know how to use it in case of parallel reading and writing to one channel (not so many).

This should be possible, as stated in Synchronous and overflowing pipe I / O :

Overlapping operations allow a single pipe to read and write data at the same time, and for a single stream, to perform simultaneous I / O on multiple pipe descriptors.

+4
source share
2 answers

All you have to do is provide a different OVERLAPPED structure for each of the simultaneous operations. In your case, all this means that each of the two threads needs its own OVERLAPPED structure. Since threads seem to perform different functions, this should happen automatically unless you mess it up using a global variable.

Note that you are making things too complicated, starting with this example, which focuses on using overlapping I / O to avoid the need for multiple threads.

Instead, pretend that you write each of the two functions using non-overlapping I / O, but whenever you call ReadFile or WriteFile, include a valid OVERLAPPED structure with an event descriptor and follow WaitForSingleObject. You need to know a few things: you need to make sure that each thread creates its own event object, and you need to handle the case when the I / O operation completes immediately (i.e. returns ERROR_SUCCESS instead of ERROR_IO_PENDING). Otherwise, all this is quite simple.

If you cannot make it work, show your code.

+3
source

As the documentation for pipes reports, one process writes, another process reads. If you want to READ and WRITE, you will need two channels: one for "writing to another process" and one for "reading data from another process."

[This is not unique to Windows, but since you are asking about Windows handsets, I thought it was best to provide Windows documents. Linux / Unix channels are the same - they have two ends, the end of reading and the end of writing]

Of course, as noted in the commentary, it seems that the Windows documentation is rather inconsistent (and I only once used windows in one direction at a time).

As long as this example does not read and write SIMULTANEOUSLY, I think that it can be relatively easily changed so that it does.

I suspect (but since the code has not been sent) the problem is either in calling ReadFile, or when setting up the channel itself. The overridden calls in ReadFile are asynchronous, and before checking the results, you will have to wait for the event associated with the overlapped structure using WaitForMultipleObjects.

Obviously, if you are reading and writing at the same time, you need one overlapping structure for reading and one for writing to indicate which side is β€œcompleted”.

+1
source

Source: https://habr.com/ru/post/1495146/


All Articles