From Newsgroup: alt.windows7.general
R.Wieser wrote:
Hello all,
I would like to write some single-threaded code to work with a single non-blocking, non-overlapped (incoming) named pipe, in combination with MsgWaitForSingleObject - the latter as part of the message-loop.
The problem is that although it looks like I can wait /something/ to happen with the pipe, I do not see how I can get *what* is happening : A new connection (ConnectNamedPipe), or data coming in (ReadFile).
Does anyone have an idea ? Some example code perhaps ?
Regards,
Rudy Wieser
p.s.
I was thinking of using a flag indicating the current mode, but that would not work well if I ever decide to accept more than a single connection. :-|
You cannot really do this cleanly with a non overlapped named pipe.
If you wait on the pipe handle in a message loop, Windows can tell you that something happened, but not in a way that neatly identifies whether that means a pending connection completed or data is ready to read. With non blocking, non overlapped pipes, you usually end up having to call ConnectNamedPipe or ReadFile and then inspect the result yourself as part of a manual state machine.
So the practical answer is that this design is the wrong fit for what you want. If you need to integrate a named pipe into a message loop and know what operation completed, the normal Windows solution is overlapped I O with event objects. Then you can wait for the message queue and for specific pipe events in the same loop.
For a single connection you could keep a state flag such as "waiting for connect" versus "connected, waiting for read" and then try the appropriate call when the handle wakes up, but that gets awkward quickly and does not scale well once you want more than one client.
So the real answer is that if you want this to work properly, especially in a single threaded message loop, use overlapped named pipes.
This is a response to the post seen at:
http://www.jlaforums.com/viewtopic.php?p=702617176#702617176
--
[via JLA Forums] alt.windows7.general on the web:
http://www.jlaforums.com/viewforum.php?f=422
--- Synchronet 3.21e-Linux NewsLink 1.2