Synchronizing reading and writing with synchronous NamedPipes

Posted by Mike Trader on Stack Overflow See other posts from Stack Overflow or by Mike Trader
Published on 2009-02-09T02:46:28Z Indexed on 2010/05/08 3:58 UTC
Read the original article Hit count: 312

Filed under:
|
|

A Named Pipe Server is created with

hPipe = CreateNamedPipe( zPipePath,
                         PIPE_ACCESS_DUPLEX,
                         PIPE_TYPE_BYTE | PIPE_WAIT | PIPE_READMODE_BYTE,
                         PIPE_UNLIMITED_INSTANCES,
                         8192, 8192, NMPWAIT_USE_DEFAULT_WAIT, NULL)

Then we immediately call:

ConnectNamedPipe( hPipe, BYVAL %NULL )

Which blocks until the client connects.

Then we proceed directly to ReadFile( hPipe, ...

The problem is that it takes the Client takes a finite amount of time to prepare and write all the fcgi request parameters. This has usually not completed before the Pipe Server performs its ReadFile(). The Read file operation thus finds no data in the pipe and the process fails.

Is there a mechanism to tell when a Write() has occurred/finished after a client has connected to a NamedPipe?

If I had control of the Client process, I could use a common Mutex, but I don't, and I really do not want to get into I/O completion ports just to solve this problem!

I can of course use a simple timer to wait 60m/s or so which is usually plenty of time for the wrote to complete, but that is a horrible hack.

© Stack Overflow or respective owner

Related posts about pipes

Related posts about named-pipes