|
||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||
Inter-Process Communication in .NET Using Named Pipes, Part 13. Creating a Named PipeAs part of the different Named Pipes operations, first we are going to see how a server Named Pipe is created. Each pipe has a name as "Named Pipe" implies. The exact syntax of server pipe names is \\.\pipe\PipeName. The "PipeName" part is actually the specific name of the pipe. In order to connect to the pipe a client application needs to create a client Named Pipe with the same name. If the client is located on a different machine, the name should also include the server e.g. \\SERVER\pipe\PipeName. The following static method from NamedPipeWrapper is used to instantiate a server Named Pipe. public static PipeHandle Create(string name,
uint outBuffer,
uint inBuffer) {
name = @"\\.\pipe\" + name;
PipeHandle handle = new PipeHandle();
for (int i = 1; i<=ATTEMPTS; i++) {
handle.State = InterProcessConnectionState.Creating;
handle.Handle = NamedPipeNative.CreateNamedPipe(
name,
NamedPipeNative.PIPE_ACCESS_DUPLEX,
NamedPipeNative.PIPE_TYPE_MESSAGE |
NamedPipeNative.PIPE_READMODE_MESSAGE |
NamedPipeNative.PIPE_WAIT,
NamedPipeNative.PIPE_UNLIMITED_INSTANCES,
outBuffer,
inBuffer,
NamedPipeNative.NMPWAIT_WAIT_FOREVER,
IntPtr.Zero);
if (handle.Handle.ToInt32() !=
NamedPipeNative.INVALID_HANDLE_VALUE) {
handle.State = InterProcessConnectionState.Created;
break;
}
if (i >= ATTEMPTS) {
handle.State = InterProcessConnectionState.Error;
throw new NamedPipeIOException("Error creating named pipe "
+ name + " . Internal error: " +
NamedPipeNative.GetLastError().ToString(),
NamedPipeNative.GetLastError());
}
}
return handle;
}
By calling NamedPipeNative.CreateNamedPipe the above method creates a duplex Named Pipe of type message and sets it in blocking mode. It is also specified that unlimited instances of the pipe will be allowed. If the pipe is created successfully, CreateNamedPipe returns the native pipe handle, which we assign to our PipeHandle object. The native handle is an operating system pointer to the Named Pipe and is further used in all pipe related operations. The PipeHandle class is introduced to hold the native handle and also to track the current state of the pipe. The Named Pipes states are defined in the InterProcessConnectionState enumeration and they correspond to the different operations - reading, writing, waiting for clients, etc. Assuming that the server Named Pipe was created successfully, it can now start listening to client connections. .NET Named Pipes Discussion
|
|||||||||||||
| © 2009 by Ivan Latunov. Disclaimer. | |||||||||||||