Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PipeTransmissionMode.Message: How do .NET named pipes distinguish between messages?

Can somebody clarify the meaning of PipeTransmissionMode.Message in .NET?

How does .NET distinguish one message passed through the pipe from another?

  • Can I serialize an object using a BinaryFormatter and then pass it through the pipe as a message?
  • Or are only string messages allowed when the pipe is in PipeTransmissionMode.Message mode?
like image 402
Art Spasky Avatar asked Dec 22 '10 23:12

Art Spasky


People also ask

How does Named Pipes work?

A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients. All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles, and provides a separate conduit for client/server communication.

What is named pipe in C#?

Named pipes provide interprocess communication between a pipe server and one or more pipe clients. Named pipes can be one-way or duplex. They support message-based communication and allow multiple clients to connect simultaneously to the server process using the same pipe name.

How do you make a named pipe in Windows?

To create an instance of a named pipe by using CreateNamedPipe, the user must have FILE_CREATE_PIPE_INSTANCE access to the named pipe object. If a new named pipe is being created, the access control list (ACL) from the security attributes parameter defines the discretionary access control for the named pipe.


1 Answers

The pipe transmission mode is a Windows operating system concept, not a .NET concept. If a pipe is created in Message mode, each write to the pipe by the sender is treated as a separate message. The receiver can read from the pipe either:

  • in Byte mode, when the data is read from the pipe as a stream of bytes, completely ignoring the implicit message boundaries; or
  • in Message mode, when data is read as a stream of messages, in the sense that any read will only receive bytes relating to a single message, and a special error code is returned by the native API to indicate if there are further bytes to be received for that same message.

The .NET wrapping of this functionality, as surfaced in the System.IO.Pipes namespace, follows the underlying native model fairly closely:

  • message boundaries are still determined by the pattern of calls made by the sender to PipeStream.Write() or PipeStream.WriteByte() - the data written in each call is treated as a distinct message;
  • the receiver can set ReadMode to PipeTransmissionMode.Message, and then every call to PipeStream.Read() or PipeStream.ReadByte() will read the next chunk of data from the current message, until the value of PipeStream.IsMessageComplete changes to true, indicating that all the bytes for that message have been read

All reads and writes are done in terms of bytes or arrays of bytes. You can send whatever bytes you like down a pipe. The TransmissionMode has no bearing on this.

So, yes, you can send a serialized object as a message, provided you write all the bytes of its serialized representation to the pipe in a single call to PipeStream.Write().

like image 162
Chris Dickson Avatar answered Sep 20 '22 03:09

Chris Dickson