Breaking ReadFile() blocking - Named Pipe (Windows API)

后端 未结 7 690
广开言路
广开言路 2020-12-30 23:43

To simplify, this is a situation where a NamedPipe SERVER is waiting for a NamedPipe CLIENT to write to the pipe (using WriteFile())

The Windows API that is blocking

相关标签:
7条回答
  • 2020-12-30 23:51

    Mike,

    You can't cancel synchronous ReadFile. But you can switch to asynchronous (overlapped) operations. By doing this, you can implement a pretty scalable architecture.

    Possible algorithm (just an idea):

    • For each new client call ReadFile
    • WaitForMultipleObjects where the handles are overlapped.hEvent + your custom events
    • Iterate over signalled events, and schedule them for execution by threads from a threads pool.

    This way you can have only few threads to receive connections and read data, while the actual data processing can be done by the threads pool.

    0 讨论(0)
  • 2020-12-30 23:59

    Try this before ReadFile :

    BOOL WINAPI PeekNamedPipe(
      __in       HANDLE hNamedPipe,
      __out_opt  LPVOID lpBuffer,
      __in       DWORD nBufferSize,
      __out_opt  LPDWORD lpBytesRead,
      __out_opt  LPDWORD lpTotalBytesAvail,
      __out_opt  LPDWORD lpBytesLeftThisMessage
    );
    
    if(TotalBytesAvail > 0)
      ReadFile(....);
    

    -AV-

    0 讨论(0)
  • 2020-12-31 00:00

    What happening is the server outbound pipe is left open waiting for connection while your client is trying to connect to the server inbound pipe (which is no longer existent)... What you need to do is flush out your outbound pipe in order to loop back to your inbound. You can flush out on the client side by reading the file (remember to loop the connect establishment because there is a "handshake" in there, and it will never work the first time)

    0 讨论(0)
  • 2020-12-31 00:01

    The problem with that, is that the event also requires a separate thread, so now I have two additional threads for each instance of this server. Since this needs to be scalable, this is undesirable.

    Never in my career have I found that "more threads" == "less scalable". How many of these "server" instances do you have?

    Normally, an operation needs to be performed in a separate thread if that operation is going to block and the system needs to be responsive while the operation is blocked.

    0 讨论(0)
  • 2020-12-31 00:03

    Take a look on CancelSynchronousIo

    Marks pending synchronous I/O operations that are issued by the specified thread as canceled.

    And CancelIo/CancelIoEx:

    To cancel all pending asynchronous I/O operations, use either:

    CancelIo — this function only cancels operations issued by the calling thread for the specified file handle.

    CancelIoEx — this function cancels all operations issued by the threads for the specified file handle.

    • http://msdn.microsoft.com/en-us/library/aa363794(VS.85).aspx
    • http://msdn.microsoft.com/en-us/library/aa365467(VS.85).aspx
    0 讨论(0)
  • 2020-12-31 00:05

    Asynchronous I/O operations do not have to block any thread if they use I/O Completion Ports. See: http://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx

    0 讨论(0)
提交回复
热议问题