Пример #1
0
        public async virtual Task ReadAsync(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);
            }

            await Data.WriteAsync(buffer, offset, length);

            LengthReceived += 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 void Read(byte[] buffer, OperationHeader header, ErrorCode errorCode = null)
        {
            Header    = header;
            ErrorCode = errorCode;

            if (buffer?.Length > 0)
            {
                Data.Write(buffer, 0, buffer.Length);
                LengthReceived += buffer.Length;
            }
        }
        protected virtual OperationBody ReadBody(IConnection connection, OperationHeader header)
        {
            var buffer = new byte[header.BodyLength];
            connection.Handle.Receive(buffer, 0, buffer.Length, SocketFlags.None);

            return new OperationBody
            {
                Extras = new ArraySegment<byte>(buffer, 0, header.ExtrasLength),
                Data = new ArraySegment<byte>(buffer, header.ExtrasLength, buffer.Length - header.ExtrasLength),
            };
        }
Пример #5
0
 public virtual void Reset()
 {
     if (Data != null)
     {
         Data.Dispose();
     }
     LengthReceived = 0;
     Data           = new MemoryStream();
     LengthReceived = 0;
     Buffer         = null;
     Header         = new OperationHeader();
 }
 /// <summary>
 /// Sets all values back to their defaults, so this object can be reused.
 /// </summary>
 public void Reset()
 {
     if(Data != null)
     {
         Data.Dispose();
     }
     Buffer = new byte[512];
     Data = new MemoryStream();
     BytesReceived = 0;
     Header = new OperationHeader();
     Body = new OperationBody();
 }
Пример #7
0
        public async Task ReadAsync(byte[] buffer, OperationHeader header, ErrorCode errorCode = null)
        {
            Header    = header;
            ErrorCode = errorCode;

            if (buffer?.Length > 0)
            {
                await Data.WriteAsync(buffer, 0, buffer.Length);

                LengthReceived += buffer.Length;
            }
        }
Пример #8
0
 protected OperationBase(string key, IVBucket vBucket, ITypeTranscoder transcoder, uint opaque, uint timeout)
 {
     Key = key;
     Transcoder = transcoder;
     Opaque = opaque;
     CreationTime = DateTime.UtcNow;
     Timeout = timeout;
     VBucket = vBucket;
     Converter = transcoder.Converter;
     MaxRetries = DefaultRetries;
     Data = new MemoryStream();
     Header = new OperationHeader {Status = ResponseStatus.None};
 }
        internal static long?GetServerDuration(this OperationHeader header, byte[] buffer)
        {
            if (header.FramingExtrasLength <= 0)
            {
                return(null);
            }

            // copy framing extra bytes
            var bytes = new byte[header.FramingExtrasLength];

            Buffer.BlockCopy(buffer, HeaderIndexFor.HeaderLength, bytes, 0, header.FramingExtrasLength);

            return(GetServerDuration(bytes));
        }
Пример #10
0
 protected OperationBase(string key, IVBucket vBucket, ITypeTranscoder transcoder, uint opaque, uint timeout)
 {
     Key          = key;
     Transcoder   = transcoder;
     Opaque       = opaque;
     CreationTime = DateTime.UtcNow;
     Timeout      = timeout;
     VBucket      = vBucket;
     Converter    = transcoder.Converter;
     MaxRetries   = DefaultRetries;
     Data         = new MemoryStream();
     Header       = new OperationHeader {
         Status = ResponseStatus.None
     };
 }
        internal static long?GetServerDuration(this OperationHeader header, MemoryStream stream)
        {
            if (header.FramingExtrasLength <= 0)
            {
                return(null);
            }

            // copy framing extra bytes then reset steam position
            var bytes = new byte[header.FramingExtrasLength];

            stream.Position = HeaderIndexFor.HeaderLength;
            stream.Read(bytes, 0, header.FramingExtrasLength);
            stream.Position = 0;

            return(GetServerDuration(bytes));
        }
        protected OperationBase(string key, IVBucket vBucket, ITypeTranscoder transcoder, uint opaque, uint timeout)
        {
            if (RequiresKey && string.IsNullOrWhiteSpace(key))
            {
                throw new MissingKeyException();
            }

            Key = key;
            Transcoder = transcoder;
            Opaque = opaque;
            CreationTime = DateTime.UtcNow;
            Timeout = timeout;
            VBucket = vBucket;
            Converter = transcoder.Converter;
            MaxRetries = DefaultRetries;
            Data = new MemoryStream();
            Header = new OperationHeader {Status = ResponseStatus.None};
        }
Пример #13
0
        public virtual void Reset(ResponseStatus status)
        {
            if (Data != null)
            {
                Data.Dispose();
            }
            Data           = MemoryStreamFactory.GetMemoryStream();
            LengthReceived = 0;

            Header = new OperationHeader
            {
                Magic         = Header.Magic,
                OperationCode = OperationCode,
                Cas           = Header.Cas,
                BodyLength    = Header.BodyLength,
                Key           = Key,
                Status        = status
            };
        }
Пример #14
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);
 }
Пример #15
0
        protected OperationBase(string key, IVBucket vBucket, ITypeTranscoder transcoder, uint opaque, uint timeout)
        {
            if (RequiresKey && string.IsNullOrWhiteSpace(key))
            {
                throw new MissingKeyException();
            }

            Key          = key;
            Transcoder   = transcoder;
            Opaque       = opaque;
            CreationTime = DateTime.UtcNow;
            Timeout      = timeout;
            VBucket      = vBucket;
            Converter    = transcoder.Converter;
            MaxRetries   = DefaultRetries;
            Data         = MemoryStreamFactory.GetMemoryStream();
            Header       = new OperationHeader {
                Status = ResponseStatus.None
            };
        }
Пример #16
0
        public virtual void HandleClientError(string message)
        {
            Header = new OperationHeader
            {
                Magic         = 0,
                OperationCode = OperationCode,
                Cas           = 0,
                BodyLength    = 0,
                Key           = Key,
                Status        = ResponseStatus.ClientFailure
            };
            var msgBytes = Encoding.UTF8.GetBytes(message);

            LengthReceived += msgBytes.Length;
            if (Data == null)
            {
                Data = new MemoryStream();
            }
            Data.Write(msgBytes, 0, msgBytes.Length);
        }
Пример #17
0
        public virtual void Reset(ResponseStatus status)
        {
            if (Data != null)
            {
                Data.Dispose();
            }
            Data = new MemoryStream();
            LengthReceived = 0;

            Header = new OperationHeader
            {
                Magic = Header.Magic,
                OperationCode = OperationCode,
                Cas = Header.Cas,
                BodyLength = Header.BodyLength,
                Key = Key,
                Status = status
            };
        }
Пример #18
0
        public async virtual Task ReadAsync(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);
            }

            await Data.WriteAsync(buffer, offset, length);
            LengthReceived += length;
        }