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

2

60

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?
Vercingetorix answered 22/12, 2010 at 23:50 Comment(0)
C
73

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().

Costello answered 23/12, 2010 at 15:10 Comment(0)
V
14

Took me a while to find the little important detail that you need to create both the server and client in PipeDirection.InOut mode:

It may be odd, but for some reason, a NamedPipeServerStream must be created with a PipeDirection parameter of InOut in order for PipeTransmissionMode.Message to work. Not only is this not directly documented, but the way the error is reported is completely counter-intuitive, and appears to have nothing to do with the pipe's TransmissionMode.

Otherwise you'll get the exception:

Attempting to connect to pipe...System.UnauthorizedAccessException:

Access to the path is denied.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Pipes.PipeStream.WinIOError(Int32 errorCode)
   at System.IO.Pipes.PipeStream.set_ReadMode(PipeTransmissionMode value)
Vlaminck answered 20/2, 2015 at 18:24 Comment(1)
I've posted a full explanation of this problem and a better solution here: https://mcmap.net/q/330585/-c-unauthorizedaccessexception-when-enabling-messagemode-for-read-only-named-pipe-namedpipeclientstream-class/3418322Negus

© 2022 - 2024 — McMap. All rights reserved.