示例#1
0
        public void Serializer(Stream stream, IMessageSerializer serializeHelper)
        {
            var len = new UnsignedInt((uint)Value.Length);

            len.Serializer(stream, serializeHelper);
            stream.Write(Value, 0, Value.Length);
        }
示例#2
0
        public void Serializer(Stream stream, IMessageSerializer serializeHelper)
        {
            var buf = Hex.HexToBytes(Value);
            var len = new UnsignedInt((uint)buf.Length);

            len.Serializer(stream, serializeHelper);
            stream.Write(buf, 0, buf.Length);
        }
        public virtual void AddToMessageStream(Stream stream, Type type, object val)
        {
            switch (val)
            {
            case bool typed:
            {
                stream.WriteByte((byte)(typed ? 1 : 0));
                return;
            }

            case byte typed:
            {
                stream.WriteByte(typed);
                return;
            }

            case short typed:
            {
                var buf = BitConverter.GetBytes(typed);
                stream.Write(buf, 0, buf.Length);
                return;
            }

            case ushort typed:
            {
                var buf = BitConverter.GetBytes(typed);
                stream.Write(buf, 0, buf.Length);
                return;
            }

            case int typed:
            {
                var buf = BitConverter.GetBytes(typed);
                stream.Write(buf, 0, buf.Length);
                return;
            }

            case uint typed:
            {
                var buf = BitConverter.GetBytes(typed);
                stream.Write(buf, 0, buf.Length);
                return;
            }

            case long typed:
            {
                var buf = BitConverter.GetBytes(typed);
                stream.Write(buf, 0, buf.Length);
                return;
            }

            case ulong typed:
            {
                var buf = BitConverter.GetBytes(typed);
                stream.Write(buf, 0, buf.Length);
                return;
            }

            case float typed:
            {
                var buf = BitConverter.GetBytes(typed);
                stream.Write(buf, 0, buf.Length);
                return;
            }

            case double typed:
            {
                var buf = BitConverter.GetBytes(typed);
                stream.Write(buf, 0, buf.Length);
                return;
            }

            case byte[] typed:
            {
                stream.Write(typed, 0, typed.Length);
                return;
            }

            case string typed:
            {
                if (string.IsNullOrEmpty(typed))
                {
                    stream.WriteByte(0);
                    return;
                }
                var buf = Encoding.UTF8.GetBytes(typed);

                var buflen = new UnsignedInt((uint)buf.Length);
                buflen.Serializer(stream, this);

                stream.Write(buf, 0, buf.Length);
                return;
            }

            case ICustomSerializer typed:
            {
                typed.Serializer(stream, this);
                return;
            }

            default:
            {
                if (type.IsEnum)
                {
                    stream.WriteByte((byte)val);
                    return;
                }
                if (type.IsArray)
                {
                    var typed = (ICollection)val;
                    if (typed == null)
                    {
                        return;
                    }

                    var buf = new UnsignedInt((uint)typed.Count);
                    buf.Serializer(stream, this);

                    foreach (var value in typed)
                    {
                        AddToMessageStream(stream, value.GetType(), value);
                    }
                    return;
                }
                if (type.IsClass)
                {
                    var chType     = val.GetType();
                    var properties = GetPropertiesForMessage(chType);
                    foreach (var prop in properties)
                    {
                        AddToMessageStream(stream, prop, val);
                    }
                    return;
                }
                throw new NotImplementedException();
            }
            }
        }