예제 #1
0
        public virtual void ReadExtras(byte[] buffer)
        {
            if (buffer.Length > 24)
            {
                var format = new byte();
                var flags  = Converter.ToByte(buffer, 24);
                Converter.SetBit(ref format, 0, Converter.GetBit(flags, 0));
                Converter.SetBit(ref format, 1, Converter.GetBit(flags, 1));
                Converter.SetBit(ref format, 2, Converter.GetBit(flags, 2));
                Converter.SetBit(ref format, 3, Converter.GetBit(flags, 3));

                var compression = new byte();
                Converter.SetBit(ref compression, 4, Converter.GetBit(flags, 4));
                Converter.SetBit(ref compression, 5, Converter.GetBit(flags, 5));
                Converter.SetBit(ref compression, 6, Converter.GetBit(flags, 6));

                var typeCode = (TypeCode)(Converter.ToUInt16(buffer, 26) & 0xff);
                Format            = (DataFormat)format;
                Compression       = (Compression)compression;
                Flags.DataFormat  = Format;
                Flags.Compression = Compression;
                Flags.TypeCode    = typeCode;
                Expires           = Converter.ToUInt32(buffer, 25);
            }
        }
예제 #2
0
        private void ParseSpecs()
        {
            var response     = _contentBytes.Memory.Slice(Header.BodyOffset);
            var commandIndex = 0;

            for (;;)
            {
                var bodyLength = _converter.ToInt32(response.Span.Slice(2));
                var payLoad    = response.Slice(6, bodyLength);

                var command = _specs[commandIndex++];
                command.Status      = (ResponseStatus)_converter.ToUInt16(response.Span);
                command.ValueIsJson = payLoad.Span.IsJson();
                command.Bytes       = payLoad;

                response = response.Slice(6 + bodyLength);

                if (response.Length <= 0)
                {
                    break;
                }
            }
        }
예제 #3
0
        public T ContentAs <T>(int index)
        {
            var response          = _bytes;
            var statusOffset      = 24;//Header.BodyOffset;
            var valueLengthOffset = statusOffset + 2;
            var valueOffset       = statusOffset + 6;

            var operationSpecs = new List <OperationSpec>();

            for (;;)
            {
                var bodyLength = _converter.ToInt32(response, valueLengthOffset);
                var payLoad    = new byte[bodyLength];
                Buffer.BlockCopy(response, valueOffset, payLoad, 0, bodyLength);

                var command = new OperationSpec
                {
                    Status      = (ResponseStatus)_converter.ToUInt16(response, statusOffset),
                    ValueIsJson = payLoad.IsJson(0, bodyLength - 1),
                    Bytes       = payLoad
                };
                operationSpecs.Add(command);

                statusOffset      = valueOffset + bodyLength;
                valueLengthOffset = statusOffset + 2;
                valueOffset       = statusOffset + 6;

                if (valueOffset >= response.Length)
                {
                    break;
                }
            }

            var spec = operationSpecs[index];

            return(_serializer.Deserialize <T>(spec.Bytes, 0, spec.Bytes.Length));
        }
예제 #4
0
        public T Deserialize <T>(byte[] buffer, int offset, int length)
        {
            object value = default(T);

            var typeCode = Type.GetTypeCode(typeof(T));

            switch (typeCode)
            {
            case TypeCode.Empty:
            case TypeCode.DBNull:
            case TypeCode.String:
            case TypeCode.Char:
                value = Deserialize(buffer, offset, length);
                break;

            case TypeCode.Int16:
                if (length > 0)
                {
                    value = _converter.ToInt16(buffer, offset);
                }
                break;

            case TypeCode.UInt16:
                if (length > 0)
                {
                    value = _converter.ToUInt16(buffer, offset);
                }
                break;

            case TypeCode.Int32:
                if (length > 0)
                {
                    value = _converter.ToInt32(buffer, offset);
                }
                break;

            case TypeCode.UInt32:
                if (length > 0)
                {
                    value = _converter.ToUInt32(buffer, offset);
                }
                break;

            case TypeCode.Int64:
                if (length > 0)
                {
                    value = _converter.ToInt64(buffer, offset);
                }
                break;

            case TypeCode.UInt64:
                if (length > 0)
                {
                    value = _converter.ToUInt64(buffer, offset);
                }
                break;

            case TypeCode.Single:
                break;

            case TypeCode.Double:
                break;

            case TypeCode.Decimal:
                break;

            case TypeCode.DateTime:
                break;

            case TypeCode.Boolean:
                break;

            case TypeCode.SByte:
                break;

            case TypeCode.Byte:
                break;

            case TypeCode.Object:
                value = DeserializeAsJson <T>(buffer, offset, length);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return((T)value);
        }
 /// <summary>
 /// Reads a <see cref="UInt16"/> from a buffer, using network byte order.
 /// </summary>
 /// <param name="converter">The converter.</param>
 /// <param name="buffer">The buffer.</param>
 /// <returns></returns>
 public static ushort ToUInt16(this IByteConverter converter, ReadOnlySpan <byte> buffer)
 {
     return(converter.ToUInt16(buffer, true));
 }