Some History of the Named Pipe, Part 3
- Some History of the Named Pipe, Part 1 (Unix pipes)
- Some History of the Named Pipe, Part 2 (Unix fifo)
- Some History of the Named Pipe, Part 3 (Windows named pipe)
- Some History of the Named Pipe, Part 4 (WCF named pipe)
In earlier parts we looked at the anonymous pipe and the fifo, which were predecessors similar in spirit to the Windows named pipe. A Windows named pipe is an inter-process communication mechanism that can provide either a one-way or duplex flow of data, organized as either a stream of bytes or a series of messages.
Creating a named pipe on Windows has significantly grown in complexity from what we’ve seen before as the number of options and capabilities of the named pipe system has increased. Here’s the named pipe creation function for comparison although I’m not going to go into detail on all of the parameters.
HANDLE WINAPI CreateNamedPipe(
__in LPCTSTR lpName,
__in DWORD dwOpenMode,
__in DWORD dwPipeMode,
__in DWORD nMaxInstances,
__in DWORD nOutBufferSize,
__in DWORD nInBufferSize,
__in DWORD nDefaultTimeOut,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes
);
The name parameter places the pipe somewhere in the file system, similar to the Unix fifo. On Windows though, the creation of pipes is limited to a special namespace, \\hostname\pipe\, which keeps pipes separate from other types of files and devices. The hostname portion of the pipe address allows pipe connections to span machines (next time we’ll see how to prevent remote access to a pipe, which is something that WCF does). For the local host, a dot is used instead of the hostname.
While a Windows named pipe can behave somewhat like a fifo, it also can behave somewhat like a socket. The pipe mode controls whether data is transferred through the pipe as a stream of bytes or as a sequence of messages. Instancing allows multiple pipe connections to be serviced using different threads where the pipe connections share the same pipe name. For example, you can construct a multi-threaded pipe server that talks to many clients, each of which connected to the same named pipe server address.
In summary, some of the things that make Windows named pipes different than the other named pipes we looked at are:
- Duplex communication
Message-oriented reads and writes
- Special pipe namespace
- Remote machine connections
- Multiple instances at the same server address
WCF uses only a portion of these capabilities and hides nearly all of the details of named pipe communication. Next time we’ll look at both how WCF uses Windows named pipes and how WCF makes a named pipe look even more like a socket.