Abstractions.Pipes.PipeMessage.Demarshal C# (CSharp) Метод

Demarshal() публичный статический Метод

public static Demarshal ( byte data ) : object>.IDictionary
data byte
Результат object>.IDictionary
        public static IDictionary<string, object> Demarshal(byte[] data)
        {
            // Cannot be empty
            if (data.Length == 0)
                throw new InvalidDataException(string.Format("Empty message provided, invalid format."));

            using (BinaryReader br = new BinaryReader(new MemoryStream(data, false), Encoding.Unicode))
            {
                // For now we support only the one message version
                byte messageFormatVersion = br.ReadByte();
                if (messageFormatVersion != CurrentMessageFormatVersion)
                    throw new InvalidDataException(string.Format("Message format: {0} is not support (library version: {1})", messageFormatVersion, CurrentMessageFormatVersion));

                // Create a dict for the message contents
                Dictionary<string, object> messageDict = new Dictionary<string, object>();

                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    string propertyName = br.ReadString();
                    DataType propertyType = (DataType)br.ReadByte();

                    switch (propertyType)
                    {
                        case DataType.Boolean:
                            messageDict.Add(propertyName, br.ReadBoolean());
                            break;
                        case DataType.Byte:
                            messageDict.Add(propertyName, br.ReadByte());
                            break;
                        case DataType.Integer:
                            messageDict.Add(propertyName, br.ReadInt32());
                            break;
                        case DataType.String:
                            messageDict.Add(propertyName, br.ReadString());
                            break;
                        case DataType.EmptyString:
                            messageDict.Add(propertyName, (string) null);
                            break;
                        default:
                            throw new InvalidDataException(string.Format("Message includes unknown data type: {0} for property: {1}", propertyType, propertyName));
                    }
                }

                return messageDict;
            }
        }

Usage Example

Пример #1
0
        protected bool DefaultMessageHandler(BinaryReader reader, BinaryWriter writer, Func <IDictionary <string, object>, IDictionary <string, object> > callback)
        {
            int len = reader.ReadInt32();

            byte[] bytes = reader.ReadBytes(len);
            IDictionary <string, object> msg   = PipeMessage.Demarshal(bytes);
            IDictionary <string, object> reply = callback(msg);

            if (reply != null)
            {
                WriteMessage(writer, reply);

                if (((IDictionary <String, Object>)reply).ContainsKey("LastMessage"))
                {
                    ((IDictionary <String, Object>)reply).Remove("LastMessage"); // Don't marshal this property
                    return(false);
                }
            }

            return(reply != null);
        }