Post

PipeMessage

A message passed through a pipe between a server and its clients.


Remarks

A basic structure for packaging messages that are passed through a pipe stream. Data should be packaged into a byte[] array, presumably by a serialize function. Each PipeMessage contains a single character message code (MsgCode), used to indicate the type of content in the byte[] array. Only a char value of ‘0’ is reserved for encoding and decoding strings. Developers can use the other 254 characters for their own purposes.


Fields

IdentifierTypeDescription
MsgCodecharIdentifies the content of MsgData; ‘0’ is reserved for strings.
MsgDatabyte[]The message payload.

Methods

MethodReturnsDescription
DecodeString()stringDecoder method to convert the byte[] MsgData array to a string.
EncodeString(string str)voidEncoder method to convert any string into the byte[] MsgData.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using Nova.IPC.Pipes;

PipeMessage text = new PipeMessage();
text.EncodeString("Hello!");     // MsgCode is now '0'

PipeMessage data = new PipeMessage();
data.MsgCode = '1';              // a code the receiver knows how to interpret
data.MsgData = new byte[] { 1, 2, 3 };

foreach (PipeMessage message in new[] { text, data })
{
  if (message.MsgCode == '0')
  {
    Console.WriteLine(message.DecodeString());
  }
  else
  {
    Console.WriteLine("Code " + message.MsgCode + ": " + message.MsgData.Length + " bytes");
  }
}
This post is licensed under CC BY 4.0 by the author.

Trending Tags