Пример #1
0
        public virtual void Read(byte[] buffer, int offset, int length)
        {
            if (Header.BodyLength == 0 && buffer.Length >= HeaderIndexFor.HeaderLength)
            {
                Header = new OperationHeader
                {
                    Magic         = Converter.ToByte(buffer, HeaderIndexFor.Magic),
                    OperationCode = Converter.ToByte(buffer, HeaderIndexFor.Opcode).ToOpCode(),
                    KeyLength     = Converter.ToInt16(buffer, HeaderIndexFor.KeyLength),
                    ExtrasLength  = Converter.ToByte(buffer, HeaderIndexFor.ExtrasLength),
                    Status        = (ResponseStatus)Converter.ToInt16(buffer, HeaderIndexFor.Status),
                    BodyLength    = Converter.ToInt32(buffer, HeaderIndexFor.Body),
                    Opaque        = Converter.ToUInt32(buffer, HeaderIndexFor.Opaque),
                    Cas           = Converter.ToUInt64(buffer, HeaderIndexFor.Cas)
                };

                if (Opaque != Header.Opaque)
                {
                    var msg = string.Format("Expected opaque {0} but got {1}", Opaque, Header.Opaque);
                    HandleClientError(msg, ResponseStatus.ClientFailure);
                }
            }
            LengthReceived += length;
            Data.Write(buffer, offset, length);
        }
        private void ReadContent(byte[] buffer, ErrorMap errorMap)
        {
            if (Header.BodyLength == 0 && buffer.Length >= HeaderIndexFor.HeaderLength)
            {
                var status = GetResponseStatus(Converter.ToInt16(buffer, HeaderIndexFor.Status), errorMap);
                Header = new OperationHeader
                {
                    Magic         = Converter.ToByte(buffer, HeaderIndexFor.Magic),
                    OperationCode = Converter.ToByte(buffer, HeaderIndexFor.Opcode).ToOpCode(),
                    KeyLength     = Converter.ToInt16(buffer, HeaderIndexFor.KeyLength),
                    ExtrasLength  = Converter.ToByte(buffer, HeaderIndexFor.ExtrasLength),
                    Status        = status,
                    BodyLength    = Converter.ToInt32(buffer, HeaderIndexFor.Body),
                    Opaque        = Converter.ToUInt32(buffer, HeaderIndexFor.Opaque),
                    Cas           = Converter.ToUInt64(buffer, HeaderIndexFor.Cas)
                };

                if (Opaque != Header.Opaque)
                {
                    var msg = string.Format("Expected opaque {0} but got {1}", Opaque, Header.Opaque);
                    HandleClientError(msg, ResponseStatus.ClientFailure);
                }
            }
            LengthReceived += buffer.Length;
        }
Пример #3
0
 public virtual void Read(byte[] buffer, int offset, int length)
 {
     if (Header.BodyLength == 0)
     {
         Header = new OperationHeader
         {
             Magic         = Converter.ToByte(buffer, HeaderIndexFor.Magic),
             OperationCode = Converter.ToByte(buffer, HeaderIndexFor.Opcode).ToOpCode(),
             KeyLength     = Converter.ToInt16(buffer, HeaderIndexFor.KeyLength),
             ExtrasLength  = Converter.ToByte(buffer, HeaderIndexFor.ExtrasLength),
             Status        = (ResponseStatus)Converter.ToInt16(buffer, HeaderIndexFor.Status),
             BodyLength    = Converter.ToInt32(buffer, HeaderIndexFor.Body),
             Opaque        = Converter.ToUInt32(buffer, HeaderIndexFor.Opaque),
             Cas           = Converter.ToUInt64(buffer, HeaderIndexFor.Cas)
         };
     }
     LengthReceived += length;
     Data.Write(buffer, offset, length);
 }
Пример #4
0
        protected void CreateHeader(OperationAsyncState state)
        {
            var buffer = state.Data.GetBuffer();

            if (buffer.Length > 0)
            {
                state.Header = new OperationHeader
                {
                    Magic         = _converter.ToByte(buffer, HeaderIndexFor.Magic),
                    OperationCode = _converter.ToByte(buffer, HeaderIndexFor.Opcode).ToOpCode(),
                    KeyLength     = _converter.ToInt16(buffer, HeaderIndexFor.KeyLength),
                    ExtrasLength  = _converter.ToByte(buffer, HeaderIndexFor.ExtrasLength),
                    Status        = (ResponseStatus)_converter.ToInt16(buffer, HeaderIndexFor.Status),
                    BodyLength    = _converter.ToInt32(buffer, HeaderIndexFor.Body),
                    Opaque        = _converter.ToUInt32(buffer, HeaderIndexFor.Opaque),
                    Cas           = _converter.ToUInt64(buffer, HeaderIndexFor.Cas)
                };
            }
        }
Пример #5
0
        internal static OperationHeader CreateHeader(this ReadOnlySpan <byte> buffer, ErrorMap errorMap, out ErrorCode errorCode)
        {
            if (buffer == null || buffer.Length < OperationHeader.Length)
            {
                errorCode = null;
                return(new OperationHeader {
                    Status = ResponseStatus.None
                });
            }

            int keyLength, framingExtrasLength;
            var magic = (Magic)buffer[HeaderOffsets.Magic];

            if (magic == Magic.AltResponse)
            {
                framingExtrasLength = buffer[HeaderOffsets.FramingExtras];
                keyLength           = buffer[HeaderOffsets.AltKeyLength];
            }
            else
            {
                framingExtrasLength = 0;
                keyLength           = Converter.ToInt16(buffer.Slice(HeaderOffsets.KeyLength));
            }

            var statusCode = Converter.ToInt16(buffer.Slice(HeaderOffsets.Status));
            var status     = GetResponseStatus(statusCode, errorMap, out errorCode);

            return(new OperationHeader
            {
                Magic = (byte)magic,
                OpCode = buffer[HeaderOffsets.Opcode].ToOpCode(),
                FramingExtrasLength = framingExtrasLength,
                KeyLength = keyLength,
                ExtrasLength = buffer[HeaderOffsets.ExtrasLength],
                DataType = (DataType)buffer[HeaderOffsets.Datatype],
                Status = status,
                BodyLength = Converter.ToInt32(buffer.Slice(HeaderOffsets.Body)),
                Opaque = Converter.ToUInt32(buffer.Slice(HeaderOffsets.Opaque)),
                Cas = Converter.ToUInt64(buffer.Slice(HeaderOffsets.Cas))
            });
        }
        internal static OperationHeader CreateHeader(this byte[] buffer, ErrorMap errorMap, out ErrorCode errorCode)
        {
            if (buffer == null || buffer.Length < HeaderIndexFor.HeaderLength)
            {
                errorCode = null;
                return(new OperationHeader {
                    Status = ResponseStatus.None
                });
            }

            int keyLength, framingExtrasLength;
            var magic = (Magic)Converter.ToByte(buffer, HeaderIndexFor.Magic);

            if (magic == Magic.AltResponse)
            {
                framingExtrasLength = Converter.ToByte(buffer, HeaderIndexFor.FramingExtras);
                keyLength           = Converter.ToByte(buffer, HeaderIndexFor.AltKeyLength);
            }
            else
            {
                framingExtrasLength = 0;
                keyLength           = Converter.ToInt16(buffer, HeaderIndexFor.KeyLength);
            }

            var statusCode = Converter.ToInt16(buffer, HeaderIndexFor.Status);
            var status     = GetResponseStatus(statusCode, errorMap, out errorCode);

            return(new OperationHeader
            {
                Magic = (byte)magic,
                OperationCode = Converter.ToByte(buffer, HeaderIndexFor.Opcode).ToOpCode(),
                FramingExtrasLength = framingExtrasLength,
                KeyLength = keyLength,
                ExtrasLength = Converter.ToByte(buffer, HeaderIndexFor.ExtrasLength),
                DataType = (DataType)Converter.ToByte(buffer, HeaderIndexFor.Datatype),
                Status = status,
                BodyLength = Converter.ToInt32(buffer, HeaderIndexFor.Body),
                Opaque = Converter.ToUInt32(buffer, HeaderIndexFor.Opaque),
                Cas = Converter.ToUInt64(buffer, HeaderIndexFor.Cas)
            });
        }
Пример #7
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="UInt64"/> from a buffer, using network byte order.
 /// </summary>
 /// <param name="converter">The converter.</param>
 /// <param name="buffer">The buffer.</param>
 /// <returns></returns>
 public static ulong ToUInt64(this IByteConverter converter, ReadOnlySpan <byte> buffer)
 {
     return(converter.ToUInt64(buffer, true));
 }