Reading Asynchronous Channel - Data Loss

I want to read data from a pipe. Reading should not block the main thread (therefore, everything is in a separate thread), and it must be undone (therefore, I use IO overlap with the FILE_FLAG_OVERLAPPED flag)

This following code is simplified, but I just removed the error check:

 pipe = CreateNamedPipe("\\\\.\\pipe\\pipename", PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED , 0, 1, 1024, 1024, 1000, NULL); while (1) { // wait for either new data, or cancellation DWORD wait = WaitForMultipleObjects(2,events,FALSE,INFINITE); // Assume "new data event" and no error GetOverlappedResult(pipe,&overlap,&unused,FALSE); // always succeeds ReadFile(pipe,outputbuffer,neededsize,outputsize, &overlap); // sometimes says ERROR_IO_PENDING via GetLastError() } 
  • The first buffers are accepted correctly.
  • No data is sent within seconds.
  • It looks like an event. No data is read (none was sent!), And ReadFile() sets ERROR_IO_PENDING .
  • Another dataset is sent
  • ReadFile losing some new data.

To be precise, the last ReadFile() loses as much data as I requested in the previous ReadFile() .

What am I doing wrong?

Mandatory link

EDIT - Correctly, without error checking:

 BOOL res = ReadFile(...as before...); if(res) return; //success, reading was instantaneous, there was already data. //GetLastError() is supposed to be ERROR_IO_PENDING here wait = WaitForMultipleObjects(2,events,FALSE,INFINITE); // Assume "new data event" and no error res = GetOverlappedResult(pipe, &overlap, &cbRet, FALSE); // Now the buffer you gave to ReadFile has its cbRet first bytes filled. 
+5
source share

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


All Articles